| 1 |
import { defineStore } from 'pinia' |
| 2 |
import { ref } from 'vue' |
| 3 |
import axios from 'axios' |
| 4 |
import { useLabels } from '@/composables/useLabels' |
| 5 |
import IvyMessage from '@/views/_components/message/ivyMessage.ts' |
| 6 |
declare const wpIvyApiSettings: { nonce: string } |
| 7 |
|
| 8 |
export const useAllForms = defineStore('allForms', () => { |
| 9 |
const allForms = ref([]) |
| 10 |
const tableData = ref([]) |
| 11 |
const paginationMeta = ref({ |
| 12 |
page: 1, |
| 13 |
perPage: 10, |
| 14 |
total: 0, |
| 15 |
}) |
| 16 |
|
| 17 |
const { getLabel } = useLabels() |
| 18 |
const fetchForms = async ({ |
| 19 |
page = 1, |
| 20 |
perPage = 10, |
| 21 |
search = '', |
| 22 |
sortBy = '', |
| 23 |
sortOrder = 'asc', |
| 24 |
dateRange = [], |
| 25 |
filters = {}, |
| 26 |
}: { |
| 27 |
page?: number |
| 28 |
perPage?: number |
| 29 |
search?: string |
| 30 |
sortBy?: string |
| 31 |
sortOrder?: string |
| 32 |
dateRange?: Array<string> |
| 33 |
filters?: Record<string, string | number> |
| 34 |
} = {}) => { |
| 35 |
try { |
| 36 |
const params: Record< |
| 37 |
string, |
| 38 |
string | number | boolean | null | string[] | Record<string, string | number> |
| 39 |
> = { |
| 40 |
page, |
| 41 |
perPage: perPage, |
| 42 |
search, |
| 43 |
orderBy: sortBy, |
| 44 |
order: sortOrder, |
| 45 |
dateRange: dateRange, |
| 46 |
filters: filters, |
| 47 |
} |
| 48 |
|
| 49 |
const { data } = await axios.get('/wp-json/ivyforms/v1/forms/search', { |
| 50 |
params, |
| 51 |
headers: { |
| 52 |
'X-WP-Nonce': wpIvyApiSettings.nonce, |
| 53 |
}, |
| 54 |
}) |
| 55 |
|
| 56 |
const response = data.data |
| 57 |
tableData.value = normalizeData(response.data.data) |
| 58 |
paginationMeta.value = response.data.meta |
| 59 |
} catch (error) { |
| 60 |
IvyMessage({ |
| 61 |
message: `${getLabel('error_fetching_forms')} ${error}`, |
| 62 |
type: 'error', |
| 63 |
}) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
const updateStarred = async (id: number, starred: boolean) => { |
| 68 |
try { |
| 69 |
const response = await fetch(`/wp-json/ivyforms/v1/form/update/starred/${id}/`, { |
| 70 |
method: 'POST', |
| 71 |
headers: { |
| 72 |
'Content-Type': 'application/json', |
| 73 |
'X-WP-Nonce': wpIvyApiSettings.nonce, |
| 74 |
}, |
| 75 |
body: JSON.stringify({ |
| 76 |
attribute: 'starred', |
| 77 |
value: starred, |
| 78 |
}), |
| 79 |
}) |
| 80 |
|
| 81 |
const result = await response.json() |
| 82 |
|
| 83 |
if (response.ok) { |
| 84 |
IvyMessage({ |
| 85 |
message: getLabel('form_updated'), |
| 86 |
type: 'success', |
| 87 |
}) |
| 88 |
} else { |
| 89 |
IvyMessage({ |
| 90 |
message: `${getLabel('failed_to_update_form')} ${result.message}`, |
| 91 |
type: 'error', |
| 92 |
}) |
| 93 |
} |
| 94 |
} catch (error) { |
| 95 |
IvyMessage({ |
| 96 |
message: `${getLabel('error_updating_forms')} ${error}`, |
| 97 |
type: 'error', |
| 98 |
}) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
const updateStatus = async (id: number, status: boolean) => { |
| 103 |
try { |
| 104 |
const response = await fetch(`/wp-json/ivyforms/v1/form/update/status/${id}/`, { |
| 105 |
method: 'POST', |
| 106 |
headers: { |
| 107 |
'Content-Type': 'application/json', |
| 108 |
'X-WP-Nonce': wpIvyApiSettings.nonce, |
| 109 |
}, |
| 110 |
body: JSON.stringify({ |
| 111 |
value: status, |
| 112 |
}), |
| 113 |
}) |
| 114 |
|
| 115 |
const result = await response.json() |
| 116 |
|
| 117 |
if (response.ok) { |
| 118 |
IvyMessage({ |
| 119 |
message: getLabel('form_updated'), |
| 120 |
type: 'success', |
| 121 |
}) |
| 122 |
} else { |
| 123 |
IvyMessage({ |
| 124 |
message: `${getLabel('failed_to_update_form')} ${result.message}`, |
| 125 |
type: 'error', |
| 126 |
}) |
| 127 |
} |
| 128 |
} catch (error) { |
| 129 |
IvyMessage({ |
| 130 |
message: `${getLabel('error_updating_forms')} ${error}`, |
| 131 |
type: 'error', |
| 132 |
}) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
const deleteForms = async (ids: number[]) => { |
| 137 |
try { |
| 138 |
const response = await fetch(`/wp-json/ivyforms/v1/forms/delete`, { |
| 139 |
method: 'POST', |
| 140 |
headers: { |
| 141 |
'Content-Type': 'application/json', |
| 142 |
'X-WP-Nonce': wpIvyApiSettings.nonce, |
| 143 |
}, |
| 144 |
body: JSON.stringify({ ids }), |
| 145 |
}) |
| 146 |
|
| 147 |
const result = await response.json() |
| 148 |
|
| 149 |
if (response.ok) { |
| 150 |
IvyMessage({ |
| 151 |
message: getLabel('form_duplicated'), |
| 152 |
type: 'success', |
| 153 |
}) |
| 154 |
return true |
| 155 |
} else { |
| 156 |
IvyMessage({ |
| 157 |
message: `${getLabel('failed_to_delete_form')} ${result.message}`, |
| 158 |
type: 'error', |
| 159 |
}) |
| 160 |
} |
| 161 |
} catch (error) { |
| 162 |
IvyMessage({ |
| 163 |
message: `${getLabel('error_deleting_forms')} ${error}`, |
| 164 |
type: 'error', |
| 165 |
}) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
const duplicateForm = async (id: number) => { |
| 170 |
try { |
| 171 |
const response = await fetch(`/wp-json/ivyforms/v1/form/duplicate/${id}/`, { |
| 172 |
method: 'POST', |
| 173 |
headers: { |
| 174 |
'Content-Type': 'application/json', |
| 175 |
'X-WP-Nonce': wpIvyApiSettings.nonce, |
| 176 |
}, |
| 177 |
}) |
| 178 |
|
| 179 |
const result = await response.json() |
| 180 |
|
| 181 |
if (response.ok) { |
| 182 |
IvyMessage({ |
| 183 |
message: getLabel('form_duplicated_successfully'), |
| 184 |
type: 'success', |
| 185 |
}) |
| 186 |
allForms.value.push(result.data) |
| 187 |
} else { |
| 188 |
IvyMessage({ |
| 189 |
message: `${getLabel('failed_to_duplicate_form')} ${result.message}`, |
| 190 |
type: 'error', |
| 191 |
}) |
| 192 |
} |
| 193 |
} catch (error) { |
| 194 |
IvyMessage({ |
| 195 |
message: `${getLabel('error_duplicating_forms')} ${error}`, |
| 196 |
type: 'error', |
| 197 |
}) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
const normalizeData = (data: Array<{ starred: string; published: string }>) => { |
| 202 |
return data.map((item) => ({ |
| 203 |
...item, |
| 204 |
starred: item.starred === '1', |
| 205 |
published: item.published === '1', |
| 206 |
})) |
| 207 |
} |
| 208 |
|
| 209 |
return { |
| 210 |
tableData, |
| 211 |
paginationMeta, |
| 212 |
fetchForms, |
| 213 |
updateStarred, |
| 214 |
updateStatus, |
| 215 |
deleteForms, |
| 216 |
duplicateForm, |
| 217 |
} |
| 218 |
}) |
| 219 |
|