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.

70 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. return (labels) => {
  29. const complete = state.stats.total - state.stats.remaining
  30. const incomplete = state.stats.remaining
  31. return {
  32. datasets: [{
  33. data: [complete, incomplete],
  34. backgroundColor: ['#00d1b2', '#ffdd57']
  35. }],
  36. labels
  37. }
  38. }
  39. },
  40. labelStats(state) {
  41. return (label) => {
  42. return makeData(state.stats.label, label)
  43. }
  44. },
  45. userStats(state) {
  46. return (label) => {
  47. return makeData(state.stats.user, label)
  48. }
  49. }
  50. }
  51. export const actions = {
  52. fetchStatistics({ commit }, payload) {
  53. commit('setLoading', true)
  54. StatisticsService.getStatistics(payload)
  55. .then((response) => {
  56. commit('setStatistics', response.data)
  57. })
  58. .catch((error) => {
  59. alert(error)
  60. })
  61. .finally(() => {
  62. commit('setLoading', false)
  63. })
  64. }
  65. }