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
|
<script>
import { GlButton, GlForm, GlFormGroup, GlFormInput, GlAlert } from '@gitlab/ui';
import { helpPagePath } from '~/helpers/help_page_helper';
import { logError } from '~/lib/logger';
import { __ } from '~/locale';
import createCustomEmojiMutation from '../queries/create_custom_emoji.mutation.graphql';
export default {
name: 'CustomEmojiForm',
components: {
GlButton,
GlForm,
GlFormGroup,
GlFormInput,
GlAlert,
},
inject: {
groupPath: {
default: '',
},
},
data() {
return {
errors: [],
saving: false,
showValidation: false,
updateCustomEmoji: {
name: '',
url: '',
},
};
},
computed: {
isNameValid() {
if (this.showValidation) return Boolean(this.updateCustomEmoji.name);
return true;
},
isUrlValid() {
if (this.showValidation) return Boolean(this.updateCustomEmoji.url);
return true;
},
isValid() {
return this.isNameValid && this.isUrlValid;
},
},
methods: {
onSubmit() {
this.showValidation = true;
if (!this.isValid) return;
this.errors = [];
this.saving = true;
this.$apollo
.mutate({
mutation: createCustomEmojiMutation,
variables: {
groupPath: this.groupPath,
name: this.updateCustomEmoji.name,
url: this.updateCustomEmoji.url,
},
update: (store, { data: { createCustomEmoji } }) => {
if (createCustomEmoji.errors.length) {
this.errors = createCustomEmoji.errors.map((e) => e);
} else {
this.$emit('saved');
this.updateCustomEmoji = { name: '', url: '' };
this.showValidation = false;
}
},
})
.catch((error) => {
const errors = error.graphQLErrors;
if (errors?.length) {
this.errors = errors.map((e) => e.message);
} else {
// eslint-disable-next-line @gitlab/require-i18n-strings
logError('Unexpected error while saving emoji', error);
this.errors = [__('An unexpected error occurred. Please try again.')];
}
})
.finally(() => {
this.saving = false;
});
},
},
restrictedToolbarItems: ['full-screen'],
markdownDocsPath: helpPagePath('user/markdown'),
};
</script>
<template>
<gl-form class="gl-mb-6" data-testid="custom-emoji-form" @submit.prevent="onSubmit">
<gl-alert
v-for="error in errors"
:key="error"
variant="danger"
class="gl-mb-3"
:dismissible="false"
>
{{ error }}
</gl-alert>
<gl-form-group
:label="__('Name')"
:state="isNameValid"
:invalid-feedback="__('Please enter a name for the custom emoji.')"
data-testid="custom-emoji-name-form-group"
>
<gl-form-input
v-model="updateCustomEmoji.name"
:placeholder="__('eg party_tanuki')"
data-testid="custom-emoji-name-input"
/>
</gl-form-group>
<gl-form-group
:label="__('URL')"
:state="isUrlValid"
:invalid-feedback="__('Please enter a URL for the custom emoji.')"
data-testid="custom-emoji-url-form-group"
>
<gl-form-input
v-model="updateCustomEmoji.url"
:placeholder="__('Enter a URL for your custom emoji')"
data-testid="custom-emoji-url-input"
/>
</gl-form-group>
<gl-button
variant="confirm"
class="gl-mr-3 js-no-auto-disable"
type="submit"
:loading="saving"
data-testid="custom-emoji-form-submit-btn"
>
{{ __('Save') }}
</gl-button>
<gl-button :to="{ path: '/' }">{{ __('Cancel') }}</gl-button>
</gl-form>
</template>
|