| 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 |
|
| 6 |
export const useNotificationSettingBuilder = defineStore('notificationSettingBuilder', () => { |
| 7 |
const allNotifications = ref([]) |
| 8 |
const name = ref<string | null>('New Notification') |
| 9 |
const sender = ref<string | null>('') |
| 10 |
const receiver = ref<string | null>('') |
| 11 |
const status = ref(false) |
| 12 |
const subject = ref<string | null>('') |
| 13 |
const message = ref<string | null>('') |
| 14 |
const smartLogic = ref(false) |
| 15 |
const formId = ref<string | null>(null) |
| 16 |
const id = ref<number | null>(null) |
| 17 |
const isEditing = ref(false) // Make `isEditing` reactive |
| 18 |
|
| 19 |
const { getLabel } = useLabels() |
| 20 |
// Load existing notification if notification id is provided |
| 21 |
const loadNotification = async (NotifId: number) => { |
| 22 |
try { |
| 23 |
const response = await fetch(`/wp-json/ivyforms/v1/notification/${NotifId}`, { |
| 24 |
method: 'GET', |
| 25 |
headers: { |
| 26 |
'Content-Type': 'application/json', |
| 27 |
// eslint-disable-next-line no-undef |
| 28 |
'X-WP-Nonce': wpIvyApiSettings.nonce, |
| 29 |
}, |
| 30 |
}) |
| 31 |
|
| 32 |
const result = await response.json() |
| 33 |
|
| 34 |
if (response.ok && response.status == 200 && result.data.data) { |
| 35 |
formId.value = result.data.data.formId |
| 36 |
id.value = NotifId |
| 37 |
name.value = result.data.data.name || 'New Notification' |
| 38 |
sender.value = result.data.data.sender || '' |
| 39 |
receiver.value = result.data.data.receiver || '' |
| 40 |
status.value = result.data.data.status || false |
| 41 |
subject.value = result.data.data.subject || 'New Notification' |
| 42 |
message.value = result.data.data.message || '' |
| 43 |
smartLogic.value = false |
| 44 |
isEditing.value = true // Set isEditing to true once notification is loaded |
| 45 |
} else { |
| 46 |
// Handle error when loading the notification |
| 47 |
IvyMessage({ |
| 48 |
message: `${getLabel('failed_to_load_notification')} ${result.message}`, |
| 49 |
type: 'error', |
| 50 |
}) |
| 51 |
} |
| 52 |
} catch (error) { |
| 53 |
IvyMessage({ |
| 54 |
message: `${getLabel('failed_to_load_notification')} ${error}`, |
| 55 |
type: 'error', |
| 56 |
}) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// Fetch all form notifications |
| 61 |
const fetchAllFormNotifications = async (formId?: number) => { |
| 62 |
try { |
| 63 |
const url = formId |
| 64 |
? `/wp-json/ivyforms/v1/notifications/${formId}` |
| 65 |
: `/wp-json/ivyforms/v1/notifications` |
| 66 |
|
| 67 |
const response = await fetch(url, { |
| 68 |
method: 'GET', |
| 69 |
headers: { |
| 70 |
'Content-Type': 'application/json', |
| 71 |
// eslint-disable-next-line no-undef |
| 72 |
'X-WP-Nonce': wpIvyApiSettings.nonce, // Add nonce for authentication |
| 73 |
}, |
| 74 |
}) |
| 75 |
|
| 76 |
if (!response.ok) { |
| 77 |
IvyMessage({ |
| 78 |
message: `${getLabel('failed_to_fetch_notification')} ${response.statusText}`, |
| 79 |
type: 'error', |
| 80 |
}) |
| 81 |
return |
| 82 |
} |
| 83 |
|
| 84 |
const result = await response.json() |
| 85 |
|
| 86 |
if (result && result.data) { |
| 87 |
allNotifications.value = result.data.data |
| 88 |
} else { |
| 89 |
allNotifications.value = [] |
| 90 |
IvyMessage({ |
| 91 |
message: `${getLabel('unexpected_response_structure')} ${result}`, |
| 92 |
type: 'error', |
| 93 |
}) |
| 94 |
} |
| 95 |
} catch (error) { |
| 96 |
IvyMessage({ |
| 97 |
message: `${getLabel('failed_to_fetch_notification')} ${error}`, |
| 98 |
type: 'error', |
| 99 |
}) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// Fetch all notifications |
| 104 |
const fetchAllNotifications = async () => { |
| 105 |
try { |
| 106 |
const url = `/wp-json/ivyforms/v1/notifications` |
| 107 |
|
| 108 |
const response = await fetch(url, { |
| 109 |
method: 'GET', |
| 110 |
headers: { |
| 111 |
'Content-Type': 'application/json', |
| 112 |
// eslint-disable-next-line no-undef |
| 113 |
'X-WP-Nonce': wpIvyApiSettings.nonce, // Add nonce for authentication |
| 114 |
}, |
| 115 |
}) |
| 116 |
|
| 117 |
if (!response.ok) { |
| 118 |
IvyMessage({ |
| 119 |
message: `${getLabel('failed_to_fetch_notification')} ${response.statusText}`, |
| 120 |
type: 'error', |
| 121 |
}) |
| 122 |
return |
| 123 |
} |
| 124 |
|
| 125 |
const result = await response.json() |
| 126 |
|
| 127 |
if (result && result.data) { |
| 128 |
allNotifications.value = result.data.data |
| 129 |
} else { |
| 130 |
allNotifications.value = [] |
| 131 |
IvyMessage({ |
| 132 |
message: `${getLabel('unexpected_response_structure')} ${result}`, |
| 133 |
type: 'error', |
| 134 |
}) |
| 135 |
} |
| 136 |
} catch (error) { |
| 137 |
IvyMessage({ |
| 138 |
message: `${getLabel('failed_to_fetch_notification')} ${error}`, |
| 139 |
type: 'error', |
| 140 |
}) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
// Reset notification to default values |
| 145 |
const resetNotification = (formIdRoute: string) => { |
| 146 |
name.value = getLabel('new_notification') |
| 147 |
sender.value = '' |
| 148 |
receiver.value = '' |
| 149 |
status.value = true |
| 150 |
subject.value = getLabel('new_notification') |
| 151 |
message.value = '{all_data}' |
| 152 |
smartLogic.value = false |
| 153 |
formId.value = formIdRoute |
| 154 |
id.value = null |
| 155 |
isEditing.value = false // Reset to false when resetting notification |
| 156 |
} |
| 157 |
|
| 158 |
// Create a new notification |
| 159 |
const createNotification = async (formId) => { |
| 160 |
const notificationData = { |
| 161 |
name: name.value || getLabel('new_notification'), |
| 162 |
sender: sender.value || '', |
| 163 |
receiver: receiver.value || '', |
| 164 |
status: status.value || false, |
| 165 |
subject: subject.value || getLabel('new_notification'), |
| 166 |
message: message.value || '', |
| 167 |
smartLogic: smartLogic.value || false, |
| 168 |
formId: formId || null, |
| 169 |
} |
| 170 |
|
| 171 |
try { |
| 172 |
const response = await fetch('/wp-json/ivyforms/v1/notification/add/', { |
| 173 |
method: 'POST', |
| 174 |
headers: { |
| 175 |
'Content-Type': 'application/json', |
| 176 |
// eslint-disable-next-line no-undef |
| 177 |
'X-WP-Nonce': wpIvyApiSettings.nonce, |
| 178 |
}, |
| 179 |
body: JSON.stringify(notificationData), |
| 180 |
}) |
| 181 |
|
| 182 |
const result = await response.json() |
| 183 |
|
| 184 |
if (response.ok) { |
| 185 |
id.value = result.data.data.id |
| 186 |
allNotifications.value.push(result.data) |
| 187 |
IvyMessage({ |
| 188 |
message: getLabel('create_notification'), |
| 189 |
type: 'success', |
| 190 |
}) |
| 191 |
} else { |
| 192 |
IvyMessage({ |
| 193 |
message: `${getLabel('failed_create_notification')} ${result.message}`, |
| 194 |
type: 'error', |
| 195 |
}) |
| 196 |
} |
| 197 |
} catch (error) { |
| 198 |
IvyMessage({ |
| 199 |
message: `${getLabel('creating_error_notification')} ${error}`, |
| 200 |
type: 'error', |
| 201 |
}) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
// Update notification data |
| 206 |
const updateNotification = async (NotifId, data) => { |
| 207 |
try { |
| 208 |
const response = await fetch(`/wp-json/ivyforms/v1/notification/update/${NotifId}/`, { |
| 209 |
method: 'POST', |
| 210 |
headers: { |
| 211 |
'Content-Type': 'application/json', |
| 212 |
// eslint-disable-next-line no-undef |
| 213 |
'X-WP-Nonce': wpIvyApiSettings.nonce, |
| 214 |
}, |
| 215 |
body: JSON.stringify(data), |
| 216 |
}) |
| 217 |
|
| 218 |
const result = await response.json() |
| 219 |
|
| 220 |
if (response.ok) { |
| 221 |
IvyMessage({ |
| 222 |
message: getLabel('update_notification'), |
| 223 |
type: 'success', |
| 224 |
}) |
| 225 |
} else { |
| 226 |
IvyMessage({ |
| 227 |
message: `${getLabel('failed_to_update_notification')} ${result.message}`, |
| 228 |
type: 'error', |
| 229 |
}) |
| 230 |
} |
| 231 |
} catch (error) { |
| 232 |
IvyMessage({ |
| 233 |
message: `${getLabel('updating_error_notification')} ${error}`, |
| 234 |
type: 'error', |
| 235 |
}) |
| 236 |
} |
| 237 |
} |
| 238 |
const duplicateNotification = async (id: number) => { |
| 239 |
try { |
| 240 |
const response = await fetch(`/wp-json/ivyforms/v1/notification/duplicate/${id}/`, { |
| 241 |
method: 'POST', |
| 242 |
headers: { |
| 243 |
'Content-Type': 'application/json', |
| 244 |
// eslint-disable-next-line no-undef |
| 245 |
'X-WP-Nonce': wpIvyApiSettings.nonce, |
| 246 |
}, |
| 247 |
}) |
| 248 |
const result = await response.json() |
| 249 |
if (response.ok) { |
| 250 |
IvyMessage({ |
| 251 |
message: getLabel('duplicated_notification'), |
| 252 |
type: 'success', |
| 253 |
}) |
| 254 |
allNotifications.value.push(result.data) |
| 255 |
} else { |
| 256 |
IvyMessage({ |
| 257 |
message: `${getLabel('failed_to_duplicate_notification')} ${result.message}`, |
| 258 |
type: 'error', |
| 259 |
}) |
| 260 |
} |
| 261 |
} catch (error) { |
| 262 |
IvyMessage({ |
| 263 |
message: `${getLabel('updating_duplicate_notification')} ${error}`, |
| 264 |
type: 'error', |
| 265 |
}) |
| 266 |
} |
| 267 |
} |
| 268 |
// Delete notifications |
| 269 |
const deleteNotifications = async (id) => { |
| 270 |
try { |
| 271 |
const response = await fetch(`/wp-json/ivyforms/v1/notification/delete/${id}/`, { |
| 272 |
method: 'POST', |
| 273 |
headers: { |
| 274 |
'Content-Type': 'application/json', |
| 275 |
// eslint-disable-next-line no-undef |
| 276 |
'X-WP-Nonce': wpIvyApiSettings.nonce, |
| 277 |
}, |
| 278 |
body: JSON.stringify({ id }), |
| 279 |
}) |
| 280 |
|
| 281 |
const result = await response.json() |
| 282 |
|
| 283 |
if (response.ok) { |
| 284 |
IvyMessage({ |
| 285 |
message: getLabel('delete_notification'), |
| 286 |
type: 'success', |
| 287 |
}) |
| 288 |
if (allNotifications.value.length === 1) { |
| 289 |
allNotifications.value = [] |
| 290 |
} else { |
| 291 |
allNotifications.value.push(result.data) |
| 292 |
} |
| 293 |
} else { |
| 294 |
IvyMessage({ |
| 295 |
message: `${getLabel('failed_deleting_notification')} ${result.message}`, |
| 296 |
type: 'error', |
| 297 |
}) |
| 298 |
} |
| 299 |
} catch (error) { |
| 300 |
IvyMessage({ |
| 301 |
message: `${getLabel('error_deleting_notification')} ${error}`, |
| 302 |
type: 'error', |
| 303 |
}) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
return { |
| 308 |
allNotifications, |
| 309 |
name, |
| 310 |
sender, |
| 311 |
receiver, |
| 312 |
status, |
| 313 |
subject, |
| 314 |
message, |
| 315 |
smartLogic, |
| 316 |
formId, |
| 317 |
id, |
| 318 |
isEditing, |
| 319 |
fetchAllFormNotifications, |
| 320 |
fetchAllNotifications, |
| 321 |
resetNotification, |
| 322 |
createNotification, |
| 323 |
updateNotification, |
| 324 |
deleteNotifications, |
| 325 |
loadNotification, |
| 326 |
duplicateNotification, |
| 327 |
} |
| 328 |
}) |
| 329 |
|