All files / store bag.js

0% Statements 0/27
0% Branches 0/5
0% Functions 0/13
0% Lines 0/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                                                                                       
import { endOfDay, startOfDay, addDays } from 'date-fns'
import uniqBy from 'lodash.uniqby'
 
export const store = () => ({})
 
export const getters = {
  _subjects: (state, getters, rootState, rootGetters) => (today = null) => {
    // Get days
    today = today || rootState.now
    const tomorrow = addDays(today, 1)
    // Get courses for theses days
    const todayCourses = rootGetters['schedule/coursesIn'](
      startOfDay(today),
      endOfDay(today)
    )
    const tomorrowCourses = rootGetters['schedule/coursesIn'](
      startOfDay(tomorrow),
      endOfDay(tomorrow)
    )
    // Get subjects of theses courses
    let todaySubjects = todayCourses.map(o => o.subject)
    let tomorrowSubjects = tomorrowCourses.map(o => o.subject)
    // Remove duplicate subjects
    todaySubjects = uniqBy(todaySubjects, 'uuid')
    tomorrowSubjects = uniqBy(tomorrowSubjects, 'uuid')
    // Return both arrays
    return {
      todaySubjects,
      tomorrowSubjects,
    }
  },
  toAdd: (state, getters) => (today = null) => {
    /* Gets the subjects that are not present in today's courses
     * but are present in tomorrow's courses.
     */
    // Get subjects
    const { todaySubjects, tomorrowSubjects } = getters._subjects(today)
    // Get an array of UUIDs to check agains
    const todayUUIDs = todaySubjects.map(o => o.uuid)
    // Keep only tomorrow's subjects if their UUIDs are not in today's UUIDs.
    return tomorrowSubjects.filter(o => !todayUUIDs.includes(o.uuid))
  },
  toRemove: (state, getters) => (today = null) => {
    // Get subjects
    const { todaySubjects, tomorrowSubjects } = getters._subjects(today)
    // Get an array of UUIDs to check agains
    const tomorrowUUIDs = tomorrowSubjects.map(o => o.uuid)
    // Keep only today's subjects if their UUIDs are not in tomorrow's UUIDs.
    return todaySubjects.filter(o => !tomorrowUUIDs.includes(o.uuid))
  },
}