PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / frontend / src / stores / useConfirmationSettingBuilder.ts

useConfirmationSettingBuilder.ts in The Innovative Form Builder – IvyForms 1.0, at frontend/src/stores/useConfirmationSettingBuilder.ts

311 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { defineStore } from 'pinia'
2 import { ref, watch, nextTick } from 'vue'
3 import { useLabels } from '@/composables/useLabels'
4 import IvyMessage from '@/views/_components/message/ivyMessage.ts'
5 import { useApiClient } from '@/composables/useApiClient'
6 import { useUnsavedChangesStore } from '@/stores/useUnsavedChangesStore'
7 import { getErrorMessage } from '@/utils/errorHandling'
8
9 export const useConfirmationSettingBuilder = defineStore('confirmationSettingBuilder', () => {
10 const allConfirmations = ref([])
11 const id = ref<number | null>(null)
12 const formId = ref<number | null>(null)
13 const type = ref<string | null>('successMessage')
14 const enabled = ref(true)
15 const showForm = ref(false)
16 const message = ref<string | null>('')
17 const url = ref<string | null>('')
18 const page = ref<string | null>('')
19 const pageUrl = ref<string | null>('')
20 const isEditing = ref(false)
21
22 const { getLabel } = useLabels()
23
24 // Use unsaved changes store for state management
25 const unsavedChangesStore = useUnsavedChangesStore()
26
27 // Flag to suppress change tracking during load/reset operations
28 let suppressDirtyTracking = false
29
30 /**
31 * Mark the confirmation builder as having unsaved changes
32 */
33 const markDirty = () => {
34 if (!suppressDirtyTracking && isEditing.value) {
35 unsavedChangesStore.markDirty('confirmationBuilder')
36 }
37 }
38
39 /**
40 * Mark the confirmation builder as clean (no unsaved changes)
41 */
42 const markClean = () => {
43 unsavedChangesStore.markClean('confirmationBuilder')
44 }
45
46 // Watch all tracked state for changes and mark dirty
47 watch([type, enabled, showForm, message, url, page, pageUrl], () => markDirty())
48
49 const resetEntityState = () => {
50 markClean()
51 }
52 const { request } = useApiClient()
53
54 // Load existing confirmation if confirmation id is provided
55 const loadConfirmation = async (confirmationId: number) => {
56 // Suppress dirty tracking during load
57 suppressDirtyTracking = true
58
59 try {
60 const { data, error, status } = await request(`/confirmation/${confirmationId}`, {
61 method: 'GET',
62 })
63 const confirmation = data?.data?.data
64 if (!error && status === 200 && confirmation) {
65 id.value = confirmationId
66 formId.value = confirmation.formId
67 type.value = confirmation.type || 'successMessage'
68 enabled.value = confirmation.enabled || false
69 showForm.value = confirmation.showForm || false
70 message.value = confirmation.message || ''
71 url.value = confirmation.url || ''
72 page.value = confirmation.page || ''
73 pageUrl.value = confirmation.pageUrl || ''
74 isEditing.value = true
75
76 // Start editing and mark clean (just loaded)
77 unsavedChangesStore.startEditing('confirmationBuilder')
78 markClean()
79 } else {
80 IvyMessage({
81 message: `${getLabel('failed_to_load_confirmation')} ${(data?.message ?? error) || ''}`,
82 type: 'error',
83 })
84 }
85 } catch (error) {
86 const errorMessage = getErrorMessage(error)
87 IvyMessage({
88 message: `${getLabel('failed_to_load_confirmation')} ${errorMessage}`,
89 type: 'error',
90 })
91 } finally {
92 // Wait for watchers to fire before re-enabling dirty tracking
93 await nextTick()
94 suppressDirtyTracking = false
95 }
96 }
97
98 // Fetch all confirmations
99 const fetchAllConfirmations = async (formIdValue: number) => {
100 if (!formIdValue || isNaN(Number(formIdValue))) {
101 return
102 }
103 // Suppress dirty tracking during fetch
104 suppressDirtyTracking = true
105
106 try {
107 const { data, error, status } = await request(`/confirmations/${formIdValue}`, {
108 method: 'GET',
109 })
110 if (!error && status === 200 && data?.data) {
111 const responseData = data.data[0][0]
112 id.value = responseData.id
113 formId.value = responseData.formId
114 type.value = responseData.type || 'successMessage'
115 enabled.value = responseData.enabled || false
116 showForm.value = responseData.showForm || false
117 message.value = responseData.message || ''
118 url.value = responseData.url || ''
119 page.value = responseData.page || ''
120 isEditing.value = true
121
122 // Start editing and mark clean (just loaded)
123 unsavedChangesStore.startEditing('confirmationBuilder')
124 markClean()
125 } else {
126 IvyMessage({
127 message: `${getLabel('failed_to_fetch_confirmation')} ${(data?.message ?? error) || ''}`,
128 type: 'error',
129 })
130 }
131 } catch (error) {
132 const errorMessage = getErrorMessage(error)
133 IvyMessage({
134 message: `${getLabel('failed_to_fetch_confirmation')} ${errorMessage}`,
135 type: 'error',
136 })
137 } finally {
138 // Re-enable dirty tracking
139 suppressDirtyTracking = false
140 }
141 }
142
143 // Reset confirmation to default values
144 const resetConfirmation = () => {
145 // Suppress dirty tracking during reset
146 suppressDirtyTracking = true
147
148 id.value = null
149 formId.value = null
150 type.value = ''
151 enabled.value = true
152 showForm.value = false
153 message.value = ''
154 url.value = ''
155 page.value = ''
156 isEditing.value = false
157
158 // Stop editing and clear dirty flag
159 unsavedChangesStore.stopEditing('confirmationBuilder')
160
161 // Re-enable dirty tracking
162 suppressDirtyTracking = false
163 }
164
165 // Create a new confirmation
166 const createConfirmation = async () => {
167 const confirmationData = {
168 formId: formId.value ?? null,
169 type: type.value || '',
170 enabled: enabled.value || false,
171 showForm: showForm.value || false,
172 message: message.value || '',
173 url: url.value || '',
174 page: page.value || '',
175 }
176 try {
177 const { data, error, status } = await request('/confirmation/add/', {
178 method: 'POST',
179 data: confirmationData,
180 })
181 if (!error && status === 200 && data?.data?.data) {
182 id.value = data.data.data.id
183 allConfirmations.value.push(data.data)
184 IvyMessage({
185 message: getLabel('create_confirmation'),
186 type: 'success',
187 })
188 markClean()
189 } else {
190 IvyMessage({
191 message: `${getLabel('failed_to_create_confirmation')} ${(data?.message ?? error) || ''}`,
192 type: 'error',
193 })
194 }
195 } catch (error) {
196 const errorMessage = getErrorMessage(error)
197 IvyMessage({
198 message: `${getLabel('error_creating_confirmation')} ${errorMessage}`,
199 type: 'error',
200 })
201 }
202 }
203
204 // Update confirmation data
205 const updateConfirmation = async (confirmationId, dataToUpdate) => {
206 try {
207 const { data, error, status } = await request(`/confirmation/update/${confirmationId}/`, {
208 method: 'POST',
209 data: dataToUpdate,
210 })
211 if (!error && status === 200) {
212 IvyMessage({
213 message: getLabel('update_confirmation'),
214 type: 'success',
215 })
216 markClean()
217 } else {
218 IvyMessage({
219 message: `${getLabel('error_creating_confirmation')}: ${(data?.message ?? error) || ''}`,
220 type: 'error',
221 })
222 }
223 } catch (error) {
224 const errorMessage = getErrorMessage(error)
225 IvyMessage({
226 message: `${getLabel('error_to_update_confirmation')} ${errorMessage}`,
227 type: 'error',
228 })
229 }
230 }
231
232 // Duplicate confirmation
233 const duplicateConfirmation = async (confirmationId: number) => {
234 try {
235 const { data, error, status } = await request(`/confirmation/duplicate/${confirmationId}/`, {
236 method: 'POST',
237 })
238 if (!error && status === 200 && data?.data) {
239 IvyMessage({
240 message: getLabel('updating_duplicated_confirmation'),
241 type: 'success',
242 })
243 allConfirmations.value.push(data.data)
244 } else {
245 IvyMessage({
246 message: `${getLabel('failed_to_duplicate_confirmation')} ${(data?.message ?? error) || ''}`,
247 type: 'error',
248 })
249 }
250 } catch (error) {
251 const errorMessage = getErrorMessage(error)
252 IvyMessage({
253 message: `${getLabel('error_duplicate_confirmation')} ${errorMessage}`,
254 type: 'error',
255 })
256 }
257 }
258
259 // Delete confirmation
260 const deleteConfirmation = async (confirmationId) => {
261 try {
262 const { data, error, status } = await request(`/confirmation/delete/${confirmationId}/`, {
263 method: 'POST',
264 data: { id: confirmationId },
265 })
266 if (!error && status === 200) {
267 IvyMessage({
268 message: getLabel('delete_confirmation'),
269 type: 'success',
270 })
271 allConfirmations.value = allConfirmations.value.filter((item) => item.id !== confirmationId)
272 } else {
273 IvyMessage({
274 message: `${getLabel('failed_to_delete_confirmation')} ${(data?.message ?? error) || ''}`,
275 type: 'error',
276 })
277 }
278 } catch (error) {
279 const errorMessage = getErrorMessage(error)
280 IvyMessage({
281 message: `${getLabel('error_delete_confirmation')} ${errorMessage}`,
282 type: 'error',
283 })
284 }
285 }
286
287 return {
288 allConfirmations,
289 id,
290 formId,
291 type,
292 enabled,
293 showForm,
294 resetEntityState,
295 message,
296 url,
297 page,
298 pageUrl,
299 isEditing,
300 markDirty,
301 markClean,
302 fetchAllConfirmations,
303 resetConfirmation,
304 createConfirmation,
305 updateConfirmation,
306 deleteConfirmation,
307 loadConfirmation,
308 duplicateConfirmation,
309 }
310 })
311