summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/static_site_editor/components/edit_meta_modal.vue
blob: 8c3ee7b96091fabd79ab1b9646a028865b8f3c1a (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<script>
import { GlModal } from '@gitlab/ui';
import Api from '~/api';
import { __, s__, sprintf } from '~/locale';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';

import { ISSUABLE_TYPE, MR_META_LOCAL_STORAGE_KEY } from '../constants';
import EditMetaControls from './edit_meta_controls.vue';

export default {
  components: {
    GlModal,
    EditMetaControls,
    LocalStorageSync,
  },
  props: {
    sourcePath: {
      type: String,
      required: true,
    },
    namespace: {
      type: String,
      required: true,
    },
    project: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      clearStorage: false,
      currentTemplate: null,
      mergeRequestTemplates: null,
      mergeRequestMeta: {
        title: sprintf(s__(`StaticSiteEditor|Update %{sourcePath} file`), {
          sourcePath: this.sourcePath,
        }),
        description: s__('StaticSiteEditor|Copy update'),
      },
    };
  },
  computed: {
    disabled() {
      return this.mergeRequestMeta.title === '';
    },
    primaryProps() {
      return {
        text: __('Submit changes'),
        attributes: [{ variant: 'success' }, { disabled: this.disabled }],
      };
    },
    secondaryProps() {
      return {
        text: __('Keep editing'),
        attributes: [{ variant: 'default' }],
      };
    },
  },
  mounted() {
    this.initTemplates();
  },
  methods: {
    hide() {
      this.$refs.modal.hide();
    },
    initTemplates() {
      const { namespace, project } = this;
      Api.issueTemplates(namespace, project, ISSUABLE_TYPE, (err, templates) => {
        if (err) return; // Error handled by global AJAX error handler
        this.mergeRequestTemplates = templates;
      });
    },
    show() {
      this.$refs.modal.show();
    },
    onPrimary() {
      this.$emit('primary', this.mergeRequestMeta);
      this.clearStorage = true;
    },
    onSecondary() {
      this.hide();
    },
    onChangeTemplate(template) {
      this.currentTemplate = template;

      const description = this.currentTemplate ? this.currentTemplate.content : '';
      const mergeRequestMeta = { ...this.mergeRequestMeta, description };
      this.onUpdateSettings(mergeRequestMeta);
    },
    onUpdateSettings(mergeRequestMeta) {
      this.mergeRequestMeta = { ...mergeRequestMeta };
    },
  },
  storageKey: MR_META_LOCAL_STORAGE_KEY,
};
</script>

<template>
  <gl-modal
    ref="modal"
    modal-id="edit-meta-modal"
    :title="__('Submit your changes')"
    :action-primary="primaryProps"
    :action-secondary="secondaryProps"
    size="sm"
    @primary="onPrimary"
    @secondary="onSecondary"
    @hide="() => $emit('hide')"
  >
    <local-storage-sync
      v-model="mergeRequestMeta"
      :storage-key="$options.storageKey"
      :clear="clearStorage"
      as-json
    />
    <edit-meta-controls
      ref="editMetaControls"
      :title="mergeRequestMeta.title"
      :description="mergeRequestMeta.description"
      :templates="mergeRequestTemplates"
      :current-template="currentTemplate"
      @updateSettings="onUpdateSettings"
      @changeTemplate="onChangeTemplate"
    />
  </gl-modal>
</template>