| 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.ts' |
| 6 |
|
| 7 |
export const useAllForms = defineStore('allForms', () => { |
| 8 |
const tableData = ref([]) |
| 9 |
const paginationMeta = ref({ |
| 10 |
page: 1, |
| 11 |
perPage: 10, |
| 12 |
total: 0, |
| 13 |
}) |
| 14 |
const filterCount = ref({ |
| 15 |
publishedTrueCount: 0, |
| 16 |
publishedFalseCount: 0, |
| 17 |
starredTrueCount: 0, |
| 18 |
starredFalseCount: 0, |
| 19 |
}) |
| 20 |
|
| 21 |
const { getLabel } = useLabels() |
| 22 |
const { request } = useApiClient() |
| 23 |
|
| 24 |
const fetchData = async ({ |
| 25 |
page = 1, |
| 26 |
perPage = 10, |
| 27 |
search = '', |
| 28 |
sortBy = '', |
| 29 |
sortOrder = 'desc', |
| 30 |
dateRange = [], |
| 31 |
filters = {}, |
| 32 |
shouldGetCount = false, |
| 33 |
}: { |
| 34 |
page?: number |
| 35 |
perPage?: number |
| 36 |
search?: string |
| 37 |
sortBy?: string |
| 38 |
sortOrder?: string |
| 39 |
dateRange?: Array<string> |
| 40 |
filters?: Record<string, string | number> |
| 41 |
shouldGetCount?: boolean |
| 42 |
} = {}) => { |
| 43 |
try { |
| 44 |
const params: Record< |
| 45 |
string, |
| 46 |
string | number | boolean | null | string[] | Record<string, string | number> |
| 47 |
> = { |
| 48 |
page, |
| 49 |
perPage, |
| 50 |
search, |
| 51 |
orderBy: sortBy, |
| 52 |
order: sortOrder, |
| 53 |
dateRange, |
| 54 |
filters, |
| 55 |
shouldGetCount, |
| 56 |
} |
| 57 |
const { data, error } = await request('forms/search', { |
| 58 |
method: 'GET', |
| 59 |
params, |
| 60 |
}) |
| 61 |
if (error) { |
| 62 |
IvyMessage({ |
| 63 |
message: `${getLabel('error_fetching_forms')} ${error}`, |
| 64 |
type: 'error', |
| 65 |
}) |
| 66 |
return |
| 67 |
} |
| 68 |
|
| 69 |
const response = data.data |
| 70 |
tableData.value = normalizeData(response.data.data) |
| 71 |
paginationMeta.value = response.data.meta |
| 72 |
filterCount.value = response.data.filterCount |
| 73 |
} catch (error) { |
| 74 |
IvyMessage({ |
| 75 |
message: `${getLabel('error_fetching_forms')} ${error}`, |
| 76 |
type: 'error', |
| 77 |
}) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
const updateStarred = async (id: number, starred: boolean) => { |
| 82 |
try { |
| 83 |
const { data, error, status } = await request(`form/update/starred/${id}/`, { |
| 84 |
method: 'POST', |
| 85 |
data: { |
| 86 |
attribute: 'starred', |
| 87 |
value: starred, |
| 88 |
}, |
| 89 |
}) |
| 90 |
|
| 91 |
if (error || status >= 400) { |
| 92 |
IvyMessage({ |
| 93 |
message: `${getLabel('failed_to_update_form')}: ${data?.message || error}`, |
| 94 |
type: 'error', |
| 95 |
}) |
| 96 |
} else { |
| 97 |
IvyMessage({ |
| 98 |
message: getLabel('form_updated'), |
| 99 |
type: 'success', |
| 100 |
}) |
| 101 |
} |
| 102 |
} catch (error) { |
| 103 |
IvyMessage({ |
| 104 |
message: `${getLabel('error_updating_forms')} ${error}`, |
| 105 |
type: 'error', |
| 106 |
}) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
const updateStatus = async (id: number, status: boolean) => { |
| 111 |
try { |
| 112 |
const { |
| 113 |
data, |
| 114 |
error, |
| 115 |
status: httpStatus, |
| 116 |
} = await request(`form/update/status/${id}/`, { |
| 117 |
method: 'POST', |
| 118 |
data: { |
| 119 |
value: status, |
| 120 |
}, |
| 121 |
}) |
| 122 |
|
| 123 |
if (error || httpStatus >= 400) { |
| 124 |
IvyMessage({ |
| 125 |
message: `${getLabel('failed_to_update_form')} ${data?.message || error}`, |
| 126 |
type: 'error', |
| 127 |
}) |
| 128 |
} else { |
| 129 |
IvyMessage({ |
| 130 |
message: getLabel('form_updated'), |
| 131 |
type: 'success', |
| 132 |
}) |
| 133 |
} |
| 134 |
} catch (error) { |
| 135 |
IvyMessage({ |
| 136 |
message: `${getLabel('error_updating_forms')} ${error}`, |
| 137 |
type: 'error', |
| 138 |
}) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
const normalizeData = (data: Array<{ starred: string; published: string }>) => { |
| 143 |
return data.map((item) => ({ |
| 144 |
...item, |
| 145 |
starred: item.starred === '1', |
| 146 |
published: item.published === '1', |
| 147 |
})) |
| 148 |
} |
| 149 |
|
| 150 |
return { |
| 151 |
tableData, |
| 152 |
paginationMeta, |
| 153 |
filterCount, |
| 154 |
fetchData, |
| 155 |
updateStarred, |
| 156 |
updateStatus, |
| 157 |
} |
| 158 |
}) |
| 159 |
|