main
vue 149 lines 3.88 KB
Raw
1 <template>
2 <div class="customer-notifications-workflows-form flex grow flex-col justify-between">
3 <div class="form-box">
4 <n-spin v-model:show="loading">
5 <n-form ref="formRef" :label-width="80" :model="form" :rules>
6 <n-form-item label="Enabled" path="enabled">
7 <n-switch v-model:value="form.enabled" />
8 </n-form-item>
9 <n-form-item label="Shuffle Workflow Id" path="shuffle_workflow_id">
10 <n-input
11 v-model:value.trim="form.shuffle_workflow_id"
12 placeholder="Input the Shuffle Workflow Id..."
13 clearable
14 />
15 </n-form-item>
16 </n-form>
17 </n-spin>
18 </div>
19 <div class="buttons-box flex justify-between gap-3">
20 <div class="flex gap-3">
21 <slot name="additionalActions" :loading></slot>
22 </div>
23 <div class="flex gap-3">
24 <n-button secondary :disabled="loading" @click="reset()">Reset</n-button>
25 <n-button type="primary" :disabled="!isValid" :loading @click="validate()">Submit</n-button>
26 </div>
27 </div>
28 </div>
29 </template>
30
31 <script setup lang="ts">
32 import type { FormInst, FormRules, FormValidationError } from "naive-ui"
33 import type { IncidentNotificationPayload } from "@/api/endpoints/incidentManagement/notification"
34 import type { IncidentNotification } from "@/types/incidentManagement/notifications.d"
35 import { NButton, NForm, NFormItem, NInput, NSpin, NSwitch, useMessage } from "naive-ui"
36 import { computed, onMounted, ref, watch } from "vue"
37 import Api from "@/api"
38
39 interface IncidentNotificationForm {
40 shuffle_workflow_id: string
41 enabled: boolean
42 }
43
44 const { incidentNotification, customerCode } = defineProps<{
45 incidentNotification?: IncidentNotification
46 customerCode: string
47 }>()
48
49 const emit = defineEmits<{
50 (e: "submitted", value: IncidentNotification): void
51 (e: "startLoading"): void
52 (e: "stopLoading"): void
53 (
54 e: "mounted",
55 value: {
56 reset: () => void
57 }
58 ): void
59 }>()
60
61 const message = useMessage()
62 const form = ref<IncidentNotificationForm>(getClearForm(incidentNotification))
63 const formRef = ref<FormInst | null>(null)
64 const loading = ref(false)
65
66 const isValid = computed(() => {
67 return !!form.value.shuffle_workflow_id
68 })
69
70 const rules: FormRules = {
71 shuffle_workflow_id: {
72 required: true,
73 message: "Please insert Shuffle Workflow Id",
74 trigger: ["input", "blur"]
75 }
76 }
77
78 watch(loading, val => {
79 if (val) {
80 emit("startLoading")
81 } else {
82 emit("stopLoading")
83 }
84 })
85
86 function getClearForm(incidentNotification?: IncidentNotification): IncidentNotificationForm {
87 return {
88 shuffle_workflow_id: incidentNotification?.shuffle_workflow_id ?? "",
89 enabled: incidentNotification?.enabled ?? false
90 }
91 }
92
93 function reset(incidentNotification?: IncidentNotification) {
94 if (!loading.value) {
95 resetForm(incidentNotification)
96 formRef.value?.restoreValidation()
97 }
98 }
99
100 function resetForm(incidentNotification?: IncidentNotification) {
101 form.value = getClearForm(incidentNotification)
102 }
103
104 function validate() {
105 if (!formRef.value) return
106
107 formRef.value.validate((errors?: Array<FormValidationError>) => {
108 if (!errors) {
109 submit()
110 } else {
111 message.warning("You must fill in the required fields correctly.")
112 return false
113 }
114 })
115 }
116
117 function submit() {
118 loading.value = true
119
120 const payload: IncidentNotificationPayload = {
121 customer_code: customerCode,
122 shuffle_workflow_id: form.value.shuffle_workflow_id,
123 enabled: form.value.enabled
124 }
125
126 Api.incidentManagement.notification
127 .setNotification(payload)
128 .then(res => {
129 if (res.data.success) {
130 message.success(res.data?.message || "Notification saved successfully")
131 emit("submitted", { ...payload, id: incidentNotification?.id || 0 })
132 } else {
133 message.warning(res.data?.message || "An error occurred. Please try again later.")
134 }
135 })
136 .catch(err => {
137 message.error(err.response?.data?.message || "An error occurred. Please try again later.")
138 })
139 .finally(() => {
140 loading.value = false
141 })
142 }
143
144 onMounted(() => {
145 emit("mounted", {
146 reset
147 })
148 })
149 </script>