debian-mirror-gitlab/app/assets/javascripts/notifications/components/custom_notifications_modal.vue

144 lines
3.6 KiB
Vue
Raw Normal View History

2021-03-11 19:13:27 +05:30
<script>
import { GlModal, GlSprintf, GlLink, GlLoadingIcon, GlFormGroup, GlFormCheckbox } from '@gitlab/ui';
import Api from '~/api';
import { i18n } from '../constants';
export default {
name: 'CustomNotificationsModal',
components: {
GlModal,
GlSprintf,
GlLink,
GlLoadingIcon,
GlFormGroup,
GlFormCheckbox,
},
inject: {
projectId: {
default: null,
},
groupId: {
default: null,
},
helpPagePath: {
default: '',
},
},
2021-04-17 20:07:23 +05:30
model: {
prop: 'visible',
event: 'change',
},
2021-03-11 19:13:27 +05:30
props: {
modalId: {
type: String,
required: false,
default: 'custom-notifications-modal',
},
2021-04-17 20:07:23 +05:30
visible: {
type: Boolean,
required: false,
default: false,
},
2021-03-11 19:13:27 +05:30
},
data() {
return {
isLoading: false,
events: [],
};
},
methods: {
open() {
this.$refs.modal.show();
},
buildEvents(events) {
return Object.keys(events).map((key) => ({
id: key,
enabled: Boolean(events[key]),
name: this.$options.i18n.eventNames[key] || '',
loading: false,
}));
},
async onOpen() {
if (!this.events.length) {
await this.loadNotificationSettings();
}
},
async loadNotificationSettings() {
this.isLoading = true;
try {
const {
data: { events },
} = await Api.getNotificationSettings(this.projectId, this.groupId);
this.events = this.buildEvents(events);
} catch (error) {
2021-09-30 23:02:18 +05:30
this.$toast.show(this.$options.i18n.loadNotificationLevelErrorMessage);
2021-03-11 19:13:27 +05:30
} finally {
this.isLoading = false;
}
},
async updateEvent(isEnabled, event) {
const index = this.events.findIndex((e) => e.id === event.id);
// update loading state for the given event
this.$set(this.events, index, { ...this.events[index], loading: true });
try {
const {
data: { events },
} = await Api.updateNotificationSettings(this.projectId, this.groupId, {
[event.id]: isEnabled,
});
this.events = this.buildEvents(events);
} catch (error) {
2021-09-30 23:02:18 +05:30
this.$toast.show(this.$options.i18n.updateNotificationLevelErrorMessage);
2021-03-11 19:13:27 +05:30
}
},
},
i18n,
};
</script>
<template>
<gl-modal
ref="modal"
2021-04-17 20:07:23 +05:30
:visible="visible"
2021-03-11 19:13:27 +05:30
:modal-id="modalId"
:title="$options.i18n.customNotificationsModal.title"
@show="onOpen"
2021-04-17 20:07:23 +05:30
v-on="$listeners"
2021-03-11 19:13:27 +05:30
>
<div class="container-fluid">
<div class="row">
<div class="col-lg-4">
<h4 class="gl-mt-0" data-testid="modalBodyTitle">
{{ $options.i18n.customNotificationsModal.bodyTitle }}
</h4>
<gl-sprintf :message="$options.i18n.customNotificationsModal.bodyMessage">
<template #notificationLink="{ content }">
<gl-link :href="helpPagePath" target="_blank">{{ content }}</gl-link>
</template>
</gl-sprintf>
</div>
<div class="col-lg-8">
<gl-loading-icon v-if="isLoading" size="lg" class="gl-mt-3" />
<template v-else>
<gl-form-group v-for="event in events" :key="event.id">
2021-04-17 20:07:23 +05:30
<gl-form-checkbox
v-model="event.enabled"
:data-testid="`notification-setting-${event.id}`"
@change="updateEvent($event, event)"
>
2021-03-11 19:13:27 +05:30
<strong>{{ event.name }}</strong
2021-09-30 23:02:18 +05:30
><gl-loading-icon v-if="event.loading" size="sm" :inline="true" class="gl-ml-2" />
2021-03-11 19:13:27 +05:30
</gl-form-checkbox>
</gl-form-group>
</template>
</div>
</div>
</div>
</gl-modal>
</template>