summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/gl_toggle_vuex.vue
blob: b649dac029a3958ad672a0bed6e7286a2929ed39 (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
<script>
import { GlToggle } from '@gitlab/ui';
import { capitalizeFirstCharacter } from '~/lib/utils/text_utility';

export default {
  name: 'GlToggleVuex',
  components: {
    GlToggle,
  },
  props: {
    stateProperty: {
      type: String,
      required: true,
    },
    storeModule: {
      type: String,
      required: false,
      default: null,
    },
    setAction: {
      type: String,
      required: false,
      default() {
        return `set${capitalizeFirstCharacter(this.stateProperty)}`;
      },
    },
  },
  computed: {
    value: {
      get() {
        const { state } = this.$store;
        const { stateProperty, storeModule } = this;
        return storeModule ? state[storeModule][stateProperty] : state[stateProperty];
      },
      set(value) {
        const { stateProperty, storeModule, setAction } = this;
        const action = storeModule ? `${storeModule}/${setAction}` : setAction;
        this.$store.dispatch(action, { key: stateProperty, value });
      },
    },
  },
};
</script>

<template>
  <gl-toggle v-model="value">
    <slot v-bind="{ value }"></slot>
  </gl-toggle>
</template>