PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
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 / useAllEntries.ts

useAllEntries.ts in The Innovative Form Builder – IvyForms 1.0, at frontend/src/stores/useAllEntries.ts

256 lines 6.7 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.ts'
6
7 /**
8 * Store for managing all entries table data and actions.
9 */
10 export const useAllEntries = defineStore('allEntries', () => {
11 const tableData = ref([])
12 const entryFields = ref([])
13 const paginationMeta = ref({
14 page: 1,
15 perPage: 10,
16 total: 0,
17 })
18 const filterCount = ref({
19 readTrueCount: 0,
20 readFalseCount: 0,
21 starredTrueCount: 0,
22 starredFalseCount: 0,
23 })
24 const formIdToName = ref<Record<number, string>>({})
25
26 const { getLabel } = useLabels()
27 const { request } = useApiClient()
28 /**
29 * Fetch entries data from the API with validation and always update meta.
30 * @param options - Fetch options
31 */
32 const fetchData = async ({
33 page = 1,
34 perPage = 10,
35 search = '',
36 sortBy = '',
37 sortOrder = 'desc',
38 dateRange = [],
39 filters = {},
40 shouldGetCount = true,
41 formId = null,
42 searchFieldValue = false,
43 }: {
44 page?: number
45 perPage?: number
46 search?: string
47 sortBy?: string
48 sortOrder?: string
49 dateRange?: Array<string>
50 filters?: Record<string, string | number | boolean | null>
51 shouldGetCount?: boolean
52 formId?: number | null
53 searchFieldValue?: boolean
54 } = {}) => {
55 try {
56 // Always include formId in filters if present
57 const mergedFilters = { ...filters }
58 if (formId !== null && formId !== undefined) {
59 mergedFilters.formId = formId
60 }
61 const params: Record<
62 string,
63 string | number | boolean | null | string[] | Record<string, string | number | boolean>
64 > = {
65 page,
66 perPage,
67 search,
68 orderBy: sortBy,
69 order: sortOrder,
70 dateRange,
71 filters: mergedFilters,
72 shouldGetCount,
73 searchFieldValue,
74 }
75 const { data, error } = await request('entries/search', {
76 method: 'GET',
77 params,
78 })
79 if (error) {
80 IvyMessage({
81 message: `${getLabel('error_fetching_entries')} ${error}`,
82 type: 'error',
83 })
84 return
85 }
86
87 const response = data.data
88 tableData.value = response.data.data
89 paginationMeta.value = response.data.meta
90 if (shouldGetCount) {
91 filterCount.value = response.data.filterCount
92 }
93 entryFields.value = response.data.entryFields
94 formIdToName.value = response.data.formIdToName || {}
95 } catch (error) {
96 IvyMessage({
97 message: `${getLabel('error_fetching_entries')} ${error}`,
98 type: 'error',
99 })
100 }
101 }
102
103 const updateStarred = async (id: number, starred: boolean) => {
104 try {
105 const { data, error, status } = await request(`entry/update/starred/${id}/`, {
106 method: 'POST',
107 data: {
108 attribute: 'starred',
109 value: starred,
110 },
111 })
112
113 if (error || status >= 400) {
114 IvyMessage({
115 message: starred
116 ? `${getLabel('failed_to_star_entry')} ${data?.message || error}`
117 : `${getLabel('failed_to_unstar_entry')} ${data?.message || error}`,
118 type: 'error',
119 })
120 } else {
121 IvyMessage({
122 message: starred ? getLabel('entry_starred') : getLabel('entry_unstarred'),
123 type: 'success',
124 })
125 }
126 } catch (error) {
127 IvyMessage({
128 message: starred
129 ? `${getLabel('failed_to_star_entry')} ${error}`
130 : `${getLabel('failed_to_unstar_entry')} ${error}`,
131 type: 'error',
132 })
133 }
134 }
135 const updateReads = async (ids: number[], status: string) => {
136 try {
137 const response = await request('entries/batch-actions', {
138 method: 'POST',
139 data: {
140 column: 'status',
141 value: status,
142 ids,
143 },
144 })
145 const { data, error, status: httpStatus } = response
146 if (error || httpStatus >= 400 || !data?.success) {
147 IvyMessage({
148 message: `${getLabel('failed_to_update_entry_status')} ${data?.message || error}`,
149 type: 'error',
150 })
151 } else {
152 IvyMessage({
153 message: status === 'read' ? getLabel('entry_read') : getLabel('entry_unread'),
154 type: 'success',
155 })
156 }
157 } catch (error) {
158 IvyMessage({
159 message: `${getLabel('failed_to_update_entry_status')} ${error}`,
160 type: 'error',
161 })
162 }
163 }
164
165 const updateStarreds = async (ids: number[], starred: boolean) => {
166 try {
167 const response = await request('entries/batch-actions', {
168 method: 'POST',
169 data: {
170 column: 'starred',
171 value: starred ? 1 : 0,
172 ids,
173 },
174 })
175 const { data, error, status: httpStatus } = response
176 if (error || httpStatus >= 400 || !data?.success) {
177 IvyMessage({
178 message: starred
179 ? `${getLabel('failed_to_star_entry')} ${data?.message || error}`
180 : `${getLabel('failed_to_unstar_entry')} ${data?.message || error}`,
181 type: 'error',
182 })
183 } else {
184 IvyMessage({
185 message: starred ? getLabel('entry_starred') : getLabel('entry_unstarred'),
186 type: 'success',
187 })
188 }
189 } catch (error) {
190 IvyMessage({
191 message: starred
192 ? `${getLabel('failed_to_star_entry')} ${error}`
193 : `${getLabel('failed_to_unstar_entry')} ${error}`,
194 type: 'error',
195 })
196 }
197 }
198 const updateStatus = async (id: number, status: string) => {
199 try {
200 const {
201 data,
202 error,
203 status: httpStatus,
204 } = await request(`entry/update/status/${id}/`, {
205 method: 'POST',
206 data: {
207 value: status,
208 },
209 })
210
211 if (error || httpStatus >= 400) {
212 IvyMessage({
213 message: `${getLabel('failed_to_update_entry_status')} ${data?.message || error}`,
214 type: 'error',
215 })
216 } else {
217 IvyMessage({
218 message: status === 'read' ? getLabel('entry_read') : getLabel('entry_unread'),
219 type: 'success',
220 })
221 }
222 } catch (error) {
223 IvyMessage({
224 message: `${getLabel('failed_to_update_entry_status')} ${error}`,
225 type: 'error',
226 })
227 }
228 }
229
230 const reset = () => {
231 tableData.value = []
232 entryFields.value = []
233 paginationMeta.value = { page: 1, perPage: 10, total: 0 }
234 filterCount.value = {
235 readTrueCount: 0,
236 readFalseCount: 0,
237 starredTrueCount: 0,
238 starredFalseCount: 0,
239 }
240 }
241
242 return {
243 tableData,
244 entryFields,
245 paginationMeta,
246 filterCount,
247 formIdToName,
248 fetchData,
249 updateStarred,
250 updateStatus,
251 updateStarreds,
252 updateReads,
253 reset,
254 }
255 })
256