All files / store homework.js

0% Statements 0/155
0% Branches 0/84
0% Functions 0/55
0% Lines 0/134

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import { firstBy } from 'thenby'
import groupBy from 'lodash.groupby'
import {
  isSameWeek,
  differenceInWeeks,
  isBefore,
  parseISO,
  getUnixTime,
  fromUnixTime,
  isSameDay,
} from 'date-fns'
// (caused by removeTime) eslint-disable-next-line import/named
import { getValidator, getMutations, removeTime } from './index'
 
export const state = () => ({
  homeworks: [],
  types: [
    { importance: 1, key: 'TOBRING', label: 'À apporter' },
    { importance: 1, key: 'EXERCISE', label: 'Exercice' },
    { importance: 2, key: 'COURSEWORK', label: 'Travail noté' },
    { importance: 3, key: 'TEST', label: 'Contrôle' },
  ],
  loaded: false,
})
 
const parseHomeworkDates = homework => {
  if (homework.due && typeof homework.due === 'string')
    homework.due = removeTime(parseISO(homework.due))
  if (homework.added && typeof homework.added === 'string')
    homework.added = parseISO(homework.added)
  return homework
}
 
// For comparisons to determinate if homework is late or not.
const getTodayDate = () => {
  const today = new Date()
  today.setHours(0)
  today.setMinutes(0)
  today.setSeconds(0)
  return today
}
 
export const getters = {
  types: ({ types }) => types,
  all: ({ homeworks }, { order }) => order(homeworks),
  one: (_, { all }) => (value, prop = 'uuid') =>
    all.find(o => o[prop] === value) || null,
  nextWeek: (_, { all }, rootState) =>
    all.filter(o => differenceInWeeks(o.due, rootState.now) === 1),
  currentWeek: (_, { all }, rootState) =>
    all.filter(o => isSameWeek(o.due, rootState.now)),
  currentOrNextWeek: (_, { currentWeek, nextWeek }) => [
    ...currentWeek,
    ...nextWeek,
  ],
  importanceOf: ({ types }) => homework => {
    if (types.map(t => t.key).includes(homework.type)) {
      return types.find(t => t.key === homework.type).importance
    }
    return 0
  },
  pending: (_, { currentOrNextWeek }) =>
    currentOrNextWeek.filter(o => o.progress === 1),
  order: (_, { importanceOf }) => homeworks =>
    [...homeworks].sort(
      firstBy((o1, o2) => isBefore(o1.due, o2.due))
        .thenBy(o => importanceOf(o))
        .thenBy(o => o.subject.weight)
        .thenBy('uuid')
    ),
  group: (_, { _needToShowGroup }, rootState) => (
    homeworks,
    specialGroups = ['late', 'today']
  ) => {
    // TODO(beta-2.0.0): refactor this
    /* Groups the provided array of homework by due date
     * into an array of groups:
     * [ { due: <date>, homeworks: [ ... ], shown: <bool> }, ... ]
     * the shown bool is computed by getters._needToShowGroup
     */
    if (homeworks.length < 1) return []
    if (specialGroups.length) {
      homeworks = homeworks.map(hw => {
        if (
          specialGroups.includes('late') &&
          isBefore(hw.due, getTodayDate()) &&
          !isSameDay(hw.due, getTodayDate())
        ) {
          return { ...hw, due: 'LATE', realDue: getUnixTime(hw.due) }
        } else {
          return { ...hw, due: getUnixTime(hw.due) }
        }
      })
    }
    // Remove done homework from LATE group
    homeworks = homeworks.filter(hw => {
      if (hw.due === 'LATE') {
        return hw.progress < 1
      }
      return true
    })
    const map = groupBy(homeworks, 'due')
    let flat = []
    for (const [due, homeworks] of Object.entries(map)) {
      flat.push({ due, homeworks })
    }
    // To put the LATE group at the top, we change its value to a negtive number.
    const LATE_DUE_NUMBER = -666 // No reason to use this number, huh. R-really, I-I-I mean... It's 1 AM
    const index = flat.findIndex(g => g.due === 'LATE')
    if (index !== -1) {
      flat[index].due = LATE_DUE_NUMBER
    }
    flat = flat.sort(firstBy('due'))
    flat = flat.map(g => ({
      ...g,
      homeworks: g.homeworks.map(h => ({ ...h, due: h.realDue || h.due })),
      due: g.due === LATE_DUE_NUMBER ? 'LATE' : g.due,
    }))
    return flat
  },
  _needToShowGroup: () => ({ due, homeworks, showCompleted }) => {
    /* Takes a group object ({due, homeworks}) and decides whether this
     * group needs to be shown to the user.
     */
 
    /* Only show...
    - if the group is non-empty, and...
		- - in case of a "late" or "today" group...
		- - - if at least one homework is still not completed
    - - if at least one homework is still not completed, *or*
    - - if any test (completed or not) is present, *or*
		- - if the setting "show completed exercises" is true
    */
    const isEmpty = homeworks.length === 0
    const someHomeworkNotCompleted = !!homeworks.filter(o => o.progress < 1)
      .length
    const hasTests = homeworks.filter(o => o.graded).length > 0
    // console.log({due, isEmpty, someHomeworkNotCompleted, hasTests, showCompleted})
    if (isEmpty) return false
    if (['LATE', 'TODAY'].includes(due)) {
      return someHomeworkNotCompleted
    }
    return someHomeworkNotCompleted || hasTests || showCompleted
  },
  grouped: (_, { group, all }) => group(all),
  only: (_, { all }) => (what, homeworks = null) => {
    homeworks = homeworks || all
    return homeworks.filter(
      o => o.type === what.replace(/(.+)s$/, ($0, $1) => $1).toUpperCase()
    )
  },
  exercises: (_, { only, all }) => only('exercises', all),
  tests: (_, { only, all }) => only('tests', all),
  counts: (_, { only, pending, currentOrNextWeek }) => ({
    exercises: only('exercises', pending),
    tests: only('tests', currentOrNextWeek),
  }),
  verboseType: (_, { types }) => type => types.find(t => t.key === type).label,
  validate: getValidator({
    constraints: {
      required: ['subject', 'name', 'due'],
      minimum: {
        0: ['progress'],
      },
      maximum: {
        1: ['progress'],
      },
      maxLength: {
        300: ['name', 'room'],
      },
    },
    customConstraints: [
      {
        field: 'type',
        message: 'Type de devoir invalide',
        constraint: ({ types }, object) =>
          types.map(t => t.key).includes(object.type),
      },
    ],
    fieldNames: {
      subject: { gender: 'F', name: 'matière' },
      name: { gender: 'M', name: 'nom' },
      type: { gender: 'M', name: 'type' },
      room: { gender: 'F', name: 'salle' },
      progress: { gender: 'F', name: 'progression' },
      due: { gender: 'F', name: 'date due' },
    },
    resourceName: { gender: 'M', name: 'devoir' },
  }),
}
 
export const mutations = {
  ...getMutations('homework', parseHomeworkDates),
  POSTLOAD: state => (state.loaded = true),
}
 
export const actions = {
  async load({ commit, state }, force = false) {
    if (!force && state.loaded) return
    try {
      const { data } = await this.$axios.get(`/homework/`)
      console.log(`[from API] GET /homework/: OK`)
      if (data) {
        commit('SET', data)
        commit('POSTLOAD')
      }
    } catch (error) {
      console.error(`[from API] GET /homework/: Error`)
      try {
        console.error(error.response.data)
      } catch (_) {
        // eslint-disable-next-line
        console.error(error)
      }
    }
  },
 
  async post({ commit, dispatch, getters }, { homework, force }) {
    force = force || false
    if (!force) {
      const validation = getters.validate(homework)
      if (!validation.validated) return validation
    }
    try {
      if (homework.subject) homework.subject = homework.subject.uuid
      const res = await this.$axios.post(`/homework/`, homework)
      const { data } = await this.$axios.get(`/homework/${res.data.uuid}`)
      if (data) {
        commit('ADD', data)
      }
    } catch (error) {
      // eslint-disable-next-line
      // eslint-disable-next-line
      console.error(error)
    }
  },
 
  async delete({ commit, dispatch, getters }, { uuid, force, toastMessage }) {
    try {
      console.log(uuid)
      // Stores the note object because we want to be able to cancel the deletion
      const homework = getters.one(uuid)
      //
      await this.$axios.delete(`/homework/${uuid}`)
      commit('DEL', uuid)
      this.$toast.show(toastMessage || 'Devoir supprimé', {
        action: {
          text: 'Annuler',
          onClick: async (e, toast) => {
            await dispatch(`post`, { homework })
            toast.goAway(0)
          },
        },
        duration: 8000,
      })
    } catch (error) {
      console.error(error)
    }
  },
 
  async patch(
    { commit, dispatch, getters },
    { uuid, modifications, force, earlyMutation }
  ) {
    earlyMutation = earlyMutation || false
    force = force || false
    if (!force) {
      let homework = getters.one(uuid)
      if ('due' in modifications && typeof modifications.due === 'number') {
        modifications.due = fromUnixTime(modifications.due)
      }
      homework = { ...homework, ...modifications }
      console.log(homework)
      const validation = getters.validate(homework)
      if (!validation.validated) return validation
    }
    try {
      if (earlyMutation) {
        commit('PATCH', { pk: uuid, modifications })
      }
 
      if (modifications.subject)
        modifications.subject = modifications.subject.uuid
      await this.$axios.patch(`/homework/${uuid}/`, modifications)
      const { data } = await this.$axios.get(`/homework/${uuid}`)
 
      if (!earlyMutation && data) {
        commit('PATCH', { pk: uuid, modifications: data })
      }
    } catch (error) {
      // eslint-disable-next-line
      console.error(error)
    }
  },
 
  async switchCompletion({ getters, dispatch }, { uuid, value }) {
    const homework = getters.one(uuid)
    let progress
    if (homework === null) {
      this.$toast.error('Erreur interne', { icon: 'error_outline' })
      return null
    }
    if (typeof value === 'number') {
      progress = value
    } else {
      // To "invert" a value in [0; 1], do y = -x + 1
      progress = -homework.progress + 1
    }
    await dispatch('patch', {
      uuid,
      modifications: { progress },
      force: true,
      earlyMutation: true,
    })
  },
}