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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
<script>
import {
GlButton,
GlBroadcastMessage,
GlForm,
GlFormGroup,
GlFormCheckbox,
GlFormCheckboxGroup,
GlFormInput,
GlFormSelect,
GlFormText,
GlFormTextarea,
} from '@gitlab/ui';
import axios from '~/lib/utils/axios_utils';
import { s__ } from '~/locale';
import { createAlert, VARIANT_DANGER } from '~/alert';
import { redirectTo } from '~/lib/utils/url_utility';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
import SafeHtml from '~/vue_shared/directives/safe_html';
import { THEMES, TYPES, TYPE_BANNER } from '../constants';
import DatetimePicker from './datetime_picker.vue';
const FORM_HEADERS = { headers: { 'Content-Type': 'application/json; charset=utf-8' } };
export default {
DEFAULT_DEBOUNCE_AND_THROTTLE_MS,
name: 'MessageForm',
components: {
DatetimePicker,
GlButton,
GlBroadcastMessage,
GlForm,
GlFormGroup,
GlFormCheckbox,
GlFormCheckboxGroup,
GlFormInput,
GlFormSelect,
GlFormText,
GlFormTextarea,
},
directives: {
SafeHtml,
},
mixins: [glFeatureFlagsMixin()],
inject: {
targetAccessLevelOptions: {
default: [[]],
},
messagesPath: {
default: '',
},
previewPath: {
default: '',
},
},
i18n: {
message: s__('BroadcastMessages|Message'),
messagePlaceholder: s__('BroadcastMessages|Your message here'),
type: s__('BroadcastMessages|Type'),
theme: s__('BroadcastMessages|Theme'),
dismissable: s__('BroadcastMessages|Dismissable'),
dismissableDescription: s__('BroadcastMessages|Allow users to dismiss the broadcast message'),
targetRoles: s__('BroadcastMessages|Target roles'),
targetRolesDescription: s__(
'BroadcastMessages|The broadcast message displays only to users in projects and groups who have these roles.',
),
targetPath: s__('BroadcastMessages|Target Path'),
targetPathDescription: s__('BroadcastMessages|Paths can contain wildcards, like */welcome'),
startsAt: s__('BroadcastMessages|Starts at'),
endsAt: s__('BroadcastMessages|Ends at'),
add: s__('BroadcastMessages|Add broadcast message'),
addError: s__('BroadcastMessages|There was an error adding broadcast message.'),
update: s__('BroadcastMessages|Update broadcast message'),
updateError: s__('BroadcastMessages|There was an error updating broadcast message.'),
},
messageThemes: THEMES,
messageTypes: TYPES,
props: {
broadcastMessage: {
type: Object,
required: true,
},
},
data() {
return {
loading: false,
message: this.broadcastMessage.message,
type: this.broadcastMessage.broadcastType,
theme: this.broadcastMessage.theme,
dismissable: this.broadcastMessage.dismissable || false,
targetPath: this.broadcastMessage.targetPath,
targetAccessLevels: this.broadcastMessage.targetAccessLevels,
targetAccessLevelOptions: this.targetAccessLevelOptions.map(([text, value]) => ({
text,
value,
})),
startsAt: new Date(this.broadcastMessage.startsAt.getTime()),
endsAt: new Date(this.broadcastMessage.endsAt.getTime()),
renderedMessage: '',
};
},
computed: {
isBanner() {
return this.type === TYPE_BANNER;
},
messageBlank() {
return this.message.trim() === '';
},
messagePreview() {
return this.messageBlank ? this.$options.i18n.messagePlaceholder : this.renderedMessage;
},
isAddForm() {
return !this.broadcastMessage.id;
},
formPath() {
return this.isAddForm
? this.messagesPath
: `${this.messagesPath}/${this.broadcastMessage.id}`;
},
formPayload() {
return JSON.stringify({
message: this.message,
broadcast_type: this.type,
theme: this.theme,
dismissable: this.dismissable,
target_path: this.targetPath,
target_access_levels: this.targetAccessLevels,
starts_at: this.startsAt.toISOString(),
ends_at: this.endsAt.toISOString(),
});
},
},
watch: {
message() {
this.renderPreview();
},
},
methods: {
async onSubmit() {
this.loading = true;
const success = await this.submitForm();
if (success) {
redirectTo(this.messagesPath);
} else {
this.loading = false;
}
},
async submitForm() {
const requestMethod = this.isAddForm ? 'post' : 'patch';
try {
await axios[requestMethod](this.formPath, this.formPayload, FORM_HEADERS);
} catch (e) {
const message = this.isAddForm
? this.$options.i18n.addError
: this.$options.i18n.updateError;
createAlert({ message, variant: VARIANT_DANGER });
return false;
}
return true;
},
async renderPreview() {
try {
const res = await axios.post(this.previewPath, this.formPayload, FORM_HEADERS);
this.renderedMessage = res.data;
} catch (e) {
this.renderedMessage = '';
}
},
},
safeHtmlConfig: {
ADD_TAGS: ['use'],
},
};
</script>
<template>
<gl-form @submit.prevent="onSubmit">
<gl-broadcast-message
class="gl-my-6"
:type="type"
:theme="theme"
:dismissible="dismissable"
data-testid="preview-broadcast-message"
>
<div v-safe-html:[$options.safeHtmlConfig]="messagePreview"></div>
</gl-broadcast-message>
<gl-form-group :label="$options.i18n.message" label-for="message-textarea">
<gl-form-textarea
id="message-textarea"
v-model="message"
size="sm"
:debounce="$options.DEFAULT_DEBOUNCE_AND_THROTTLE_MS"
:placeholder="$options.i18n.messagePlaceholder"
data-testid="message-input"
/>
</gl-form-group>
<gl-form-group :label="$options.i18n.type" label-for="type-select">
<gl-form-select id="type-select" v-model="type" :options="$options.messageTypes" />
</gl-form-group>
<template v-if="isBanner">
<gl-form-group :label="$options.i18n.theme" label-for="theme-select">
<gl-form-select
id="theme-select"
v-model="theme"
:options="$options.messageThemes"
data-testid="theme-select"
/>
</gl-form-group>
<gl-form-group :label="$options.i18n.dismissable" label-for="dismissable-checkbox">
<gl-form-checkbox
id="dismissable-checkbox"
v-model="dismissable"
class="gl-mt-3"
data-testid="dismissable-checkbox"
>
<span>{{ $options.i18n.dismissableDescription }}</span>
</gl-form-checkbox>
</gl-form-group>
</template>
<gl-form-group
v-if="glFeatures.roleTargetedBroadcastMessages"
:label="$options.i18n.targetRoles"
data-testid="target-roles-checkboxes"
>
<gl-form-checkbox-group v-model="targetAccessLevels" :options="targetAccessLevelOptions" />
<gl-form-text>
{{ $options.i18n.targetRolesDescription }}
</gl-form-text>
</gl-form-group>
<gl-form-group :label="$options.i18n.targetPath" label-for="target-path-input">
<gl-form-input id="target-path-input" v-model="targetPath" />
<gl-form-text>
{{ $options.i18n.targetPathDescription }}
</gl-form-text>
</gl-form-group>
<gl-form-group :label="$options.i18n.startsAt">
<datetime-picker v-model="startsAt" />
</gl-form-group>
<gl-form-group :label="$options.i18n.endsAt">
<datetime-picker v-model="endsAt" />
</gl-form-group>
<div class="gl-my-5">
<gl-button
type="submit"
variant="confirm"
:loading="loading"
:disabled="messageBlank"
data-testid="submit-button"
>
{{ isAddForm ? $options.i18n.add : $options.i18n.update }}
</gl-button>
</div>
</gl-form>
</template>
|