summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/gl_modal_vuex.vue
blob: dd0c0358ef627f314b35866a6ae9702a7c31276b (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<script>
import { GlModal } from '@gitlab/ui';
import { mapState, mapActions } from 'vuex';
import { BV_SHOW_MODAL, BV_HIDE_MODAL } from '~/lib/utils/constants';

/**
 * This component keeps the GlModal's visibility in sync with the given vuex module.
 */
export default {
  components: {
    GlModal,
  },
  props: {
    modalId: {
      type: String,
      required: true,
    },
    modalModule: {
      type: String,
      required: true,
    },
  },
  computed: {
    ...mapState({
      isVisible(state) {
        return state[this.modalModule].isVisible;
      },
    }),
    attrs() {
      const { modalId, modalModule, ...attrs } = this.$attrs;

      return attrs;
    },
  },
  watch: {
    isVisible(val) {
      return val ? this.bsShow() : this.bsHide();
    },
  },
  methods: {
    ...mapActions({
      syncShow(dispatch) {
        return dispatch(`${this.modalModule}/show`);
      },
      syncHide(dispatch) {
        return dispatch(`${this.modalModule}/hide`);
      },
    }),
    bsShow() {
      this.$root.$emit(BV_SHOW_MODAL, this.modalId);
    },
    bsHide() {
      // $root.$emit is a workaround because other b-modal approaches don't work yet with gl-modal
      this.$root.$emit(BV_HIDE_MODAL, this.modalId);
    },
    cancel() {
      this.$emit('cancel');
      this.syncHide();
    },
    ok() {
      this.$emit('ok');
      this.syncHide();
    },
  },
};
</script>

<template>
  <gl-modal
    v-bind="attrs"
    :modal-id="modalId"
    v-on="$listeners"
    @shown="syncShow"
    @hidden="syncHide"
  >
    <slot></slot>
    <template #modal-footer>
      <slot name="modal-footer" :ok="ok" :cancel="cancel"></slot>
    </template>
  </gl-modal>
</template>