PluginProbe
The Innovative Form Builder – IvyForms / 0.3
The Innovative Form Builder – IvyForms v0.3
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 0.3, at frontend/src/stores/useAllForms.ts

172 lines 4.3 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 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 const entryCount = ref<Record<number, number>>({})
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 = true,
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 if (shouldGetCount) {
73 filterCount.value = response.data.filterCount
74 }
75 // Update entryCount from response
76 if (response.data.entryCounts) {
77 Object.entries(response.data.entryCounts).forEach(([id, count]) => {
78 entryCount.value[Number(id)] = count as number
79 })
80 }
81 } catch (error) {
82 IvyMessage({
83 message: `${getLabel('error_fetching_forms')} ${error}`,
84 type: 'error',
85 })
86 }
87 }
88
89 const updateStarred = async (id: number, starred: boolean) => {
90 try {
91 const { data, error, status } = await request(`form/update/starred/${id}/`, {
92 method: 'POST',
93 data: {
94 attribute: 'starred',
95 value: starred,
96 },
97 })
98
99 if (error || status >= 400) {
100 IvyMessage({
101 message: starred
102 ? `${getLabel('failed_to_star_form')}: ${data?.message || error}`
103 : `${getLabel('failed_to_unstar_form')}: ${data?.message || error}`,
104 type: 'error',
105 })
106 } else {
107 IvyMessage({
108 message: starred ? getLabel('form_starred') : getLabel('form_unstarred'),
109 type: 'success',
110 })
111 }
112 } catch (error) {
113 IvyMessage({
114 message: starred
115 ? `${getLabel('failed_to_star_form')} ${error}`
116 : `${getLabel('failed_to_unstar_form')} ${error}`,
117 type: 'error',
118 })
119 }
120 }
121
122 const updateStatus = async (id: number, status: boolean) => {
123 try {
124 const {
125 data,
126 error,
127 status: httpStatus,
128 } = await request(`form/update/status/${id}/`, {
129 method: 'POST',
130 data: {
131 value: status,
132 },
133 })
134
135 if (error || httpStatus >= 400) {
136 IvyMessage({
137 message: `${getLabel('failed_to_update_form')} ${data?.message || error}`,
138 type: 'error',
139 })
140 } else {
141 IvyMessage({
142 message: status ? getLabel('form_published') : getLabel('form_unpublished'),
143 type: 'success',
144 })
145 }
146 } catch (error) {
147 IvyMessage({
148 message: `${getLabel('error_updating_forms')} ${error}`,
149 type: 'error',
150 })
151 }
152 }
153
154 const normalizeData = (data: Array<{ starred: string; published: string }>) => {
155 return data.map((item) => ({
156 ...item,
157 starred: item.starred === '1',
158 published: item.published === '1',
159 }))
160 }
161
162 return {
163 tableData,
164 paginationMeta,
165 filterCount,
166 fetchData,
167 updateStarred,
168 updateStatus,
169 entryCount,
170 }
171 })
172