summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/confirm_modal.vue
blob: 72504e5bc503968a95083c3e34653b2b5c38a80b (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<script>
import { GlModal, GlSafeHtmlDirective as SafeHtml } from '@gitlab/ui';
import { uniqueId } from 'lodash';
import csrf from '~/lib/utils/csrf';
import eventHub, { EVENT_OPEN_CONFIRM_MODAL } from './confirm_modal_eventhub';
import DomElementListener from './dom_element_listener.vue';

export default {
  components: {
    GlModal,
    DomElementListener,
  },
  directives: {
    SafeHtml,
  },
  props: {
    selector: {
      type: String,
      required: true,
    },
    handleSubmit: {
      type: Function,
      required: false,
      default: null,
    },
  },
  data() {
    return {
      modalId: uniqueId('confirm-modal-'),
      path: '',
      method: '',
      modalAttributes: {},
    };
  },
  mounted() {
    eventHub.$on(EVENT_OPEN_CONFIRM_MODAL, this.onOpenEvent);
  },
  destroyed() {
    eventHub.$off(EVENT_OPEN_CONFIRM_MODAL, this.onOpenEvent);
  },
  methods: {
    onButtonPress(e) {
      const element = e.currentTarget;

      if (!element.dataset.path) {
        return;
      }

      const modalAttributes = element.dataset.modalAttributes
        ? JSON.parse(element.dataset.modalAttributes)
        : {};

      this.onOpenEvent({
        path: element.dataset.path,
        method: element.dataset.method,
        modalAttributes,
      });
    },
    onOpenEvent({ path, method, modalAttributes }) {
      this.path = path;
      this.method = method;
      this.modalAttributes = modalAttributes;
      this.openModal();
    },
    openModal() {
      this.$refs.modal.show();
    },
    closeModal() {
      this.$refs.modal.hide();
    },
    submitModal() {
      if (this.handleSubmit) {
        this.handleSubmit(this.path);
      } else {
        this.$refs.form.submit();
      }
    },
  },
  csrf,
};
</script>

<template>
  <dom-element-listener :selector="selector" @click.prevent="onButtonPress">
    <gl-modal
      ref="modal"
      :modal-id="modalId"
      v-bind="modalAttributes"
      @primary="submitModal"
      @cancel="closeModal"
    >
      <form ref="form" :action="path" method="post">
        <!-- Rails workaround for <form method="delete" />
      https://github.com/rails/rails/blob/master/actionview/app/assets/javascripts/rails-ujs/features/method.coffee
      -->
        <input type="hidden" name="_method" :value="method" />
        <input type="hidden" name="authenticity_token" :value="$options.csrf.token" />
        <div v-if="modalAttributes.messageHtml" v-safe-html="modalAttributes.messageHtml"></div>
        <div v-else>{{ modalAttributes.message }}</div>
      </form>
    </gl-modal>
  </dom-element-listener>
</template>