All files / store reports.js

0% Statements 0/49
0% Branches 0/25
0% Functions 0/17
0% Lines 0/40

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                                                                                                                                                                                               
import { formatISO, isAfter, parseISO } from 'date-fns'
import { firstBy } from 'thenby'
import { getValidator, getMutations } from './index'
const UAParser = require('ua-parser-js')
 
export const parseReportDates = report => ({
  ...report,
  published: report.published ? parseISO(report.published) : null,
  added: report.added ? parseISO(report.added) : null,
})
 
export const state = () => ({
  reports: [],
  loaded: false,
})
 
export const getters = {
  order: () => (reports, by = 'date') => {
    if (['published', 'added'].includes(by)) {
      return reports.sort(firstBy((o1, o2) => isAfter(o1[by], o2[by])))
    } else {
      console.warn(`=reports: ordering by "${by}" is not supported`)
      return reports
    }
  },
  all: ({ reports }, { order }) => order(reports),
  one: ({ reports }) => (what = 'uuid', value) =>
    reports.find(o => o[what] === value),
  latest: ({ reports }) =>
    reports.length > 0 ? [...reports].sort(firstBy('added'))[0] : null,
  resolved: ({ reports }) => reports.filter(o => o.resolved),
  unresolved: ({ reports }) => reports.filter(o => !o.resolved),
  validate: getValidator({
    fieldNames: {
      type: { gender: 'M', name: 'type de demande' },
      title: { gender: 'M', name: 'titre' },
      content: { gender: 'M', name: 'message' },
      happened: { gender: 'M', name: 'date' },
    },
    resourceName: { gender: 'M', name: 'Rapport' },
    constraints: {
      notEmpty: ['title', 'type', 'content'],
    },
    debug: true,
  }),
}
 
export const mutations = {
  ...getMutations('report', parseReportDates),
  POSTLOAD: state => (state.loaded = true),
}
 
export const actions = {
  async load({ commit }, force = false) {
    if (!force && state.loaded) return
    try {
      const { data } = await this.$axios.get(`/reports/`)
      if (data) {
        commit('SET', data)
        commit('POSTLOAD')
      }
    } catch (error) {
      this.$toast.error('Impossible de charger les rapports', {
        icon: 'error_outline',
      })
      console.error(error)
    }
  },
  async post({ getters, commit, state }, { report, force }) {
    force = force || false
    const { browser, cpu, os } = UAParser(window.userAgent)
    if ('happened' in report) report.happened = formatISO(report.happened)
    report = {
      user: this.$auth.user.id,
      browser: `${browser.name}/${browser.version}`,
      os: `${cpu.architecture}/${os.name}/${os.version}`,
      language: 'fr',
      ...report,
    }
    if (!force) {
      const validation = getters.validate(report)
      if (!validation.validated) return false
    }
    try {
      const res = await this.$axios.post('reports/', report)
      const { data } = await this.$axios.get(`/reports/${res.data.uuid}/`)
      commit('ADD', data)
      return true
    } catch (error) {
      // eslint-disable-next-line
      console.error(error)
      return false
    }
  },
}