All files / store learndata.js

0% Statements 0/58
0% Branches 0/28
0% Functions 0/18
0% Lines 0/47

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                                                                                                                                                                                                                                                                                                           
import { firstBy } from 'thenby'
import { isBefore, parseISO } from 'date-fns'
import { getMutations, getValidator } from './index'
 
export const state = () => ({
  learndatas: [],
})
 
const parseObjectDates = object => ({
  ...object,
  modified: parseISO(object.modified),
  added: parseISO(object.added),
  opened: parseISO(object.opened),
})
 
export const getters = {
  orderBy: (state, getters, rootState) => (learndatas, whichDate) =>
    [...learndatas].sort(
      firstBy((o1, o2) => isBefore(o1[whichDate], o2[whichDate]))
        .thenBy('name')
        .thenBy('uuid')
    ),
  all: state => state.learndatas,
  one: (state, { all }, rootState) => (value, prop = 'uuid') =>
    all.find(o => o[prop] === value) || null,
  of: ({ learndatas }, getters) => (value, prop = 'note') => {
    switch (prop) {
      case 'note':
        return learndatas.filter(o =>
          // If the requested note's UUID is in
          // the array of UUIDs of notes linked to that learndata
          o.notes.map(n => n.uuid).includes(value)
        )
 
      case 'subject':
        return learndatas.filter(o => o.subject.uuid === value)
 
      default:
        // console.error(`[notes/learndatasOf] Unrecognized prop: ${prop}`)
        return []
    }
  },
  validate: getValidator({
    constraints: {
      required: ['subject', 'name', 'data'],
      maxLength: {
        300: ['name'],
      },
      maximum: {
        1: ['progress'],
      },
      minimum: {
        0: ['progress', 'test_tries', 'train_tries'],
      },
    },
    fieldNames: {
      subject: { gender: 'F', name: 'matière' },
      name: { gender: 'M', name: 'nom' },
      data: { gender: 'M', name: 'contenu' },
      progress: { gender: 'F', name: 'progression' },
      test_tries: { gender: 'M', name: 'nombre de tests' },
      train_tries: { gender: 'M', name: "nombre d'entraînements" },
    },
    resourceName: { gender: 'M', name: 'learndata' },
  }),
}
 
export const mutations = {
  ...getMutations('learndata', parseObjectDates),
}
 
export const actions = {
  async load({ commit, state }, force = false) {
    if (!force && state.learndatas.length) return
    try {
      const { data } = await this.$axios.get('/learndata/')
      if (data) commit('SET', data)
      return false
    } catch (error) {
      this.$toast.error('Erreur interne lors du chargement', {
        icon: 'error_outline',
      })
      return true
    }
  },
  async post({ commit, dispatch }, learndata, force = false) {
    if (!force) {
      const validation = await dispatch('validate', learndata)
      if (!validation.validated) return validation
    }
    try {
      const { data } = await this.$axios.post('/learndata/', learndata)
      if (data) commit('ADD', data)
      // console.log("[from API] POST /learndata/: OK")
    } catch (error) {
      // console.error("[from API] POST /learndata/: Error")
      try {
        // console.error(error.response.data)
      } catch (_) {
        // eslint-disable-next-line
        console.error(error)
      }
    }
  },
  async patch(
    { commit, dispatch, getters },
    uuid,
    modifications,
    force = false
  ) {
    if (!force) {
      let learndata = getters.one(uuid)
      learndata = { ...learndata, ...modifications }
      const validation = await dispatch('validate', learndata)
      if (!validation.validated) return validation
    }
    try {
      const { data } = await this.$axios.patch(
        `/learndata/${uuid}/`,
        modifications
      )
      if (data) commit('PATCH', uuid, data)
      // console.log("[from API] POST /learndata/: OK")
    } catch (error) {
      // console.error("[from API] POST /learndata/: Error")
      try {
        // console.error(error.response.data)
      } catch (_) {
        // eslint-disable-next-line
        console.error(error)
      }
    }
  },
  async delete({ commit }, uuid) {
    try {
      await this.$axios.delete(`/learndata/${uuid}/`)
      commit('DEL', uuid)
      // console.log(`[from API] DELETE /learndata/${uuid}/: OK`)
    } catch (error) {
      // console.error(`[from API] DELETE /learndata/${uuid}/: Error`)
      try {
        // console.error(error.response.data)
      } catch (_) {
        // eslint-disable-next-line
        console.error(error)
      }
    }
  },
}