summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/admin/users/components/modals/user_modal_manager.vue
blob: 1dfea3f1e7bb7b29457fa8a1d0769b3898ce72c6 (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
<script>
import DeleteUserModal from './delete_user_modal.vue';

export default {
  components: { DeleteUserModal },
  props: {
    modalConfiguration: {
      required: true,
      type: Object,
    },
    csrfToken: {
      required: true,
      type: String,
    },
    selector: {
      required: true,
      type: String,
    },
  },
  data() {
    return {
      currentModalData: null,
    };
  },
  computed: {
    activeModal() {
      return Boolean(this.currentModalData);
    },

    modalProps() {
      const { glModalAction: requestedAction } = this.currentModalData;
      return {
        ...this.modalConfiguration[requestedAction],
        ...this.currentModalData,
        csrfToken: this.csrfToken,
      };
    },
  },

  mounted() {
    /*
     * Here we're looking for every button that needs to launch a modal
     * on click, and then attaching a click event handler to show the modal
     * if it's correctly configured.
     *
     * TODO: Replace this with integrated modal components https://gitlab.com/gitlab-org/gitlab/-/issues/320922
     */
    document.querySelectorAll(this.selector).forEach((button) => {
      button.addEventListener('click', (e) => {
        if (!button.dataset.glModalAction) return;

        e.preventDefault();
        this.show(button.dataset);
      });
    });
  },

  methods: {
    show(modalData) {
      const { glModalAction: requestedAction } = modalData;

      if (!this.modalConfiguration[requestedAction]) {
        throw new Error(`Modal action ${requestedAction} has no configuration in HTML`);
      }

      this.currentModalData = modalData;

      return this.$nextTick().then(() => {
        this.$refs.modal.show();
      });
    },
  },
};
</script>
<template>
  <delete-user-modal v-if="activeModal" ref="modal" v-bind="modalProps" />
</template>