| 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 useAllEntries = defineStore('allEntries', () => { |
| 8 |
const tableData = ref([]) |
| 9 |
const entryFields = ref([]) |
| 10 |
const paginationMeta = ref({ |
| 11 |
page: 1, |
| 12 |
perPage: 10, |
| 13 |
total: 0, |
| 14 |
}) |
| 15 |
const filterCount = ref({ |
| 16 |
readTrueCount: 0, |
| 17 |
readFalseCount: 0, |
| 18 |
starredTrueCount: 0, |
| 19 |
starredFalseCount: 0, |
| 20 |
}) |
| 21 |
const formIdToName = ref<Record<number, string>>({}) |
| 22 |
|
| 23 |
const { getLabel } = useLabels() |
| 24 |
const { request } = useApiClient() |
| 25 |
const fetchData = async ({ |
| 26 |
page = 1, |
| 27 |
perPage = 10, |
| 28 |
search = '', |
| 29 |
sortBy = '', |
| 30 |
sortOrder = 'desc', |
| 31 |
dateRange = [], |
| 32 |
filters = {}, |
| 33 |
shouldGetCount = true, |
| 34 |
formId = null, |
| 35 |
searchFieldValue = false, |
| 36 |
}: { |
| 37 |
page?: number |
| 38 |
perPage?: number |
| 39 |
search?: string |
| 40 |
sortBy?: string |
| 41 |
sortOrder?: string |
| 42 |
dateRange?: Array<string> |
| 43 |
filters?: Record<string, string | number | null> |
| 44 |
shouldGetCount?: boolean |
| 45 |
formId?: number | null |
| 46 |
searchFieldValue?: boolean |
| 47 |
} = {}) => { |
| 48 |
try { |
| 49 |
// Always include formId in filters if present |
| 50 |
const mergedFilters = { ...filters } |
| 51 |
if (formId !== null && formId !== undefined) { |
| 52 |
mergedFilters.formId = formId |
| 53 |
} |
| 54 |
const params: Record< |
| 55 |
string, |
| 56 |
string | number | boolean | null | string[] | Record<string, string | number> |
| 57 |
> = { |
| 58 |
page, |
| 59 |
perPage, |
| 60 |
search, |
| 61 |
orderBy: sortBy, |
| 62 |
order: sortOrder, |
| 63 |
dateRange, |
| 64 |
filters: mergedFilters, |
| 65 |
shouldGetCount, |
| 66 |
searchFieldValue, |
| 67 |
} |
| 68 |
const { data, error } = await request('entries/search', { |
| 69 |
method: 'GET', |
| 70 |
params, |
| 71 |
}) |
| 72 |
if (error) { |
| 73 |
IvyMessage({ |
| 74 |
message: `${getLabel('error_fetching_entries')} ${error}`, |
| 75 |
type: 'error', |
| 76 |
}) |
| 77 |
return |
| 78 |
} |
| 79 |
|
| 80 |
const response = data.data |
| 81 |
tableData.value = response.data.data |
| 82 |
paginationMeta.value = response.data.meta |
| 83 |
if (shouldGetCount) { |
| 84 |
filterCount.value = response.data.filterCount |
| 85 |
} |
| 86 |
entryFields.value = response.data.entryFields |
| 87 |
formIdToName.value = response.data.formIdToName || {} |
| 88 |
} catch (error) { |
| 89 |
IvyMessage({ |
| 90 |
message: `${getLabel('error_fetching_entries')} ${error}`, |
| 91 |
type: 'error', |
| 92 |
}) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
const updateStarred = async (id: number, starred: boolean) => { |
| 97 |
try { |
| 98 |
const { data, error, status } = await request(`entry/update/starred/${id}/`, { |
| 99 |
method: 'POST', |
| 100 |
data: { |
| 101 |
attribute: 'starred', |
| 102 |
value: starred, |
| 103 |
}, |
| 104 |
}) |
| 105 |
|
| 106 |
if (error || status >= 400) { |
| 107 |
IvyMessage({ |
| 108 |
message: starred |
| 109 |
? `${getLabel('failed_to_star_entry')} ${data?.message || error}` |
| 110 |
: `${getLabel('failed_to_unstar_entry')} ${data?.message || error}`, |
| 111 |
type: 'error', |
| 112 |
}) |
| 113 |
} else { |
| 114 |
IvyMessage({ |
| 115 |
message: starred ? getLabel('entry_starred') : getLabel('entry_unstarred'), |
| 116 |
type: 'success', |
| 117 |
}) |
| 118 |
} |
| 119 |
} catch (error) { |
| 120 |
IvyMessage({ |
| 121 |
message: starred |
| 122 |
? `${getLabel('failed_to_star_entry')} ${error}` |
| 123 |
: `${getLabel('failed_to_unstar_entry')} ${error}`, |
| 124 |
type: 'error', |
| 125 |
}) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
const updateStatus = async (id: number, status: string) => { |
| 130 |
try { |
| 131 |
const { |
| 132 |
data, |
| 133 |
error, |
| 134 |
status: httpStatus, |
| 135 |
} = await request(`entry/update/status/${id}/`, { |
| 136 |
method: 'POST', |
| 137 |
data: { |
| 138 |
value: status, |
| 139 |
}, |
| 140 |
}) |
| 141 |
|
| 142 |
if (error || httpStatus >= 400) { |
| 143 |
IvyMessage({ |
| 144 |
message: `${getLabel('failed_to_update_entry_status')} ${data?.message || error}`, |
| 145 |
type: 'error', |
| 146 |
}) |
| 147 |
} else { |
| 148 |
IvyMessage({ |
| 149 |
message: status === 'read' ? getLabel('entry_read') : getLabel('entry_unread'), |
| 150 |
type: 'success', |
| 151 |
}) |
| 152 |
} |
| 153 |
} catch (error) { |
| 154 |
IvyMessage({ |
| 155 |
message: `${getLabel('failed_to_update_entry_status')} ${error}`, |
| 156 |
type: 'error', |
| 157 |
}) |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
const reset = () => { |
| 162 |
tableData.value = [] |
| 163 |
entryFields.value = [] |
| 164 |
paginationMeta.value = { page: 1, perPage: 10, total: 0 } |
| 165 |
filterCount.value = { |
| 166 |
readTrueCount: 0, |
| 167 |
readFalseCount: 0, |
| 168 |
starredTrueCount: 0, |
| 169 |
starredFalseCount: 0, |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return { |
| 174 |
tableData, |
| 175 |
entryFields, |
| 176 |
paginationMeta, |
| 177 |
filterCount, |
| 178 |
formIdToName, |
| 179 |
fetchData, |
| 180 |
updateStarred, |
| 181 |
updateStatus, |
| 182 |
reset, |
| 183 |
} |
| 184 |
}) |
| 185 |
|