summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/monitoring/components/duplicate_dashboard_form.vue
blob: 1765a2f3d5d61dd6d14878983e29c336a1c16e39 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<script>
import { GlFormGroup, GlFormInput, GlFormRadioGroup, GlFormTextarea } from '@gitlab/ui';
import { escape as esc } from 'lodash';
import { __, s__, sprintf } from '~/locale';

const defaultFileName = (dashboard) => dashboard.path.split('/').reverse()[0];

export default {
  components: {
    GlFormGroup,
    GlFormInput,
    GlFormRadioGroup,
    GlFormTextarea,
  },
  props: {
    dashboard: {
      type: Object,
      required: true,
    },
    defaultBranch: {
      type: String,
      required: true,
    },
  },
  radioVals: {
    /* Use the default branch (e.g. main) */
    DEFAULT: 'DEFAULT',
    /* Create a new branch */
    NEW: 'NEW',
  },
  data() {
    return {
      form: {
        dashboard: this.dashboard.path,
        fileName: defaultFileName(this.dashboard),
        commitMessage: '',
      },
      branchName: '',
      branchOption: this.$options.radioVals.NEW,
      branchOptions: [
        {
          value: this.$options.radioVals.DEFAULT,
          html: sprintf(
            __('Commit to %{branchName} branch'),
            {
              branchName: `<strong>${esc(this.defaultBranch)}</strong>`,
            },
            false,
          ),
        },
        { value: this.$options.radioVals.NEW, text: __('Create new branch') },
      ],
    };
  },
  computed: {
    defaultCommitMsg() {
      return sprintf(s__('Metrics|Create custom dashboard %{fileName}'), {
        fileName: this.form.fileName,
      });
    },
    fileNameState() {
      // valid if empty or *.yml
      return !(this.form.fileName && !this.form.fileName.endsWith('.yml'));
    },
    fileNameFeedback() {
      return !this.fileNameState ? s__('The file name should have a .yml extension') : '';
    },
  },
  mounted() {
    this.change();
  },
  methods: {
    change() {
      this.$emit('change', {
        ...this.form,
        commitMessage: this.form.commitMessage || this.defaultCommitMsg,
        branch:
          this.branchOption === this.$options.radioVals.NEW ? this.branchName : this.defaultBranch,
      });
    },
    focus(option) {
      if (option === this.$options.radioVals.NEW) {
        this.$nextTick(() => {
          this.$refs.branchName.$el.focus();
        });
      }
    },
  },
};
</script>
<template>
  <form @change="change">
    <p class="text-muted">
      {{
        s__(`Metrics|You can save a copy of this dashboard to your repository
      so it can be customized. Select a file name and branch to save it.`)
      }}
    </p>
    <gl-form-group
      ref="fileNameFormGroup"
      :label="__('File name')"
      :state="fileNameState"
      :invalid-feedback="fileNameFeedback"
      label-size="sm"
      label-for="fileName"
    >
      <gl-form-input
        id="fileName"
        ref="fileName"
        v-model="form.fileName"
        data-qa-selector="duplicate_dashboard_filename_field"
        :required="true"
      />
    </gl-form-group>
    <gl-form-group :label="__('Branch')" label-size="sm" label-for="branch">
      <gl-form-radio-group
        ref="branchOption"
        v-model="branchOption"
        :checked="$options.radioVals.NEW"
        :stacked="true"
        :options="branchOptions"
        @change="focus"
      />
      <gl-form-input
        v-show="branchOption === $options.radioVals.NEW"
        id="branchName"
        ref="branchName"
        v-model="branchName"
      />
    </gl-form-group>
    <gl-form-group
      :label="__('Commit message (optional)')"
      label-size="sm"
      label-for="commitMessage"
    >
      <gl-form-textarea
        id="commitMessage"
        ref="commitMessage"
        v-model="form.commitMessage"
        :placeholder="defaultCommitMsg"
      />
    </gl-form-group>
  </form>
</template>