@knovator/pagecreator-admin 1.2.1 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/README.md +205 -205
  2. package/index.d.ts +1 -0
  3. package/package.json +38 -42
  4. package/src/{index.d.ts → index.ts} +1 -0
  5. package/src/lib/api/index.ts +70 -0
  6. package/src/lib/api/list.ts +59 -0
  7. package/src/lib/components/Page/AddButton/AddButton.tsx +16 -0
  8. package/src/lib/components/Page/AddButton/{index.d.ts → index.ts} +1 -0
  9. package/src/lib/components/Page/Form/PageForm.tsx +194 -0
  10. package/src/lib/components/Page/Form/{index.d.ts → index.ts} +1 -0
  11. package/src/lib/components/Page/Page/Page.tsx +151 -0
  12. package/src/lib/components/Page/Page/{index.d.ts → index.ts} +1 -0
  13. package/src/lib/components/Page/PageFormActions/PageFormActions.tsx +50 -0
  14. package/src/lib/components/Page/PageFormActions/{index.d.ts → index.ts} +1 -0
  15. package/src/lib/components/Page/PageFormWrapper/PageFormWrapper.tsx +16 -0
  16. package/src/lib/components/Page/PageFormWrapper/{index.d.ts → index.ts} +1 -0
  17. package/src/lib/components/Page/Pagination/PagePagination.tsx +26 -0
  18. package/src/lib/components/Page/Pagination/{index.d.ts → index.ts} +1 -0
  19. package/src/lib/components/Page/Search/PageSearch.tsx +32 -0
  20. package/src/lib/components/Page/Search/{index.d.ts → index.tsx} +1 -0
  21. package/src/lib/components/Page/Table/PageTable.tsx +36 -0
  22. package/src/lib/components/Page/Table/{index.d.ts → index.ts} +1 -0
  23. package/src/lib/components/Page/{index.d.ts → index.ts} +1 -0
  24. package/src/lib/components/Widget/AddButton/AddButton.tsx +16 -0
  25. package/src/lib/components/Widget/AddButton/{index.d.ts → index.ts} +1 -0
  26. package/src/lib/components/Widget/Form/ItemsAccordian.tsx +354 -0
  27. package/src/lib/components/Widget/Form/Tabs/TabItem.tsx +43 -0
  28. package/src/lib/components/Widget/Form/Tabs/Tabs.tsx +190 -0
  29. package/src/lib/components/Widget/Form/Tabs/{index.d.ts → index.ts} +1 -0
  30. package/src/lib/components/Widget/Form/WidgetForm.tsx +771 -0
  31. package/src/lib/components/Widget/Form/{index.d.ts → index.ts} +1 -0
  32. package/src/lib/components/Widget/Pagination/WidgetPagination.tsx +26 -0
  33. package/src/lib/components/Widget/Pagination/{index.d.ts → index.ts} +1 -0
  34. package/src/lib/components/Widget/Search/WidgetSearch.tsx +32 -0
  35. package/src/lib/components/Widget/Search/{index.d.ts → index.tsx} +1 -0
  36. package/src/lib/components/Widget/Table/WidgetTable.tsx +70 -0
  37. package/src/lib/components/Widget/Table/{index.d.ts → index.ts} +1 -0
  38. package/src/lib/components/Widget/Widget/Widget.tsx +173 -0
  39. package/src/lib/components/Widget/Widget/{index.d.ts → index.ts} +1 -0
  40. package/src/lib/components/Widget/WidgetFormActions/WidgetFormActions.tsx +50 -0
  41. package/src/lib/components/Widget/WidgetFormActions/{index.d.ts → index.ts} +1 -0
  42. package/src/lib/components/Widget/WidgetFormWrapper/WidgetFormWrapper.tsx +16 -0
  43. package/src/lib/components/Widget/WidgetFormWrapper/{index.d.ts → index.ts} +1 -0
  44. package/src/lib/components/Widget/{index.d.ts → index.tsx} +1 -0
  45. package/src/lib/components/common/Accordian/Accordian.tsx +56 -0
  46. package/src/lib/components/common/Accordian/{index.d.ts → index.ts} +1 -0
  47. package/src/lib/components/common/Button/Button.tsx +45 -0
  48. package/src/lib/components/common/Button/{index.d.ts → index.ts} +1 -0
  49. package/src/lib/components/common/ConfirmPopover/ConfirmPopover.tsx +47 -0
  50. package/src/lib/components/common/ConfirmPopover/{index.d.ts → index.ts} +1 -0
  51. package/src/lib/components/common/DNDItemsList/DNDItemsList.tsx +53 -0
  52. package/src/lib/components/common/DNDItemsList/{index.d.ts → index.ts} +1 -0
  53. package/src/lib/components/common/DeleteModal/DeleteModal.tsx +72 -0
  54. package/src/lib/components/common/DeleteModal/index.tsx +3 -0
  55. package/src/lib/components/common/Drawer/Drawer.tsx +79 -0
  56. package/src/lib/components/common/Drawer/{index.d.ts → index.ts} +1 -0
  57. package/src/lib/components/common/Form/Form.tsx +256 -0
  58. package/src/lib/components/common/Form/SimpleForm.tsx +314 -0
  59. package/src/lib/components/common/Form/{index.d.ts → index.ts} +1 -0
  60. package/src/lib/components/common/FormActions/FormActions.tsx +37 -0
  61. package/src/lib/components/common/FormActions/{index.d.ts → index.ts} +1 -0
  62. package/src/lib/components/common/ImageUpload/ImageUpload.tsx +113 -0
  63. package/src/lib/components/common/ImageUpload/index.tsx +3 -0
  64. package/src/lib/components/common/Input/Checkbox.tsx +25 -0
  65. package/src/lib/components/common/Input/Input.tsx +51 -0
  66. package/src/lib/components/common/Input/ReactSelect.tsx +60 -0
  67. package/src/lib/components/common/Input/Select.tsx +47 -0
  68. package/src/lib/components/common/Input/SrcSet.tsx +143 -0
  69. package/src/lib/components/common/Input/index.ts +20 -0
  70. package/src/lib/components/common/Modal/Modal.tsx +57 -0
  71. package/src/lib/components/common/Modal/index.tsx +3 -0
  72. package/src/lib/components/common/Pagination/Pagination.tsx +88 -0
  73. package/src/lib/components/common/Pagination/{index.d.ts → index.ts} +1 -0
  74. package/src/lib/components/common/Table/Table.tsx +133 -0
  75. package/src/lib/components/common/Table/{index.d.ts → index.ts} +1 -0
  76. package/src/lib/components/common/Toggle/Toggle.tsx +23 -0
  77. package/src/lib/components/common/Toggle/{index.d.ts → index.ts} +1 -0
  78. package/src/lib/constants/common.ts +145 -0
  79. package/src/lib/context/PageContext.tsx +100 -0
  80. package/src/lib/context/ProviderContext.tsx +52 -0
  81. package/src/lib/context/WidgetContext.tsx +127 -0
  82. package/src/lib/helper/utils.ts +46 -0
  83. package/src/lib/hooks/usePage.tsx +307 -0
  84. package/src/lib/hooks/usePagination.tsx +41 -0
  85. package/src/lib/hooks/useWidget.tsx +503 -0
  86. package/src/lib/icons/chevronDown.tsx +21 -0
  87. package/src/lib/icons/chevronLeft.tsx +20 -0
  88. package/src/lib/icons/chevronRight.tsx +20 -0
  89. package/src/lib/icons/chevronUp.tsx +21 -0
  90. package/src/lib/icons/close.tsx +21 -0
  91. package/src/lib/icons/pencil.tsx +21 -0
  92. package/src/lib/icons/plus.tsx +23 -0
  93. package/src/lib/icons/trash.tsx +21 -0
  94. package/src/lib/types/api.ts +44 -0
  95. package/src/lib/types/common.ts +31 -0
  96. package/src/lib/types/components.ts +427 -0
  97. package/src/lib/types/context.ts +184 -0
  98. package/src/styles/index.css +469 -0
  99. package/index.cjs +0 -7717
  100. package/index.css +0 -2258
  101. package/index.js +0 -7703
  102. package/src/lib/api/index.d.ts +0 -10
  103. package/src/lib/api/list.d.ts +0 -56
  104. package/src/lib/components/Page/AddButton/AddButton.d.ts +0 -3
  105. package/src/lib/components/Page/Form/PageForm.d.ts +0 -4
  106. package/src/lib/components/Page/Page/Page.d.ts +0 -13
  107. package/src/lib/components/Page/PageFormActions/PageFormActions.d.ts +0 -4
  108. package/src/lib/components/Page/PageFormWrapper/PageFormWrapper.d.ts +0 -4
  109. package/src/lib/components/Page/Pagination/PagePagination.d.ts +0 -3
  110. package/src/lib/components/Page/Search/PageSearch.d.ts +0 -3
  111. package/src/lib/components/Page/Table/PageTable.d.ts +0 -4
  112. package/src/lib/components/Widget/AddButton/AddButton.d.ts +0 -3
  113. package/src/lib/components/Widget/Form/ItemsAccordian.d.ts +0 -4
  114. package/src/lib/components/Widget/Form/Tabs/TabItem.d.ts +0 -4
  115. package/src/lib/components/Widget/Form/Tabs/Tabs.d.ts +0 -4
  116. package/src/lib/components/Widget/Form/WidgetForm.d.ts +0 -4
  117. package/src/lib/components/Widget/Pagination/WidgetPagination.d.ts +0 -3
  118. package/src/lib/components/Widget/Search/WidgetSearch.d.ts +0 -3
  119. package/src/lib/components/Widget/Table/WidgetTable.d.ts +0 -4
  120. package/src/lib/components/Widget/Widget/Widget.d.ts +0 -13
  121. package/src/lib/components/Widget/WidgetFormActions/WidgetFormActions.d.ts +0 -4
  122. package/src/lib/components/Widget/WidgetFormWrapper/WidgetFormWrapper.d.ts +0 -4
  123. package/src/lib/components/common/Accordian/Accordian.d.ts +0 -13
  124. package/src/lib/components/common/Button/Button.d.ts +0 -4
  125. package/src/lib/components/common/ConfirmPopover/ConfirmPopover.d.ts +0 -4
  126. package/src/lib/components/common/DNDItemsList/DNDItemsList.d.ts +0 -4
  127. package/src/lib/components/common/DeleteModal/DeleteModal.d.ts +0 -4
  128. package/src/lib/components/common/DeleteModal/index.d.ts +0 -2
  129. package/src/lib/components/common/Drawer/Drawer.d.ts +0 -9
  130. package/src/lib/components/common/Form/Form.d.ts +0 -15
  131. package/src/lib/components/common/Form/SimpleForm.d.ts +0 -18
  132. package/src/lib/components/common/FormActions/FormActions.d.ts +0 -10
  133. package/src/lib/components/common/ImageUpload/ImageUpload.d.ts +0 -4
  134. package/src/lib/components/common/ImageUpload/index.d.ts +0 -2
  135. package/src/lib/components/common/Input/Checkbox.d.ts +0 -4
  136. package/src/lib/components/common/Input/Input.d.ts +0 -4
  137. package/src/lib/components/common/Input/ReactSelect.d.ts +0 -4
  138. package/src/lib/components/common/Input/Select.d.ts +0 -4
  139. package/src/lib/components/common/Input/SrcSet.d.ts +0 -4
  140. package/src/lib/components/common/Input/index.d.ts +0 -12
  141. package/src/lib/components/common/Modal/Modal.d.ts +0 -8
  142. package/src/lib/components/common/Modal/index.d.ts +0 -2
  143. package/src/lib/components/common/Pagination/Pagination.d.ts +0 -4
  144. package/src/lib/components/common/Table/Table.d.ts +0 -4
  145. package/src/lib/components/common/Toggle/Toggle.d.ts +0 -4
  146. package/src/lib/constants/common.d.ts +0 -122
  147. package/src/lib/context/PageContext.d.ts +0 -7
  148. package/src/lib/context/ProviderContext.d.ts +0 -5
  149. package/src/lib/context/WidgetContext.d.ts +0 -7
  150. package/src/lib/helper/utils.d.ts +0 -10
  151. package/src/lib/hooks/usePage.d.ts +0 -43
  152. package/src/lib/hooks/usePagination.d.ts +0 -15
  153. package/src/lib/hooks/useWidget.d.ts +0 -46
  154. package/src/lib/icons/chevronDown.d.ts +0 -4
  155. package/src/lib/icons/chevronLeft.d.ts +0 -4
  156. package/src/lib/icons/chevronRight.d.ts +0 -4
  157. package/src/lib/icons/chevronUp.d.ts +0 -4
  158. package/src/lib/icons/close.d.ts +0 -4
  159. package/src/lib/icons/pencil.d.ts +0 -4
  160. package/src/lib/icons/plus.d.ts +0 -4
  161. package/src/lib/icons/trash.d.ts +0 -4
  162. package/src/lib/types/api.d.ts +0 -34
  163. package/src/lib/types/common.d.ts +0 -29
  164. package/src/lib/types/components.d.ts +0 -420
  165. package/src/lib/types/context.d.ts +0 -135
  166. /package/src/lib/types/{index.d.ts → index.ts} +0 -0
@@ -0,0 +1,503 @@
1
+ import { useCallback, useEffect, useState } from 'react';
2
+ import { CALLBACK_CODES, INTERNAL_ERROR_CODE } from '../constants/common';
3
+ import { useProviderState } from '../context/ProviderContext';
4
+ import { paginationDataGatter, dataGatter, build_path } from '../helper/utils';
5
+ import usePagination from './usePagination';
6
+ import request, { getApiType } from '../api';
7
+ import { Routes_Input, WidgetType, ItemsType } from '../types';
8
+ import { FormActionTypes, ObjectType, LanguageType } from '../types/common';
9
+
10
+ interface UseWidgetProps {
11
+ canList?: boolean;
12
+ defaultLimit: number;
13
+ routes?: Routes_Input;
14
+ preConfirmDelete?: (data: { row: ObjectType }) => Promise<boolean>;
15
+ imageBaseUrl?: string;
16
+ }
17
+
18
+ const useWidget = ({
19
+ canList = true,
20
+ defaultLimit,
21
+ routes,
22
+ preConfirmDelete,
23
+ imageBaseUrl,
24
+ }: UseWidgetProps) => {
25
+ const [list, setList] = useState<ObjectType[]>([]);
26
+ const [loading, setLoading] = useState(false);
27
+ const [totalPages, setTotalPages] = useState(0);
28
+ const [totalRecords, setTotalRecords] = useState(0);
29
+ const [languages, setLanguages] = useState<LanguageType[]>([]);
30
+ const [itemData, setItemData] = useState<ObjectType | null>(null);
31
+ const [formState, setFormState] = useState<FormActionTypes>();
32
+ const [itemsTypes, setItemsTypes] = useState<ItemsType[]>([]);
33
+ const [widgetTypes, setWidgetTypes] = useState<WidgetType[]>([]);
34
+ const [collectionDataLoading, setCollectionDataLoading] =
35
+ useState<boolean>(false);
36
+ const [collectionData, setCollectionData] = useState<any[]>([]);
37
+
38
+ const { baseUrl, token, onError, onSuccess, onLogout, widgetRoutesPrefix } =
39
+ useProviderState();
40
+ const {
41
+ changeSearch,
42
+ setPageSize,
43
+ pageSize,
44
+ limitRef,
45
+ currentPageRef,
46
+ setCurrentPage,
47
+ offsetRef,
48
+ searchRef,
49
+ } = usePagination({ defaultLimit });
50
+
51
+ const handleError = useCallback(
52
+ (code: CALLBACK_CODES) => (error: any) => {
53
+ const { data = {} } = error?.response || {};
54
+ if (data?.code === 'UNAUTHENTICATED') {
55
+ onLogout();
56
+ }
57
+ onError(code, 'error', data?.message);
58
+ },
59
+ [onError, onLogout]
60
+ );
61
+ // List operations
62
+ const getWidgets = useCallback(
63
+ async (search?: string) => {
64
+ try {
65
+ setLoading(true);
66
+ const api = getApiType({
67
+ routes,
68
+ action: 'LIST',
69
+ prefix: widgetRoutesPrefix,
70
+ });
71
+ const response = await request({
72
+ baseUrl,
73
+ token,
74
+ method: api.method,
75
+ url: api.url,
76
+ onError: handleError(CALLBACK_CODES.GET_ALL),
77
+ data: {
78
+ search,
79
+ options: {
80
+ offset: offsetRef.current,
81
+ limit: limitRef.current,
82
+ page: currentPageRef.current,
83
+ },
84
+ },
85
+ });
86
+ if (response?.code === 'SUCCESS') {
87
+ setLoading(false);
88
+ setTotalPages(response.data.totalPages);
89
+ setTotalRecords(response.data.totalDocs);
90
+ return setList(paginationDataGatter(response));
91
+ }
92
+ setLoading(false);
93
+ } catch (error) {
94
+ setLoading(false);
95
+ }
96
+ },
97
+ [
98
+ baseUrl,
99
+ currentPageRef,
100
+ limitRef,
101
+ offsetRef,
102
+ handleError,
103
+ routes,
104
+ token,
105
+ widgetRoutesPrefix,
106
+ ]
107
+ );
108
+ const getLanguagesList = useCallback(async () => {
109
+ const api = getApiType({
110
+ routes,
111
+ action: 'LANGUAGES',
112
+ prefix: widgetRoutesPrefix,
113
+ });
114
+ const response = await request({
115
+ baseUrl,
116
+ token,
117
+ method: api.method,
118
+ url: api.url,
119
+ onError: handleError(CALLBACK_CODES.GET_ALL),
120
+ });
121
+ if (response?.code === 'SUCCESS') {
122
+ setLanguages(response.data);
123
+ return response.data;
124
+ }
125
+ }, [baseUrl, handleError, widgetRoutesPrefix, routes, token]);
126
+
127
+ const onCofirmDeleteWidget = async () => {
128
+ try {
129
+ let proceed = true;
130
+ if (typeof preConfirmDelete === 'function' && itemData) {
131
+ try {
132
+ proceed = await preConfirmDelete({ row: itemData });
133
+ } catch (error) {
134
+ proceed = false;
135
+ }
136
+ }
137
+
138
+ if (proceed) {
139
+ setLoading(true);
140
+ const api = getApiType({
141
+ routes,
142
+ action: 'DELETE',
143
+ prefix: widgetRoutesPrefix,
144
+ id: itemData?.['_id'],
145
+ });
146
+ const response = await request({
147
+ baseUrl,
148
+ token,
149
+ method: api.method,
150
+ url: api.url,
151
+ onError: handleError(CALLBACK_CODES.DELETE),
152
+ });
153
+ if (response?.code === 'SUCCESS') {
154
+ setLoading(false);
155
+ onSuccess(CALLBACK_CODES.DELETE, response?.code, response?.message);
156
+ getWidgets();
157
+ onCloseForm();
158
+ return;
159
+ }
160
+ setLoading(false);
161
+ onError(CALLBACK_CODES.DELETE, response?.code, response?.message);
162
+ onCloseForm();
163
+ }
164
+ } catch (error) {
165
+ setLoading(false);
166
+ onError(
167
+ CALLBACK_CODES.DELETE,
168
+ INTERNAL_ERROR_CODE,
169
+ (error as Error).message
170
+ );
171
+ onCloseForm();
172
+ }
173
+ };
174
+ const onPartialUpdateWidget = async (data: ObjectType, id: string) => {
175
+ try {
176
+ const api = getApiType({
177
+ routes,
178
+ action: 'PARTIAL_UPDATE',
179
+ prefix: widgetRoutesPrefix,
180
+ id,
181
+ });
182
+ const response = await request({
183
+ baseUrl,
184
+ token,
185
+ data,
186
+ url: api.url,
187
+ method: api.method,
188
+ onError: handleError(CALLBACK_CODES.PARTIAL_UPDATE),
189
+ });
190
+ if (response?.code === 'SUCCESS') {
191
+ setList((oldListData) =>
192
+ oldListData.map((item) => (item['_id'] === id ? response.data : item))
193
+ );
194
+ if (response.message)
195
+ onSuccess(
196
+ CALLBACK_CODES.PARTIAL_UPDATE,
197
+ response?.code,
198
+ response?.message
199
+ );
200
+ } else {
201
+ onError(
202
+ CALLBACK_CODES.PARTIAL_UPDATE,
203
+ response?.code,
204
+ response?.message
205
+ );
206
+ }
207
+ } catch (error) {
208
+ onError(
209
+ CALLBACK_CODES.PARTIAL_UPDATE,
210
+ INTERNAL_ERROR_CODE,
211
+ (error as Error).message
212
+ );
213
+ }
214
+ };
215
+ const getWidgetsTypes = async () => {
216
+ if (itemsTypes?.length > 0) return;
217
+ setLoading(true);
218
+ const api = getApiType({
219
+ routes,
220
+ action: 'WIDGET_TYPES',
221
+ prefix: widgetRoutesPrefix,
222
+ });
223
+ const response = await request({
224
+ baseUrl,
225
+ token,
226
+ method: api.method,
227
+ url: api.url,
228
+ onError: handleError(CALLBACK_CODES.GET_ALL),
229
+ });
230
+ if (response?.code === 'SUCCESS') {
231
+ setLoading(false);
232
+ return setItemsTypes(dataGatter(response));
233
+ }
234
+ setLoading(false);
235
+ };
236
+ const getWidgetTypes = async () => {
237
+ if (widgetTypes?.length > 0) return;
238
+ setLoading(true);
239
+ const api = getApiType({
240
+ routes,
241
+ action: 'SELECTION_TYPES',
242
+ prefix: widgetRoutesPrefix,
243
+ });
244
+ const response = await request({
245
+ baseUrl,
246
+ token,
247
+ method: api.method,
248
+ url: api.url,
249
+ onError: handleError(CALLBACK_CODES.GET_ALL),
250
+ });
251
+ if (response?.code === 'SUCCESS') {
252
+ setLoading(false);
253
+ return setWidgetTypes(dataGatter(response));
254
+ }
255
+ setLoading(false);
256
+ };
257
+ const getCollectionData = async (
258
+ collectionName: string,
259
+ search?: string,
260
+ callback?: (data: any) => void,
261
+ collectionItems?: string[]
262
+ ) => {
263
+ setCollectionDataLoading(true);
264
+ const api = getApiType({
265
+ routes,
266
+ action: 'COLLECTION_DATA',
267
+ prefix: widgetRoutesPrefix,
268
+ id: collectionName,
269
+ });
270
+ const response = await request({
271
+ baseUrl,
272
+ token,
273
+ method: api.method,
274
+ url: api.url,
275
+ onError: handleError(CALLBACK_CODES.GET_ALL),
276
+ data: {
277
+ search: search || '',
278
+ collectionName,
279
+ collectionItems: collectionItems || [],
280
+ },
281
+ });
282
+ if (response?.code === 'SUCCESS') {
283
+ setCollectionDataLoading(false);
284
+ if (typeof callback === 'function')
285
+ callback(paginationDataGatter(response));
286
+ return setCollectionData(paginationDataGatter(response));
287
+ }
288
+ setCollectionDataLoading(false);
289
+ };
290
+ // Form operations
291
+ const onWidgetFormSubmit = async (data: ObjectType) => {
292
+ try {
293
+ setLoading(true);
294
+ const code =
295
+ formState === 'ADD' ? CALLBACK_CODES.CREATE : CALLBACK_CODES.UPDATE;
296
+ const api = getApiType({
297
+ routes,
298
+ action: formState === 'ADD' ? 'CREATE' : 'UPDATE',
299
+ prefix: widgetRoutesPrefix,
300
+ id: itemData?.['_id'],
301
+ });
302
+ const response = await request({
303
+ baseUrl,
304
+ token,
305
+ data,
306
+ url: api.url,
307
+ method: api.method,
308
+ onError: handleError(code),
309
+ });
310
+ if (response?.code === 'SUCCESS') {
311
+ if (formState === 'ADD') {
312
+ setCurrentPage(1);
313
+ }
314
+ setLoading(false);
315
+ onSuccess(code, response?.code, response?.message);
316
+ getWidgets();
317
+ onCloseForm();
318
+ }
319
+ } catch (error) {
320
+ setLoading(false);
321
+ }
322
+ };
323
+ const onCloseForm = () => {
324
+ setFormState(undefined);
325
+ setItemData(null);
326
+ };
327
+ const getAndSetWidget = async (id: string) => {
328
+ try {
329
+ setLoading(true);
330
+ const api = getApiType({
331
+ routes,
332
+ action: 'GET_ONE',
333
+ prefix: widgetRoutesPrefix,
334
+ id,
335
+ });
336
+ const response = await request({
337
+ baseUrl,
338
+ token,
339
+ url: api.url,
340
+ method: api.method,
341
+ onError: handleError(CALLBACK_CODES.GET_SINGLE),
342
+ });
343
+ if (response?.code === 'SUCCESS') {
344
+ setLoading(false);
345
+ const data = response?.data;
346
+ if (Array.isArray(data.items)) {
347
+ const items = JSON.parse(JSON.stringify(data.items));
348
+ data.webItems = items.filter((item: any) => item.itemType === 'Web');
349
+ data.mobileItems = items.filter(
350
+ (item: any) => item.itemType === 'Mobile'
351
+ );
352
+ delete data.items;
353
+ }
354
+ setItemData(data);
355
+ }
356
+ } catch (error) {
357
+ setLoading(false);
358
+ onError(CALLBACK_CODES.UPDATE, '', (error as Error).message);
359
+ }
360
+ };
361
+ const onChangeFormState = async (
362
+ state: FormActionTypes,
363
+ data?: ObjectType
364
+ ) => {
365
+ setFormState(state);
366
+ // fetch ItemsTypes & WidgetTypes if needed
367
+ if (state === 'ADD' || state === 'UPDATE') {
368
+ getWidgetsTypes();
369
+ getWidgetTypes();
370
+ }
371
+ // get Item data if widget is updating
372
+ if (state === 'UPDATE' && data) {
373
+ getAndSetWidget(data['_id']);
374
+ } else if (state === 'ADD') {
375
+ // reset Item data if widget is adding
376
+ setItemData(null);
377
+ } else if (state === 'DELETE' && data) {
378
+ setItemData(data);
379
+ setFormState(state);
380
+ }
381
+ };
382
+ // Image Upload operations
383
+ const onImageUpload = async (
384
+ file: File
385
+ ): Promise<{ fileUrl: string; fileId: string; fileUri: string } | void> => {
386
+ try {
387
+ const payload = new FormData();
388
+ payload?.append('folder', 'images');
389
+ payload?.append('file', file, file.name);
390
+ const api = getApiType({
391
+ routes,
392
+ action: 'IMAGE_UPLOAD',
393
+ prefix: 'media',
394
+ });
395
+ const response = await request({
396
+ data: payload,
397
+ baseUrl,
398
+ token,
399
+ method: api.method,
400
+ url: api.url,
401
+ config: {
402
+ contentType: 'multipart/form-data',
403
+ },
404
+ onError: handleError(CALLBACK_CODES.IMAGE_UPLOAD),
405
+ });
406
+ if (response.code === 'SUCCESS') {
407
+ const responseData = response?.data[0] || response?.data;
408
+ return {
409
+ fileId: responseData?._id || responseData?.id,
410
+ fileUrl: build_path(
411
+ imageBaseUrl ? imageBaseUrl : baseUrl,
412
+ responseData?.uri
413
+ ),
414
+ fileUri: responseData?.uri,
415
+ };
416
+ } else
417
+ onError(CALLBACK_CODES.IMAGE_REMOVE, response.code, response.message);
418
+ } catch (error) {
419
+ onError(
420
+ CALLBACK_CODES.IMAGE_REMOVE,
421
+ INTERNAL_ERROR_CODE,
422
+ (error as Error).message
423
+ );
424
+ }
425
+ };
426
+ const onImageRemove = async (id: string): Promise<void> => {
427
+ try {
428
+ const api = getApiType({
429
+ routes,
430
+ action: 'IMAGE_REMOVE',
431
+ prefix: 'media',
432
+ id,
433
+ });
434
+ const response = await request({
435
+ baseUrl,
436
+ token,
437
+ method: api.method,
438
+ url: api.url,
439
+ onError: handleError(CALLBACK_CODES.IMAGE_REMOVE),
440
+ });
441
+ if (response?.code === 'SUCCESS') {
442
+ onSuccess(CALLBACK_CODES.IMAGE_REMOVE, response.code, response.message);
443
+ } else {
444
+ onError(CALLBACK_CODES.IMAGE_REMOVE, response.code, response.message);
445
+ }
446
+ } catch (error) {
447
+ onError(
448
+ CALLBACK_CODES.IMAGE_REMOVE,
449
+ INTERNAL_ERROR_CODE,
450
+ (error as Error).message
451
+ );
452
+ }
453
+ };
454
+ const changeCurrentPage = (page: number) => {
455
+ setCurrentPage(page);
456
+ getWidgets(searchRef.current);
457
+ };
458
+
459
+ useEffect(() => {
460
+ if (canList) getWidgets();
461
+ // eslint-disable-next-line react-hooks/exhaustive-deps
462
+ }, [canList]);
463
+
464
+ useEffect(() => {
465
+ getLanguagesList();
466
+ }, [getLanguagesList]);
467
+
468
+ return {
469
+ list,
470
+ getWidgets,
471
+ loading,
472
+ languages,
473
+ setLoading,
474
+
475
+ // Pagination
476
+ searchText: searchRef.current,
477
+ pageSize,
478
+ totalPages,
479
+ currentPage: currentPageRef.current,
480
+ totalRecords,
481
+ setCurrentPage: changeCurrentPage,
482
+ setPageSize,
483
+
484
+ // Form
485
+ formState,
486
+ itemData,
487
+ onChangeFormState,
488
+ onCloseForm,
489
+ onWidgetFormSubmit,
490
+ onCofirmDeleteWidget,
491
+ onPartialUpdateWidget,
492
+ onImageUpload,
493
+ onImageRemove,
494
+ itemsTypes,
495
+ widgetTypes,
496
+ changeSearch,
497
+ collectionDataLoading,
498
+ getCollectionData,
499
+ collectionData,
500
+ };
501
+ };
502
+
503
+ export default useWidget;
@@ -0,0 +1,21 @@
1
+ import React from 'react';
2
+ import { IconProps } from '../types';
3
+
4
+ const ChevronDown = ({ srText, className }: IconProps) => {
5
+ return (
6
+ <>
7
+ {srText ? <span className="khb_sr-only">{srText}</span> : null}
8
+ <svg
9
+ xmlns="http://www.w3.org/2000/svg"
10
+ viewBox="-5 -8 24 24"
11
+ width="24"
12
+ fill="currentColor"
13
+ className={className}
14
+ >
15
+ <path d="M7.071 5.314l4.95-4.95a1 1 0 1 1 1.414 1.414L7.778 7.435a1 1 0 0 1-1.414 0L.707 1.778A1 1 0 1 1 2.121.364l4.95 4.95z"></path>
16
+ </svg>
17
+ </>
18
+ );
19
+ };
20
+
21
+ export default ChevronDown;
@@ -0,0 +1,20 @@
1
+ import React from 'react';
2
+ import { IconProps } from '../types';
3
+
4
+ const ChevronLeft = ({ srText }: IconProps) => {
5
+ return (
6
+ <>
7
+ {srText ? <span className="khb_sr-only">{srText}</span> : null}
8
+ <svg
9
+ xmlns="http://www.w3.org/2000/svg"
10
+ viewBox="-8 -5 24 24"
11
+ width="24"
12
+ fill="currentColor"
13
+ >
14
+ <path d="M2.757 7l4.95 4.95a1 1 0 1 1-1.414 1.414L.636 7.707a1 1 0 0 1 0-1.414L6.293.636A1 1 0 0 1 7.707 2.05L2.757 7z"></path>
15
+ </svg>
16
+ </>
17
+ );
18
+ };
19
+
20
+ export default ChevronLeft;
@@ -0,0 +1,20 @@
1
+ import React from 'react';
2
+ import { IconProps } from '../types';
3
+
4
+ const ChevronRight = ({ srText }: IconProps) => {
5
+ return (
6
+ <>
7
+ {srText ? <span className="khb_sr-only">{srText}</span> : null}
8
+ <svg
9
+ xmlns="http://www.w3.org/2000/svg"
10
+ viewBox="-8 -5 24 24"
11
+ width="24"
12
+ fill="currentColor"
13
+ >
14
+ <path d="M5.314 7.071l-4.95-4.95A1 1 0 0 1 1.778.707l5.657 5.657a1 1 0 0 1 0 1.414l-5.657 5.657a1 1 0 0 1-1.414-1.414l4.95-4.95z"></path>
15
+ </svg>
16
+ </>
17
+ );
18
+ };
19
+
20
+ export default ChevronRight;
@@ -0,0 +1,21 @@
1
+ import React from 'react';
2
+ import { IconProps } from '../types';
3
+
4
+ const ChevronUp = ({ srText, className }: IconProps) => {
5
+ return (
6
+ <>
7
+ {srText ? <span className="khb_sr-only">{srText}</span> : null}
8
+ <svg
9
+ xmlns="http://www.w3.org/2000/svg"
10
+ viewBox="-5 -7.5 24 24"
11
+ width="24"
12
+ fill="currentColor"
13
+ className={className}
14
+ >
15
+ <path d="M7.071 2.828l-4.95 4.95A1 1 0 0 1 .707 6.364L6.364.707a1 1 0 0 1 1.414 0l5.657 5.657a1 1 0 0 1-1.414 1.414l-4.95-4.95z"></path>
16
+ </svg>
17
+ </>
18
+ );
19
+ };
20
+
21
+ export default ChevronUp;
@@ -0,0 +1,21 @@
1
+ import React from 'react';
2
+ import { IconProps } from '../types';
3
+
4
+ const Close = ({ srText, className }: IconProps) => {
5
+ return (
6
+ <>
7
+ {srText ? <span className="khb_sr-only">{srText}</span> : null}
8
+ <svg
9
+ className={className}
10
+ xmlns="http://www.w3.org/2000/svg"
11
+ viewBox="-6 -6 24 24"
12
+ width="24"
13
+ fill="currentColor"
14
+ >
15
+ <path d="M7.314 5.9l3.535-3.536A1 1 0 1 0 9.435.95L5.899 4.485 2.364.95A1 1 0 1 0 .95 2.364l3.535 3.535L.95 9.435a1 1 0 1 0 1.414 1.414l3.535-3.535 3.536 3.535a1 1 0 1 0 1.414-1.414L7.314 5.899z"></path>
16
+ </svg>
17
+ </>
18
+ );
19
+ };
20
+
21
+ export default Close;
@@ -0,0 +1,21 @@
1
+ import React from 'react';
2
+ import { IconProps } from '../types';
3
+
4
+ const Pencil = ({ srText, className }: IconProps) => {
5
+ return (
6
+ <>
7
+ {srText ? <span className="khb_sr-only">{srText}</span> : null}
8
+ <svg
9
+ xmlns="http://www.w3.org/2000/svg"
10
+ viewBox="-2.5 -2.5 24 24"
11
+ width="24"
12
+ fill="currentColor"
13
+ className={className}
14
+ >
15
+ <path d="M12.238 5.472L3.2 14.51l-.591 2.016 1.975-.571 9.068-9.068-1.414-1.415zM13.78 3.93l1.414 1.414 1.318-1.318a.5.5 0 0 0 0-.707l-.708-.707a.5.5 0 0 0-.707 0L13.781 3.93zm3.439-2.732l.707.707a2.5 2.5 0 0 1 0 3.535L5.634 17.733l-4.22 1.22a1 1 0 0 1-1.237-1.241l1.248-4.255 12.26-12.26a2.5 2.5 0 0 1 3.535 0z"></path>
16
+ </svg>
17
+ </>
18
+ );
19
+ };
20
+
21
+ export default Pencil;
@@ -0,0 +1,23 @@
1
+ import React from 'react';
2
+ import { IconProps } from '../types';
3
+
4
+ const Plus = ({ srText, className }: IconProps) => {
5
+ return (
6
+ <>
7
+ {srText ? <span className="khb_sr-only">{srText}</span> : null}
8
+ <svg
9
+ xmlns="http://www.w3.org/2000/svg"
10
+ fill="currentColor"
11
+ width="24"
12
+ viewBox="0 0 24 24"
13
+ strokeWidth={1.5}
14
+ stroke="currentColor"
15
+ className={className}
16
+ >
17
+ <path strokeLinecap="round" strokeLinejoin="round" d="M12 6v12m6-6H6" />
18
+ </svg>
19
+ </>
20
+ );
21
+ };
22
+
23
+ export default Plus;
@@ -0,0 +1,21 @@
1
+ import React from 'react';
2
+ import { IconProps } from '../types';
3
+
4
+ const Trash = ({ srText, className }: IconProps) => {
5
+ return (
6
+ <>
7
+ {srText ? <span className="khb_sr-only">{srText}</span> : null}
8
+ <svg
9
+ className={className}
10
+ xmlns="http://www.w3.org/2000/svg"
11
+ viewBox="-4 -2 24 24"
12
+ width="24"
13
+ fill="currentColor"
14
+ >
15
+ <path d="M14.833 5l-.728 13.11A2 2 0 0 1 12.108 20H3.892a2 2 0 0 1-1.997-1.89L1.167 5H1a1 1 0 0 1-1-1V1a1 1 0 0 1 1-1h14a1 1 0 0 1 1 1v3a1 1 0 0 1-1 1h-.167zM12.83 5H3.17l.722 13h8.216l.722-13zM2 2v1h12V2H2zm4 5a1 1 0 0 1 1 1v7a1 1 0 0 1-2 0V8a1 1 0 0 1 1-1zm4 0a1 1 0 0 1 1 1v7a1 1 0 0 1-2 0V8a1 1 0 0 1 1-1z"></path>
16
+ </svg>
17
+ </>
18
+ );
19
+ };
20
+
21
+ export default Trash;