PluginProbe
The Innovative Form Builder – IvyForms / 0.7
The Innovative Form Builder – IvyForms v0.7
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 / useNotificationSettingBuilder.ts

useNotificationSettingBuilder.ts in The Innovative Form Builder – IvyForms 0.7, at frontend/src/stores/useNotificationSettingBuilder.ts

367 lines 10.4 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 } 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
8 export const useNotificationSettingBuilder = defineStore('notificationSettingBuilder', () => {
9 const allNotifications = ref([])
10 const { getLabel } = useLabels()
11 const tableData = ref([])
12 const paginationMeta = ref({
13 page: 1,
14 perPage: 10,
15 total: 0,
16 })
17 const name = ref<string | null>(getLabel('new_notification'))
18 const sender = ref<string | null>('')
19 const replyTo = ref<string | null>('')
20 const receiver = ref<string | null>('')
21 const enabled = ref(false)
22 const subject = ref<string | null>('')
23 const message = ref<string | null>('')
24 const smartLogic = ref(false)
25 const formId = ref<string | null>(null)
26 const id = ref<number | null>(null)
27 const isEditing = ref(false) // Make `isEditing` reactive
28
29 // Use unsaved changes store for state management
30 const unsavedChangesStore = useUnsavedChangesStore()
31
32 // Create complete entity manager for notification builder
33 const entityManager = unsavedChangesStore.createEntityManager('notificationBuilder', {
34 name,
35 sender,
36 replyTo,
37 receiver,
38 enabled,
39 subject,
40 message,
41 })
42
43 const { request } = useApiClient()
44
45 // Load existing notification if notification id is provided
46 const loadNotification = async (NotifId: number) => {
47 try {
48 const { data, error, status } = await request(`/notification/${NotifId}`, {
49 method: 'GET',
50 })
51 if (!error && status === 200 && data?.data?.data) {
52 const responseData = data.data.data
53 formId.value = responseData.formId
54 id.value = NotifId
55 name.value = responseData.name || getLabel('new_notification')
56 sender.value = responseData.sender || ''
57 replyTo.value = responseData.replyTo || ''
58 receiver.value = responseData.receiver || ''
59 enabled.value = responseData.enabled || false
60 subject.value = responseData.subject || getLabel('new_notification')
61 message.value = responseData.message || ''
62 smartLogic.value = false
63 isEditing.value = true
64 } else {
65 IvyMessage({
66 message: `${getLabel('failed_to_load_notification')} ${data?.message || error}`,
67 type: 'error',
68 })
69 }
70 } catch (error) {
71 IvyMessage({
72 message: `${getLabel('failed_to_load_notification')} ${error}`,
73 type: 'error',
74 })
75 }
76 }
77
78 // Fetch all form notifications
79 const fetchData = async (formId?: number) => {
80 try {
81 const url = formId ? `/notifications/${formId}` : `/notifications`
82 const { data, error } = await request(url, { method: 'GET' })
83 if (data && data.data) {
84 allNotifications.value = data.data.data
85 } else {
86 allNotifications.value = []
87 IvyMessage({
88 message: `${getLabel('unexpected_response_structure')} ${error || data}`,
89 type: 'error',
90 })
91 }
92 } catch (error) {
93 IvyMessage({
94 message: `${getLabel('failed_to_fetch_notification')} ${error}`,
95 type: 'error',
96 })
97 }
98 }
99
100 // Fetch all notifications
101 const fetchAllNotifications = async () => {
102 try {
103 const url = `/notifications`
104 const { data, error } = await request(url, { method: 'GET' })
105 if (data && data.data) {
106 allNotifications.value = data.data.data
107 } else {
108 allNotifications.value = []
109 IvyMessage({
110 message: `${getLabel('unexpected_response_structure')} ${error || data}`,
111 type: 'error',
112 })
113 }
114 } catch (error) {
115 IvyMessage({
116 message: `${getLabel('failed_to_fetch_notification')} ${error}`,
117 type: 'error',
118 })
119 }
120 }
121
122 // Reset notification to default values
123 const resetNotification = (formIdRoute: string) => {
124 name.value = getLabel('new_notification')
125 sender.value = ''
126 replyTo.value = ''
127 receiver.value = ''
128 enabled.value = true
129 subject.value = getLabel('new_notification')
130 message.value = '{{all_data}}'
131 smartLogic.value = false
132 formId.value = formIdRoute
133 id.value = null
134 isEditing.value = false // Reset to false when resetting notification
135 }
136
137 // Create a new notification
138 const createNotification = async (formId) => {
139 const notificationData = {
140 name: name.value || getLabel('new_notification'),
141 sender: sender.value || '',
142 replyTo: replyTo.value || '',
143 receiver: receiver.value || '',
144 enabled: enabled.value || false,
145 subject: subject.value || getLabel('new_notification'),
146 message: message.value || '',
147 smartLogic: smartLogic.value || false,
148 formId: formId || null,
149 }
150
151 try {
152 const { data, error, status } = await request('/notification/add/', {
153 method: 'POST',
154 data: notificationData,
155 })
156 if (!error && status === 200) {
157 id.value = data.data.data.id
158 allNotifications.value.push(data.data)
159 IvyMessage({
160 message: getLabel('create_notification'),
161 type: 'success',
162 })
163 entityManager.resetUnsavedChanges()
164 } else {
165 IvyMessage({
166 message: `${getLabel('failed_to_create_notification')}: ${data?.message || error}`,
167 type: 'error',
168 })
169 }
170 } catch (error) {
171 IvyMessage({
172 message: `${getLabel('error_creating_notification')} ${error}`,
173 type: 'error',
174 })
175 }
176 }
177
178 // Update notification data
179 const updateNotification = async (NotifId, dataObj) => {
180 try {
181 const { data, error, status } = await request(`/notification/update/${NotifId}/`, {
182 method: 'POST',
183 data: dataObj,
184 })
185 if (!error && status === 200) {
186 IvyMessage({
187 message: getLabel('update_notification'),
188 type: 'success',
189 })
190 entityManager.resetUnsavedChanges()
191 } else {
192 IvyMessage({
193 message: `${getLabel('failed_to_update_notification')}: ${data?.message || error}`,
194 type: 'error',
195 })
196 }
197 } catch (error) {
198 IvyMessage({
199 message: `${getLabel('error_updating_notification')} ${error}`,
200 type: 'error',
201 })
202 }
203 }
204
205 // Search notifications with pagination and sorting
206 const searchNotifications = async ({
207 page = 1,
208 perPage = 10,
209 search = '',
210 sortBy = '',
211 sortOrder = 'asc',
212 filters = {},
213 formId = null,
214 }: {
215 page?: number
216 perPage?: number
217 search?: string
218 sortBy?: string
219 sortOrder?: string
220 filters?: Record<string, string | number | null>
221 formId?: number | null
222 } = {}) => {
223 try {
224 const mergedFilters = { ...filters }
225 if (formId !== null && formId !== undefined) {
226 mergedFilters.formId = formId
227 }
228 const params: Record<
229 string,
230 string | number | boolean | null | Record<string, string | number | null>
231 > = {
232 page,
233 perPage,
234 search,
235 orderBy: sortBy,
236 order: sortOrder,
237 filters: mergedFilters,
238 }
239
240 const { data, error } = await request('notifications/search', {
241 method: 'GET',
242 params,
243 })
244
245 if (error) {
246 IvyMessage({
247 message: `${getLabel('error_fetching_notifications')} ${error}`,
248 type: 'error',
249 })
250 return
251 }
252
253 const response = data.data
254 tableData.value = normalizeNotificationData(response.data.data || [])
255 paginationMeta.value = response.data.meta || { page: 1, perPage: 10, total: 0 }
256 } catch (error) {
257 IvyMessage({
258 message: `${getLabel('error_fetching_notifications')} ${error}`,
259 type: 'error',
260 })
261 }
262 }
263
264 // Define proper types for notification data
265 interface RawNotificationItem {
266 id?: number
267 name?: string
268 sender?: string
269 receiver?: string
270 subject?: string
271 message?: string
272 enabled?: string | number | boolean
273 formId?: number
274 [key: string]: unknown // For any wrapped notification properties
275 }
276
277 interface NormalizedNotification {
278 id: number
279 name: string
280 sender: string
281 receiver: string
282 subject: string
283 message: string
284 enabled: boolean
285 formId: number
286 }
287
288 // Normalize notification data structure
289 const normalizeNotificationData = (data: RawNotificationItem[]): NormalizedNotification[] => {
290 return data.map((item) => {
291 if (item && typeof item === 'object') {
292 const notificationKey = Object.keys(item).find(
293 (key) => key.includes('notification') || key.includes('Notification'),
294 )
295
296 if (notificationKey && item[notificationKey]) {
297 const notification = item[notificationKey] as RawNotificationItem
298 return {
299 id: notification.id || 0,
300 name: notification.name || '',
301 sender: notification.sender || '',
302 replyTo: notification.replyTo || '',
303 receiver: notification.receiver || '',
304 subject: notification.subject || '',
305 message: notification.message || '',
306 enabled: notification.enabled === '1',
307 formId: notification.formId || 0,
308 }
309 }
310
311 // If it's a direct notification object
312 if (item.id && item.name !== undefined) {
313 return {
314 id: item.id,
315 name: item.name,
316 sender: item.sender || '',
317 replyTo: item.replyTo || '',
318 receiver: item.receiver || '',
319 subject: item.subject || '',
320 message: item.message || '',
321 enabled: item.enabled === '1',
322 formId: item.formId || null,
323 }
324 }
325 }
326
327 // Return a fallback object for items that couldn't be normalized
328 return {
329 id: 0,
330 name: 'Unknown',
331 sender: '',
332 replyTo: '',
333 receiver: '',
334 subject: '',
335 message: '',
336 enabled: false,
337 formId: null,
338 }
339 })
340 }
341
342 return {
343 allNotifications,
344 tableData,
345 paginationMeta,
346 name,
347 sender,
348 replyTo,
349 receiver,
350 enabled,
351 subject,
352 message,
353 smartLogic,
354 formId,
355 id,
356 isEditing,
357 getCurrentState: entityManager.getCurrentState,
358 fetchData,
359 fetchAllNotifications,
360 resetNotification,
361 createNotification,
362 updateNotification,
363 loadNotification,
364 searchNotifications,
365 }
366 })
367