valasek/timesheet

View on GitHub
client/src/store/modules/rates.js

Summary

Maintainability
C
1 day
Test Coverage
// Copyright © 2018-2020 Stanislav Valasek <valasek@gmail.com>

import api from '../../api/axiosSettings'
import { Notify } from 'quasar'

// initial state
const state = {
  // rates: [ '' ], // array of rates:string
  types: [ '' ], // array of rateType:string
  all: [ {} ] // id: '', name: '', type: ''
}

const getters = {}

const actions = {

  getRates ({ commit }) {
    api.apiClient.get(`/api/rates`, { crossDomain: true })
      .then(response => {
        commit('SET_RATES', response.data)
      })
      .catch(e => {
        Notify.create({
          message: 'Couldn\'t read rates from server. \n' + e.toString(),
          color: 'negative',
          icon: 'report_problem'
        })
        console.log(e) /* eslint-disable-line no-console */
      })
  }

}

const mutations = {

  SET_RATES (state, rates) {
    state.all = rates.sort(function (a, b) {
      var nameA = a.name.toUpperCase()
      var nameB = b.name.toUpperCase()
      if (nameA < nameB) {
        return -1
      }
      if (nameA > nameB) {
        return 1
      }
      return 0
    })
    state.types = [...new Set(rates.map(function (obj) {
      return obj.type
    }))]
  }

}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}