config.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright (C) 2012-2021 Zammad Foundation, https://zammad-foundation.org/
  2. import { defineStore } from 'pinia'
  3. import { SingleValueStore, ConfigValues } from '@common/types/store'
  4. import { useApplicationConfigQuery } from '@mobile/graphql/api'
  5. import { QueryHandler } from '@common/server/apollo/handler'
  6. // TODO: maybe we can avoid the usage of unknown?
  7. const useApplicationConfigStore = defineStore('applicationConfig', {
  8. state: (): SingleValueStore<Record<string, ConfigValues>> => {
  9. return {
  10. value: {},
  11. }
  12. },
  13. getters: {
  14. get() {
  15. return (name: string): ConfigValues => this.value[name]
  16. },
  17. },
  18. actions: {
  19. async getConfig(refetchQuery = false): Promise<void> {
  20. const configQuery = new QueryHandler(useApplicationConfigQuery())
  21. // Trigger query refetch in some situtations, to skip the cache.
  22. if (refetchQuery) {
  23. configQuery.refetch()
  24. }
  25. const result = await configQuery.loadedResult()
  26. if (result?.applicationConfig) {
  27. result.applicationConfig.forEach((item) => {
  28. this.value[item.key] = item.value
  29. })
  30. }
  31. },
  32. async resetAndGetConfig(): Promise<void> {
  33. this.$reset()
  34. await this.getConfig(true)
  35. },
  36. },
  37. })
  38. export default useApplicationConfigStore