aboutsummaryrefslogtreecommitdiff
path: root/src/components/settings_modal/helpers/shared_computed_object.js
blob: 86703697d260397379cb495b29cc127e34287959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {
  instanceDefaultProperties,
  multiChoiceProperties,
  defaultState as configDefaultState
} from 'src/modules/config.js'

const SharedComputedObject = () => ({
  user () {
    return this.$store.state.users.currentUser
  },
  // Getting localized values for instance-default properties
  ...instanceDefaultProperties
    .filter(key => multiChoiceProperties.includes(key))
    .map(key => [
      key + 'DefaultValue',
      function () {
        return this.$store.getters.instanceDefaultConfig[key]
      }
    ])
    .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}),
  ...instanceDefaultProperties
    .filter(key => !multiChoiceProperties.includes(key))
    .map(key => [
      key + 'LocalizedValue',
      function () {
        return this.$t('settings.values.' + this.$store.getters.instanceDefaultConfig[key])
      }
    ])
    .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}),
  // Generating computed values for vuex properties
  ...Object.keys(configDefaultState)
    .map(key => [key, {
      get () { return this.$store.getters.mergedConfig[key] },
      set (value) {
        this.$store.dispatch('setOption', { name: key, value })
      }
    }])
    .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}),
  // Special cases (need to transform values or perform actions first)
  useStreamingApi: {
    get () { return this.$store.getters.mergedConfig.useStreamingApi },
    set (value) {
      const promise = value
        ? this.$store.dispatch('enableMastoSockets')
        : this.$store.dispatch('disableMastoSockets')

      promise.then(() => {
        this.$store.dispatch('setOption', { name: 'useStreamingApi', value })
      }).catch((e) => {
        console.error('Failed starting MastoAPI Streaming socket', e)
        this.$store.dispatch('disableMastoSockets')
        this.$store.dispatch('setOption', { name: 'useStreamingApi', value: false })
      })
    }
  }
})

export default SharedComputedObject