admin.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { defineStore } from 'pinia'
  2. import gql from 'graphql-tag'
  3. import { clone, cloneDeep } from 'lodash-es'
  4. import semverGte from 'semver/functions/gte'
  5. /* global APOLLO_CLIENT */
  6. export const useAdminStore = defineStore('admin', {
  7. state: () => ({
  8. currentSiteId: null,
  9. info: {
  10. currentVersion: 'n/a',
  11. latestVersion: 'n/a',
  12. groupsTotal: 0,
  13. pagesTotal: 0,
  14. usersTotal: 0,
  15. loginsPastDay: 0,
  16. isApiEnabled: false,
  17. isMailConfigured: false,
  18. isSchedulerHealthy: false
  19. },
  20. overlay: null,
  21. overlayOpts: {},
  22. sites: [],
  23. locales: [
  24. { code: 'en', name: 'English' }
  25. ]
  26. }),
  27. getters: {
  28. isVersionLatest: (state) => {
  29. if (!state.info.currentVersion || !state.info.latestVersion || state.info.currentVersion === 'n/a' || state.info.latestVersion === 'n/a') {
  30. return false
  31. }
  32. return semverGte(state.info.currentVersion, state.info.latestVersion)
  33. }
  34. },
  35. actions: {
  36. async fetchSites () {
  37. const resp = await APOLLO_CLIENT.query({
  38. query: gql`
  39. query getSites {
  40. sites {
  41. id
  42. hostname
  43. isEnabled
  44. title
  45. }
  46. }
  47. `,
  48. fetchPolicy: 'network-only'
  49. })
  50. this.sites = cloneDeep(resp?.data?.sites ?? [])
  51. if (!this.currentSiteId) {
  52. this.currentSiteId = this.sites[0].id
  53. }
  54. },
  55. async fetchInfo () {
  56. const resp = await APOLLO_CLIENT.query({
  57. query: gql`
  58. query getAdminInfo {
  59. apiState
  60. systemInfo {
  61. groupsTotal
  62. usersTotal
  63. currentVersion
  64. latestVersion
  65. isMailConfigured
  66. isSchedulerHealthy
  67. }
  68. }
  69. `,
  70. fetchPolicy: 'network-only'
  71. })
  72. this.info.groupsTotal = clone(resp?.data?.systemInfo?.groupsTotal ?? 0)
  73. this.info.usersTotal = clone(resp?.data?.systemInfo?.usersTotal ?? 0)
  74. this.info.currentVersion = clone(resp?.data?.systemInfo?.currentVersion ?? 'n/a')
  75. this.info.latestVersion = clone(resp?.data?.systemInfo?.latestVersion ?? 'n/a')
  76. this.info.isApiEnabled = clone(resp?.data?.apiState ?? false)
  77. this.info.isMailConfigured = clone(resp?.data?.systemInfo?.isMailConfigured ?? false)
  78. this.info.isSchedulerHealthy = clone(resp?.data?.systemInfo?.isSchedulerHealthy ?? false)
  79. }
  80. }
  81. })