You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.4 KiB

4 years ago
4 years ago
  1. import StatisticsService from '@/services/statistics.service'
  2. function makeData(object, label) {
  3. const labels = object ? Object.keys(object) : []
  4. const counts = object ? Object.values(object) : []
  5. return {
  6. labels,
  7. datasets: [{
  8. label,
  9. backgroundColor: '#00d1b2',
  10. data: counts
  11. }]
  12. }
  13. }
  14. export const state = () => ({
  15. loading: false,
  16. stats: {}
  17. })
  18. export const mutations = {
  19. setLoading(state, payload) {
  20. state.loading = payload
  21. },
  22. setStatistics(state, payload) {
  23. state.stats = payload
  24. }
  25. }
  26. export const getters = {
  27. progress(state) {
  28. const complete = state.stats.total - state.stats.remaining
  29. const incomplete = state.stats.remaining
  30. return {
  31. datasets: [{
  32. data: [complete, incomplete],
  33. backgroundColor: ['#00d1b2', '#ffdd57']
  34. }],
  35. labels: [
  36. 'Completed',
  37. 'Incomplete'
  38. ]
  39. }
  40. },
  41. labelStats(state) {
  42. return makeData(state.stats.label, 'Label stats')
  43. },
  44. userStats(state) {
  45. return makeData(state.stats.user, 'User stats')
  46. }
  47. }
  48. export const actions = {
  49. fetchStatistics({ commit }, payload) {
  50. commit('setLoading', true)
  51. StatisticsService.getStatistics(payload)
  52. .then((response) => {
  53. commit('setStatistics', response.data)
  54. })
  55. .catch((error) => {
  56. alert(error)
  57. })
  58. .finally(() => {
  59. commit('setLoading', false)
  60. })
  61. }
  62. }