PluginProbe
The Innovative Form Builder – IvyForms / 1.1.1
The Innovative Form Builder – IvyForms v1.1.1
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 / useAllForms.ts

useAllForms.ts in The Innovative Form Builder – IvyForms 1.1.1, at frontend/src/stores/useAllForms.ts

213 lines 5.5 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 forms table data and actions.
9 */
10 export const useAllForms = defineStore('allForms', () => {
11 const tableData = ref([])
12 const paginationMeta = ref({
13 page: 1,
14 perPage: 10,
15 total: 0,
16 })
17 const filterCount = ref({
18 publishedTrueCount: 0,
19 publishedFalseCount: 0,
20 starredTrueCount: 0,
21 starredFalseCount: 0,
22 })
23 const entryCount = ref<Record<number, number>>({})
24 const { getLabel } = useLabels()
25 const { request } = useApiClient()
26
27 /**
28 * Fetch forms data from the API with validation and always update meta.
29 * @param options - Fetch options
30 */
31 const fetchData = async ({
32 page = 1,
33 perPage = 10,
34 search = '',
35 sortBy = '',
36 sortOrder = 'desc',
37 dateRange = [],
38 filters = {},
39 shouldGetCount = true,
40 }: {
41 page?: number
42 perPage?: number
43 search?: string
44 sortBy?: string
45 sortOrder?: string
46 dateRange?: Array<string>
47 filters?: Record<string, string | number | boolean>
48 shouldGetCount?: boolean
49 } = {}) => {
50 try {
51 const params: Record<
52 string,
53 string | number | boolean | null | string[] | Record<string, string | number | boolean>
54 > = {
55 page,
56 perPage,
57 search,
58 orderBy: sortBy,
59 order: sortOrder,
60 dateRange,
61 filters,
62 shouldGetCount,
63 }
64 const { data, error } = await request('forms/search', {
65 method: 'GET',
66 params,
67 })
68 if (error) {
69 IvyMessage({
70 message: `${getLabel('error_fetching_forms')} ${error}`,
71 type: 'error',
72 })
73 return
74 }
75
76 const response = data.data
77 tableData.value = normalizeData(response.data.data)
78 paginationMeta.value = response.data.meta
79 if (shouldGetCount) {
80 filterCount.value = response.data.filterCount
81 }
82 // Update entryCount from response
83 if (response.data.entryCounts) {
84 Object.entries(response.data.entryCounts).forEach(([id, count]) => {
85 entryCount.value[Number(id)] = count as number
86 })
87 }
88 } catch (error) {
89 IvyMessage({
90 message: `${getLabel('error_fetching_forms')} ${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(`form/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_form')}: ${data?.message || error}`
110 : `${getLabel('failed_to_unstar_form')}: ${data?.message || error}`,
111 type: 'error',
112 })
113 } else {
114 IvyMessage({
115 message: starred ? getLabel('form_starred') : getLabel('form_unstarred'),
116 type: 'success',
117 })
118 }
119 } catch (error) {
120 IvyMessage({
121 message: starred
122 ? `${getLabel('failed_to_star_form')} ${error}`
123 : `${getLabel('failed_to_unstar_form')} ${error}`,
124 type: 'error',
125 })
126 }
127 }
128
129 const updateStarreds = async (ids: number[], starred: boolean) => {
130 try {
131 const { data, error, status } = await request('/forms/batch-actions', {
132 method: 'POST',
133 data: {
134 column: 'starred',
135 value: starred ? 1 : 0,
136 ids,
137 },
138 })
139 if (error || status >= 400 || !data?.success) {
140 IvyMessage({
141 message: starred
142 ? `${getLabel('failed_to_star_form')}: ${data?.message || error}`
143 : `${getLabel('failed_to_unstar_form')}: ${data?.message || error}`,
144 type: 'error',
145 })
146 } else {
147 IvyMessage({
148 message: starred ? getLabel('form_starred') : getLabel('form_unstarred'),
149 type: 'success',
150 })
151 }
152 } catch (error) {
153 IvyMessage({
154 message: starred
155 ? `${getLabel('failed_to_star_form')} ${error}`
156 : `${getLabel('failed_to_unstar_form')} ${error}`,
157 type: 'error',
158 })
159 }
160 }
161
162 const updateStatus = async (id: number, status: boolean) => {
163 try {
164 const {
165 data,
166 error,
167 status: httpStatus,
168 } = await request(`form/update/status/${id}/`, {
169 method: 'POST',
170 data: {
171 value: status,
172 },
173 })
174
175 if (error || httpStatus >= 400) {
176 IvyMessage({
177 message: `${getLabel('failed_to_update_form')} ${data?.message || error}`,
178 type: 'error',
179 })
180 } else {
181 IvyMessage({
182 message: status ? getLabel('form_published') : getLabel('form_unpublished'),
183 type: 'success',
184 })
185 }
186 } catch (error) {
187 IvyMessage({
188 message: `${getLabel('error_updating_forms')} ${error}`,
189 type: 'error',
190 })
191 }
192 }
193
194 const normalizeData = (data: Array<{ starred: string; published: string }>) => {
195 return data.map((item) => ({
196 ...item,
197 starred: item.starred === '1',
198 published: item.published === '1',
199 }))
200 }
201
202 return {
203 tableData,
204 paginationMeta,
205 filterCount,
206 fetchData,
207 updateStarred,
208 updateStarreds,
209 updateStatus,
210 entryCount,
211 }
212 })
213