@knovator/pagecreator-admin 1.2.6 → 1.2.7
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.
- package/index.d.ts +1 -0
- package/package.json +38 -42
- package/src/{index.d.ts → index.ts} +1 -0
- package/src/lib/api/index.ts +70 -0
- package/src/lib/api/list.ts +59 -0
- package/src/lib/components/Page/AddButton/AddButton.tsx +16 -0
- package/src/lib/components/Page/AddButton/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Page/Form/PageForm.tsx +194 -0
- package/src/lib/components/Page/Form/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Page/Page/Page.tsx +151 -0
- package/src/lib/components/Page/Page/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Page/PageFormActions/PageFormActions.tsx +50 -0
- package/src/lib/components/Page/PageFormActions/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Page/PageFormWrapper/PageFormWrapper.tsx +16 -0
- package/src/lib/components/Page/PageFormWrapper/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Page/Pagination/PagePagination.tsx +26 -0
- package/src/lib/components/Page/Pagination/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Page/Search/PageSearch.tsx +32 -0
- package/src/lib/components/Page/Search/{index.d.ts → index.tsx} +1 -0
- package/src/lib/components/Page/Table/PageTable.tsx +36 -0
- package/src/lib/components/Page/Table/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Page/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Widget/AddButton/AddButton.tsx +16 -0
- package/src/lib/components/Widget/AddButton/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Widget/Form/ItemsAccordian.tsx +354 -0
- package/src/lib/components/Widget/Form/Tabs/TabItem.tsx +43 -0
- package/src/lib/components/Widget/Form/Tabs/Tabs.tsx +190 -0
- package/src/lib/components/Widget/Form/Tabs/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Widget/Form/WidgetForm.tsx +771 -0
- package/src/lib/components/Widget/Form/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Widget/Pagination/WidgetPagination.tsx +26 -0
- package/src/lib/components/Widget/Pagination/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Widget/Search/WidgetSearch.tsx +32 -0
- package/src/lib/components/Widget/Search/{index.d.ts → index.tsx} +1 -0
- package/src/lib/components/Widget/Table/WidgetTable.tsx +70 -0
- package/src/lib/components/Widget/Table/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Widget/Widget/Widget.tsx +173 -0
- package/src/lib/components/Widget/Widget/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Widget/WidgetFormActions/WidgetFormActions.tsx +50 -0
- package/src/lib/components/Widget/WidgetFormActions/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Widget/WidgetFormWrapper/WidgetFormWrapper.tsx +16 -0
- package/src/lib/components/Widget/WidgetFormWrapper/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/Widget/{index.d.ts → index.tsx} +1 -0
- package/src/lib/components/common/Accordian/Accordian.tsx +56 -0
- package/src/lib/components/common/Accordian/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/common/Button/Button.tsx +45 -0
- package/src/lib/components/common/Button/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/common/ConfirmPopover/ConfirmPopover.tsx +47 -0
- package/src/lib/components/common/ConfirmPopover/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/common/DNDItemsList/DNDItemsList.tsx +77 -0
- package/src/lib/components/common/DNDItemsList/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/common/DeleteModal/DeleteModal.tsx +72 -0
- package/src/lib/components/common/DeleteModal/index.tsx +3 -0
- package/src/lib/components/common/Drawer/Drawer.tsx +79 -0
- package/src/lib/components/common/Drawer/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/common/Form/Form.tsx +256 -0
- package/src/lib/components/common/Form/SimpleForm.tsx +314 -0
- package/src/lib/components/common/Form/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/common/FormActions/FormActions.tsx +37 -0
- package/src/lib/components/common/FormActions/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/common/ImageUpload/ImageUpload.tsx +113 -0
- package/src/lib/components/common/ImageUpload/index.tsx +3 -0
- package/src/lib/components/common/Input/Checkbox.tsx +25 -0
- package/src/lib/components/common/Input/Input.tsx +51 -0
- package/src/lib/components/common/Input/ReactSelect.tsx +61 -0
- package/src/lib/components/common/Input/Select.tsx +47 -0
- package/src/lib/components/common/Input/SrcSet.tsx +143 -0
- package/src/lib/components/common/Input/index.ts +20 -0
- package/src/lib/components/common/Modal/Modal.tsx +57 -0
- package/src/lib/components/common/Modal/index.tsx +3 -0
- package/src/lib/components/common/Pagination/Pagination.tsx +88 -0
- package/src/lib/components/common/Pagination/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/common/Table/Table.tsx +133 -0
- package/src/lib/components/common/Table/{index.d.ts → index.ts} +1 -0
- package/src/lib/components/common/Toggle/Toggle.tsx +23 -0
- package/src/lib/components/common/Toggle/{index.d.ts → index.ts} +1 -0
- package/src/lib/constants/common.ts +145 -0
- package/src/lib/context/PageContext.tsx +100 -0
- package/src/lib/context/ProviderContext.tsx +52 -0
- package/src/lib/context/WidgetContext.tsx +127 -0
- package/src/lib/helper/utils.ts +46 -0
- package/src/lib/hooks/usePage.tsx +306 -0
- package/src/lib/hooks/usePagination.tsx +41 -0
- package/src/lib/hooks/useWidget.tsx +503 -0
- package/src/lib/icons/chevronDown.tsx +21 -0
- package/src/lib/icons/chevronLeft.tsx +20 -0
- package/src/lib/icons/chevronRight.tsx +20 -0
- package/src/lib/icons/chevronUp.tsx +21 -0
- package/src/lib/icons/close.tsx +21 -0
- package/src/lib/icons/pencil.tsx +21 -0
- package/src/lib/icons/plus.tsx +23 -0
- package/src/lib/icons/settings.tsx +35 -0
- package/src/lib/icons/trash.tsx +21 -0
- package/src/lib/types/api.ts +44 -0
- package/src/lib/types/common.ts +31 -0
- package/src/lib/types/components.ts +428 -0
- package/src/lib/types/context.ts +184 -0
- package/src/styles/index.css +481 -0
- package/index.cjs +0 -7714
- package/index.css +0 -2
- package/index.js +0 -7700
- package/src/lib/api/index.d.ts +0 -10
- package/src/lib/api/list.d.ts +0 -56
- package/src/lib/components/Page/AddButton/AddButton.d.ts +0 -3
- package/src/lib/components/Page/Form/PageForm.d.ts +0 -4
- package/src/lib/components/Page/Page/Page.d.ts +0 -13
- package/src/lib/components/Page/PageFormActions/PageFormActions.d.ts +0 -4
- package/src/lib/components/Page/PageFormWrapper/PageFormWrapper.d.ts +0 -4
- package/src/lib/components/Page/Pagination/PagePagination.d.ts +0 -3
- package/src/lib/components/Page/Search/PageSearch.d.ts +0 -3
- package/src/lib/components/Page/Table/PageTable.d.ts +0 -4
- package/src/lib/components/Widget/AddButton/AddButton.d.ts +0 -3
- package/src/lib/components/Widget/Form/ItemsAccordian.d.ts +0 -4
- package/src/lib/components/Widget/Form/Tabs/TabItem.d.ts +0 -4
- package/src/lib/components/Widget/Form/Tabs/Tabs.d.ts +0 -4
- package/src/lib/components/Widget/Form/WidgetForm.d.ts +0 -4
- package/src/lib/components/Widget/Pagination/WidgetPagination.d.ts +0 -3
- package/src/lib/components/Widget/Search/WidgetSearch.d.ts +0 -3
- package/src/lib/components/Widget/Table/WidgetTable.d.ts +0 -4
- package/src/lib/components/Widget/Widget/Widget.d.ts +0 -13
- package/src/lib/components/Widget/WidgetFormActions/WidgetFormActions.d.ts +0 -4
- package/src/lib/components/Widget/WidgetFormWrapper/WidgetFormWrapper.d.ts +0 -4
- package/src/lib/components/common/Accordian/Accordian.d.ts +0 -13
- package/src/lib/components/common/Button/Button.d.ts +0 -4
- package/src/lib/components/common/ConfirmPopover/ConfirmPopover.d.ts +0 -4
- package/src/lib/components/common/DNDItemsList/DNDItemsList.d.ts +0 -4
- package/src/lib/components/common/DeleteModal/DeleteModal.d.ts +0 -4
- package/src/lib/components/common/DeleteModal/index.d.ts +0 -2
- package/src/lib/components/common/Drawer/Drawer.d.ts +0 -9
- package/src/lib/components/common/Form/Form.d.ts +0 -15
- package/src/lib/components/common/Form/SimpleForm.d.ts +0 -18
- package/src/lib/components/common/FormActions/FormActions.d.ts +0 -10
- package/src/lib/components/common/ImageUpload/ImageUpload.d.ts +0 -4
- package/src/lib/components/common/ImageUpload/index.d.ts +0 -2
- package/src/lib/components/common/Input/Checkbox.d.ts +0 -4
- package/src/lib/components/common/Input/Input.d.ts +0 -4
- package/src/lib/components/common/Input/ReactSelect.d.ts +0 -4
- package/src/lib/components/common/Input/Select.d.ts +0 -4
- package/src/lib/components/common/Input/SrcSet.d.ts +0 -4
- package/src/lib/components/common/Input/index.d.ts +0 -12
- package/src/lib/components/common/Modal/Modal.d.ts +0 -8
- package/src/lib/components/common/Modal/index.d.ts +0 -2
- package/src/lib/components/common/Pagination/Pagination.d.ts +0 -4
- package/src/lib/components/common/Table/Table.d.ts +0 -4
- package/src/lib/components/common/Toggle/Toggle.d.ts +0 -4
- package/src/lib/constants/common.d.ts +0 -122
- package/src/lib/context/PageContext.d.ts +0 -7
- package/src/lib/context/ProviderContext.d.ts +0 -5
- package/src/lib/context/WidgetContext.d.ts +0 -7
- package/src/lib/helper/utils.d.ts +0 -10
- package/src/lib/hooks/usePage.d.ts +0 -43
- package/src/lib/hooks/usePagination.d.ts +0 -15
- package/src/lib/hooks/useWidget.d.ts +0 -46
- package/src/lib/icons/chevronDown.d.ts +0 -4
- package/src/lib/icons/chevronLeft.d.ts +0 -4
- package/src/lib/icons/chevronRight.d.ts +0 -4
- package/src/lib/icons/chevronUp.d.ts +0 -4
- package/src/lib/icons/close.d.ts +0 -4
- package/src/lib/icons/pencil.d.ts +0 -4
- package/src/lib/icons/plus.d.ts +0 -4
- package/src/lib/icons/trash.d.ts +0 -4
- package/src/lib/types/api.d.ts +0 -34
- package/src/lib/types/common.d.ts +0 -29
- package/src/lib/types/components.d.ts +0 -420
- package/src/lib/types/context.d.ts +0 -135
- /package/src/lib/types/{index.d.ts → index.ts} +0 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
2
|
+
import React, { createContext, useContext } from 'react';
|
|
3
|
+
import { PAGE_LIMITS, TRANSLATION_PAIRS_PAGE } from '../constants/common';
|
|
4
|
+
import { PageContextType } from '../types';
|
|
5
|
+
|
|
6
|
+
interface PageContextProviderProps
|
|
7
|
+
extends React.PropsWithChildren,
|
|
8
|
+
Partial<PageContextType> {}
|
|
9
|
+
|
|
10
|
+
const PageContext = createContext<PageContextType | null>(null);
|
|
11
|
+
|
|
12
|
+
const PageContextProvider = ({
|
|
13
|
+
searchText = '',
|
|
14
|
+
changeSearch = () => {},
|
|
15
|
+
list = [],
|
|
16
|
+
widgets = [],
|
|
17
|
+
formState = '',
|
|
18
|
+
closeForm = () => {},
|
|
19
|
+
getPages = () => {},
|
|
20
|
+
loading = false,
|
|
21
|
+
getWidgets = () => {},
|
|
22
|
+
onChangeFormState = () => {},
|
|
23
|
+
onPageFormSubmit = () => {},
|
|
24
|
+
canAdd = false,
|
|
25
|
+
canUpdate = false,
|
|
26
|
+
selectedWidgets = [],
|
|
27
|
+
setSelectedWidgets = () => {},
|
|
28
|
+
onChangeWidgetSequence = () => {},
|
|
29
|
+
// Pagination
|
|
30
|
+
currentPage = 1,
|
|
31
|
+
limits = PAGE_LIMITS,
|
|
32
|
+
pageSize = PAGE_LIMITS[0],
|
|
33
|
+
setCurrentPage = () => {},
|
|
34
|
+
setPageSize = () => {},
|
|
35
|
+
totalPages = 0,
|
|
36
|
+
totalRecords = 0,
|
|
37
|
+
canList = false,
|
|
38
|
+
// Table
|
|
39
|
+
columns = [],
|
|
40
|
+
data = [],
|
|
41
|
+
canDelete = false,
|
|
42
|
+
loader = undefined,
|
|
43
|
+
pageTranslations,
|
|
44
|
+
// other
|
|
45
|
+
children,
|
|
46
|
+
}: PageContextProviderProps) => {
|
|
47
|
+
return (
|
|
48
|
+
<PageContext.Provider
|
|
49
|
+
value={{
|
|
50
|
+
// Form
|
|
51
|
+
list,
|
|
52
|
+
searchText,
|
|
53
|
+
changeSearch,
|
|
54
|
+
widgets,
|
|
55
|
+
closeForm,
|
|
56
|
+
formState,
|
|
57
|
+
loading,
|
|
58
|
+
getWidgets,
|
|
59
|
+
onChangeFormState,
|
|
60
|
+
canAdd,
|
|
61
|
+
canUpdate,
|
|
62
|
+
onPageFormSubmit,
|
|
63
|
+
selectedWidgets,
|
|
64
|
+
setSelectedWidgets,
|
|
65
|
+
onChangeWidgetSequence,
|
|
66
|
+
// Pagination
|
|
67
|
+
getPages,
|
|
68
|
+
currentPage,
|
|
69
|
+
limits,
|
|
70
|
+
pageSize,
|
|
71
|
+
setCurrentPage,
|
|
72
|
+
setPageSize,
|
|
73
|
+
totalPages,
|
|
74
|
+
totalRecords,
|
|
75
|
+
canList,
|
|
76
|
+
// Table
|
|
77
|
+
columns,
|
|
78
|
+
data,
|
|
79
|
+
canDelete,
|
|
80
|
+
loader,
|
|
81
|
+
pageTranslations: {
|
|
82
|
+
...TRANSLATION_PAIRS_PAGE,
|
|
83
|
+
...(pageTranslations || {}),
|
|
84
|
+
},
|
|
85
|
+
}}
|
|
86
|
+
>
|
|
87
|
+
{children}
|
|
88
|
+
</PageContext.Provider>
|
|
89
|
+
);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export function usePageState() {
|
|
93
|
+
const context = useContext(PageContext);
|
|
94
|
+
if (!context)
|
|
95
|
+
throw new Error('Page Context must be used within PageContext.Provider');
|
|
96
|
+
|
|
97
|
+
return context;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export default PageContextProvider;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
2
|
+
import React, { createContext, useContext } from 'react';
|
|
3
|
+
import { TRANSLATION_PAIRS_COMMON } from '../constants/common';
|
|
4
|
+
import { ProviderContextProviderProps, ProviderContextType } from '../types';
|
|
5
|
+
|
|
6
|
+
const ProviderContext = createContext<ProviderContextType | null>(null);
|
|
7
|
+
|
|
8
|
+
const Provider = ({
|
|
9
|
+
children,
|
|
10
|
+
baseUrl,
|
|
11
|
+
token,
|
|
12
|
+
translations,
|
|
13
|
+
onError = () => {},
|
|
14
|
+
onSuccess = () => {},
|
|
15
|
+
onLogout = () => {},
|
|
16
|
+
switchClass = 'khb_switch',
|
|
17
|
+
widgetRoutesPrefix = 'widgets',
|
|
18
|
+
pageRoutesPrefix = 'pages',
|
|
19
|
+
}: ProviderContextProviderProps) => {
|
|
20
|
+
return (
|
|
21
|
+
<ProviderContext.Provider
|
|
22
|
+
value={{
|
|
23
|
+
baseUrl,
|
|
24
|
+
token,
|
|
25
|
+
onError,
|
|
26
|
+
onSuccess,
|
|
27
|
+
onLogout,
|
|
28
|
+
switchClass,
|
|
29
|
+
widgetRoutesPrefix,
|
|
30
|
+
pageRoutesPrefix,
|
|
31
|
+
commonTranslations: {
|
|
32
|
+
...TRANSLATION_PAIRS_COMMON,
|
|
33
|
+
...(translations || {}),
|
|
34
|
+
},
|
|
35
|
+
}}
|
|
36
|
+
>
|
|
37
|
+
{children}
|
|
38
|
+
</ProviderContext.Provider>
|
|
39
|
+
);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export function useProviderState() {
|
|
43
|
+
const context = useContext(ProviderContext);
|
|
44
|
+
if (!context)
|
|
45
|
+
throw new Error(
|
|
46
|
+
'Provider Context must be used within ProviderContext.Provider'
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
return context;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default Provider;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
2
|
+
import React, { createContext, useContext } from 'react';
|
|
3
|
+
import { PAGE_LIMITS, TRANSLATION_PAIRS_WIDGET } from '../constants/common';
|
|
4
|
+
import { WidgetContextType } from '../types';
|
|
5
|
+
|
|
6
|
+
interface WidgetContextProviderProps
|
|
7
|
+
extends React.PropsWithChildren,
|
|
8
|
+
Partial<WidgetContextType> {}
|
|
9
|
+
|
|
10
|
+
const WidgetContext = createContext<WidgetContextType | null>(null);
|
|
11
|
+
|
|
12
|
+
const WidgetContextProvider = ({
|
|
13
|
+
// Form
|
|
14
|
+
list = [],
|
|
15
|
+
languages = [],
|
|
16
|
+
imageBaseUrl = '',
|
|
17
|
+
searchText = '',
|
|
18
|
+
changeSearch = () => {},
|
|
19
|
+
formState = '',
|
|
20
|
+
closeForm = () => {},
|
|
21
|
+
loading = false,
|
|
22
|
+
onChangeFormState = () => {},
|
|
23
|
+
onWidgetFormSubmit = () => {},
|
|
24
|
+
updateData = null,
|
|
25
|
+
canAdd = false,
|
|
26
|
+
canUpdate = false,
|
|
27
|
+
onDeleteItem = () => {},
|
|
28
|
+
getWidgets = () => {},
|
|
29
|
+
onImageUpload = async (file: File) => {},
|
|
30
|
+
onImageRemove = async (id: string) => {},
|
|
31
|
+
itemsTypes = [],
|
|
32
|
+
widgetTypes = [],
|
|
33
|
+
getCollectionData = () => Promise.resolve(),
|
|
34
|
+
collectionDataLoading = false,
|
|
35
|
+
collectionData = [],
|
|
36
|
+
formatListItem,
|
|
37
|
+
formatOptionLabel,
|
|
38
|
+
// Pagination
|
|
39
|
+
currentPage = 1,
|
|
40
|
+
limits = PAGE_LIMITS,
|
|
41
|
+
pageSize = PAGE_LIMITS[0],
|
|
42
|
+
setCurrentPage = () => {},
|
|
43
|
+
setPageSize = () => {},
|
|
44
|
+
totalPages = 0,
|
|
45
|
+
totalRecords = 0,
|
|
46
|
+
// Table
|
|
47
|
+
canList = false,
|
|
48
|
+
canPartialUpdate = false,
|
|
49
|
+
columns = [],
|
|
50
|
+
data = [],
|
|
51
|
+
canDelete = false,
|
|
52
|
+
loader = <span />,
|
|
53
|
+
onPartialUpdateWidget = () => Promise.resolve(),
|
|
54
|
+
reactSelectStyles = {},
|
|
55
|
+
imageMaxSize = 10_485_760,
|
|
56
|
+
widgetTranslations,
|
|
57
|
+
// other
|
|
58
|
+
children,
|
|
59
|
+
}: WidgetContextProviderProps) => {
|
|
60
|
+
return (
|
|
61
|
+
<WidgetContext.Provider
|
|
62
|
+
value={{
|
|
63
|
+
// Form
|
|
64
|
+
list,
|
|
65
|
+
languages,
|
|
66
|
+
imageBaseUrl,
|
|
67
|
+
closeForm,
|
|
68
|
+
formState,
|
|
69
|
+
loading,
|
|
70
|
+
searchText,
|
|
71
|
+
changeSearch,
|
|
72
|
+
onChangeFormState,
|
|
73
|
+
onWidgetFormSubmit,
|
|
74
|
+
updateData,
|
|
75
|
+
canAdd,
|
|
76
|
+
canUpdate,
|
|
77
|
+
onDeleteItem,
|
|
78
|
+
getWidgets,
|
|
79
|
+
onImageUpload,
|
|
80
|
+
onImageRemove,
|
|
81
|
+
onPartialUpdateWidget,
|
|
82
|
+
itemsTypes,
|
|
83
|
+
widgetTypes,
|
|
84
|
+
getCollectionData,
|
|
85
|
+
collectionDataLoading,
|
|
86
|
+
collectionData,
|
|
87
|
+
formatListItem,
|
|
88
|
+
formatOptionLabel,
|
|
89
|
+
reactSelectStyles,
|
|
90
|
+
// Pagination
|
|
91
|
+
currentPage,
|
|
92
|
+
limits,
|
|
93
|
+
pageSize,
|
|
94
|
+
setCurrentPage,
|
|
95
|
+
setPageSize,
|
|
96
|
+
totalPages,
|
|
97
|
+
totalRecords,
|
|
98
|
+
// Table
|
|
99
|
+
canList,
|
|
100
|
+
canPartialUpdate,
|
|
101
|
+
columns,
|
|
102
|
+
data,
|
|
103
|
+
canDelete,
|
|
104
|
+
loader,
|
|
105
|
+
imageMaxSize,
|
|
106
|
+
widgetTranslations: {
|
|
107
|
+
...TRANSLATION_PAIRS_WIDGET,
|
|
108
|
+
...(widgetTranslations || {}),
|
|
109
|
+
},
|
|
110
|
+
}}
|
|
111
|
+
>
|
|
112
|
+
{children}
|
|
113
|
+
</WidgetContext.Provider>
|
|
114
|
+
);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export function useWidgetState() {
|
|
118
|
+
const context = useContext(WidgetContext);
|
|
119
|
+
if (!context)
|
|
120
|
+
throw new Error(
|
|
121
|
+
'Widget Context must be used within WidgetContext.Provider'
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
return context;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export default WidgetContextProvider;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { CONSTANTS } from '../constants/common';
|
|
2
|
+
import { TFunc } from '../types';
|
|
3
|
+
|
|
4
|
+
/* eslint-disable no-useless-escape */
|
|
5
|
+
export const paginationDataGatter = (data: any) => {
|
|
6
|
+
return data.data.docs;
|
|
7
|
+
};
|
|
8
|
+
export const dataGatter = (data: any) => {
|
|
9
|
+
return data.data;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const capitalizeFirstLetter = (string = '') =>
|
|
13
|
+
`${string?.trim().charAt(0)?.toUpperCase()}${string?.slice(1)}`;
|
|
14
|
+
|
|
15
|
+
export const changeToCode = (string = '') =>
|
|
16
|
+
string
|
|
17
|
+
.replace(/[^\s\w]/gi, '')
|
|
18
|
+
?.toUpperCase()
|
|
19
|
+
?.replace(CONSTANTS.EMPTY_REGEX, '_');
|
|
20
|
+
|
|
21
|
+
export const changeToSlug = (string = '') =>
|
|
22
|
+
string?.toLowerCase()?.replace(CONSTANTS.EMPTY_REGEX, '-');
|
|
23
|
+
|
|
24
|
+
export const isObject = (data: any) => data?.constructor?.name === 'Object';
|
|
25
|
+
export const isString = (data: any) => data?.constructor?.name === 'String';
|
|
26
|
+
export const isArray = (data: any) => data?.constructor?.name === 'Array';
|
|
27
|
+
|
|
28
|
+
export const isEmpty = (data: any) => {
|
|
29
|
+
if (isObject(data)) return Object.keys(data).length === 0;
|
|
30
|
+
if (isArray(data)) return data.length === 0;
|
|
31
|
+
if (isString(data)) return !data || data.length === 0;
|
|
32
|
+
if ([undefined, null, ''].includes(data)) return true;
|
|
33
|
+
return false;
|
|
34
|
+
};
|
|
35
|
+
export const build_path = (...args: string[]) => {
|
|
36
|
+
return args
|
|
37
|
+
.map((part, i) => {
|
|
38
|
+
if (i === 0) {
|
|
39
|
+
return part.trim().replace(/[\/]*$/g, '');
|
|
40
|
+
} else {
|
|
41
|
+
return part.trim().replace(/(^[\/]*|[\/]*$)/g, '');
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
.filter((x) => x.length)
|
|
45
|
+
.join('/');
|
|
46
|
+
};
|
|
@@ -0,0 +1,306 @@
|
|
|
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 } from '../helper/utils';
|
|
5
|
+
import usePagination from './usePagination';
|
|
6
|
+
import request, { getApiType } from '../api';
|
|
7
|
+
import { FormActionTypes, ObjectType, OptionType, Routes_Input } from '../types';
|
|
8
|
+
|
|
9
|
+
interface UsePageProps {
|
|
10
|
+
canList?: boolean;
|
|
11
|
+
defaultLimit: number;
|
|
12
|
+
routes?: Routes_Input;
|
|
13
|
+
preConfirmDelete?: (data: { row: ObjectType }) => Promise<boolean>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const usePage = ({
|
|
17
|
+
routes,
|
|
18
|
+
defaultLimit,
|
|
19
|
+
canList = true,
|
|
20
|
+
preConfirmDelete,
|
|
21
|
+
}: UsePageProps) => {
|
|
22
|
+
const [list, setList] = useState<ObjectType[]>([]);
|
|
23
|
+
const [loading, setLoading] = useState(false);
|
|
24
|
+
const [widgets, setWidgets] = useState<ObjectType[]>([]);
|
|
25
|
+
const [selectedWidgets, setSelectedWidgets] = useState<OptionType[]>([]);
|
|
26
|
+
const [widgetsLoading, setWidgetsLoading] = useState<boolean>(false);
|
|
27
|
+
const [totalPages, setTotalPages] = useState(0);
|
|
28
|
+
const [totalRecords, setTotalRecords] = useState(0);
|
|
29
|
+
const [itemData, setItemData] = useState<any | null>(null);
|
|
30
|
+
const [formState, setFormState] = useState<FormActionTypes>();
|
|
31
|
+
|
|
32
|
+
const {
|
|
33
|
+
baseUrl,
|
|
34
|
+
token,
|
|
35
|
+
onError,
|
|
36
|
+
onSuccess,
|
|
37
|
+
onLogout,
|
|
38
|
+
pageRoutesPrefix,
|
|
39
|
+
widgetRoutesPrefix,
|
|
40
|
+
} = useProviderState();
|
|
41
|
+
const {
|
|
42
|
+
setPageSize,
|
|
43
|
+
pageSize,
|
|
44
|
+
currentPageRef,
|
|
45
|
+
setCurrentPage,
|
|
46
|
+
offsetRef,
|
|
47
|
+
limitRef,
|
|
48
|
+
searchRef,
|
|
49
|
+
changeSearch,
|
|
50
|
+
} = usePagination({ defaultLimit });
|
|
51
|
+
|
|
52
|
+
const handleError = useCallback(
|
|
53
|
+
(code: CALLBACK_CODES) => (error: any) => {
|
|
54
|
+
const { data = {} } = error?.response || {};
|
|
55
|
+
if (data?.code === 'UNAUTHENTICATED') {
|
|
56
|
+
onLogout();
|
|
57
|
+
}
|
|
58
|
+
onError(code, 'error', data?.message);
|
|
59
|
+
},
|
|
60
|
+
[onError, onLogout]
|
|
61
|
+
);
|
|
62
|
+
const getWidgets = useCallback(
|
|
63
|
+
async (
|
|
64
|
+
search?: string,
|
|
65
|
+
collectionItems?: string[],
|
|
66
|
+
callback?: (data: any) => void
|
|
67
|
+
) => {
|
|
68
|
+
try {
|
|
69
|
+
setWidgetsLoading(true);
|
|
70
|
+
const api = getApiType({
|
|
71
|
+
routes,
|
|
72
|
+
action: 'LIST',
|
|
73
|
+
prefix: widgetRoutesPrefix,
|
|
74
|
+
});
|
|
75
|
+
const response = await request({
|
|
76
|
+
baseUrl,
|
|
77
|
+
token,
|
|
78
|
+
method: api.method,
|
|
79
|
+
url: api.url,
|
|
80
|
+
onError: handleError(CALLBACK_CODES.GET_ALL),
|
|
81
|
+
data: {
|
|
82
|
+
collectionItems: collectionItems || [],
|
|
83
|
+
search: search || '',
|
|
84
|
+
all: true,
|
|
85
|
+
isActive: true,
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
if (response?.code === 'SUCCESS') {
|
|
89
|
+
let widgetsData = paginationDataGatter(response);
|
|
90
|
+
widgetsData = widgetsData.map((item: ObjectType) => {
|
|
91
|
+
return {
|
|
92
|
+
label: item['name'],
|
|
93
|
+
value: item['_id'] || item['id'],
|
|
94
|
+
code: item['code'],
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
if (typeof callback === 'function') callback(widgetsData);
|
|
98
|
+
return setWidgets(widgetsData);
|
|
99
|
+
}
|
|
100
|
+
setWidgetsLoading(false);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
setWidgetsLoading(false);
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
[baseUrl, handleError, routes, token, widgetRoutesPrefix]
|
|
106
|
+
);
|
|
107
|
+
const getPages = useCallback(
|
|
108
|
+
async (search?: string) => {
|
|
109
|
+
try {
|
|
110
|
+
setLoading(true);
|
|
111
|
+
const api = getApiType({
|
|
112
|
+
routes,
|
|
113
|
+
action: 'LIST',
|
|
114
|
+
prefix: pageRoutesPrefix,
|
|
115
|
+
});
|
|
116
|
+
const response = await request({
|
|
117
|
+
baseUrl,
|
|
118
|
+
token,
|
|
119
|
+
method: api.method,
|
|
120
|
+
url: api.url,
|
|
121
|
+
onError: handleError(CALLBACK_CODES.GET_ALL),
|
|
122
|
+
data: {
|
|
123
|
+
search,
|
|
124
|
+
options: {
|
|
125
|
+
offset: offsetRef.current,
|
|
126
|
+
limit: limitRef.current,
|
|
127
|
+
page: currentPageRef.current,
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
if (response?.code === 'SUCCESS') {
|
|
132
|
+
setLoading(false);
|
|
133
|
+
setTotalPages(response.data.totalPages);
|
|
134
|
+
setTotalRecords(response.data.totalDocs);
|
|
135
|
+
return setList(paginationDataGatter(response));
|
|
136
|
+
}
|
|
137
|
+
setLoading(false);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
setLoading(false);
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
[
|
|
143
|
+
baseUrl,
|
|
144
|
+
currentPageRef,
|
|
145
|
+
limitRef,
|
|
146
|
+
offsetRef,
|
|
147
|
+
handleError,
|
|
148
|
+
pageRoutesPrefix,
|
|
149
|
+
routes,
|
|
150
|
+
token,
|
|
151
|
+
]
|
|
152
|
+
);
|
|
153
|
+
const onPageFormSubmit = async (data: ObjectType) => {
|
|
154
|
+
if (selectedWidgets.length) {
|
|
155
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
156
|
+
// @ts-ignore
|
|
157
|
+
data.widgets = selectedWidgets.map((item) => item.value);
|
|
158
|
+
}
|
|
159
|
+
setLoading(true);
|
|
160
|
+
const code =
|
|
161
|
+
formState === 'ADD' ? CALLBACK_CODES.CREATE : CALLBACK_CODES.UPDATE;
|
|
162
|
+
try {
|
|
163
|
+
const api = getApiType({
|
|
164
|
+
routes,
|
|
165
|
+
action: formState === 'ADD' ? 'CREATE' : 'UPDATE',
|
|
166
|
+
prefix: pageRoutesPrefix,
|
|
167
|
+
id: itemData?._id,
|
|
168
|
+
});
|
|
169
|
+
const response = await request({
|
|
170
|
+
baseUrl,
|
|
171
|
+
token,
|
|
172
|
+
data,
|
|
173
|
+
url: api.url,
|
|
174
|
+
method: api.method,
|
|
175
|
+
onError: handleError(code),
|
|
176
|
+
});
|
|
177
|
+
if (response?.code === 'SUCCESS') {
|
|
178
|
+
if (formState === 'ADD') {
|
|
179
|
+
setCurrentPage(1);
|
|
180
|
+
}
|
|
181
|
+
setLoading(false);
|
|
182
|
+
onSuccess(code, response?.code, response?.message);
|
|
183
|
+
getPages();
|
|
184
|
+
onCloseForm();
|
|
185
|
+
} else {
|
|
186
|
+
setLoading(false);
|
|
187
|
+
onError(code, response?.code, response?.message);
|
|
188
|
+
}
|
|
189
|
+
} catch (error) {
|
|
190
|
+
setLoading(false);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
const onCloseForm = () => {
|
|
194
|
+
setFormState(undefined);
|
|
195
|
+
setItemData(null);
|
|
196
|
+
};
|
|
197
|
+
const onCofirmDeletePage = async () => {
|
|
198
|
+
try {
|
|
199
|
+
let proceed = true;
|
|
200
|
+
if (typeof preConfirmDelete === 'function') {
|
|
201
|
+
try {
|
|
202
|
+
proceed = await preConfirmDelete({ row: itemData });
|
|
203
|
+
} catch (error) {
|
|
204
|
+
proceed = false;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (proceed) {
|
|
209
|
+
setLoading(true);
|
|
210
|
+
const api = getApiType({
|
|
211
|
+
routes,
|
|
212
|
+
action: 'DELETE',
|
|
213
|
+
prefix: pageRoutesPrefix,
|
|
214
|
+
id: itemData?._id,
|
|
215
|
+
});
|
|
216
|
+
const response = await request({
|
|
217
|
+
baseUrl,
|
|
218
|
+
token,
|
|
219
|
+
method: api.method,
|
|
220
|
+
url: api.url,
|
|
221
|
+
onError: handleError(CALLBACK_CODES.DELETE),
|
|
222
|
+
});
|
|
223
|
+
if (response?.code === 'SUCCESS') {
|
|
224
|
+
setLoading(false);
|
|
225
|
+
onSuccess(CALLBACK_CODES.DELETE, response?.code, response?.message);
|
|
226
|
+
getPages();
|
|
227
|
+
onCloseForm();
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
setLoading(false);
|
|
231
|
+
onError(CALLBACK_CODES.DELETE, response?.code, response?.message);
|
|
232
|
+
onCloseForm();
|
|
233
|
+
}
|
|
234
|
+
} catch (error) {
|
|
235
|
+
setLoading(false);
|
|
236
|
+
onError(
|
|
237
|
+
CALLBACK_CODES.DELETE,
|
|
238
|
+
INTERNAL_ERROR_CODE,
|
|
239
|
+
(error as Error).message
|
|
240
|
+
);
|
|
241
|
+
onCloseForm();
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
const onChangeFormState = (state: FormActionTypes, data?: any) => {
|
|
245
|
+
setItemData(data || null);
|
|
246
|
+
setFormState(state);
|
|
247
|
+
if (state === 'UPDATE' && data?.widgets) {
|
|
248
|
+
// setSelectedWidgets(widgets.filter((widget) => data.widgets.includes(widget.value)));
|
|
249
|
+
} else {
|
|
250
|
+
setSelectedWidgets([]);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
const onChangeWidgetSequence = (
|
|
254
|
+
sourceIndex: number,
|
|
255
|
+
destinationIndex: number
|
|
256
|
+
) => {
|
|
257
|
+
console.log(sourceIndex, destinationIndex);
|
|
258
|
+
setSelectedWidgets((listData) => {
|
|
259
|
+
const temporaryData = [...listData];
|
|
260
|
+
const [selectedRow] = temporaryData.splice(sourceIndex, 1);
|
|
261
|
+
temporaryData.splice(destinationIndex, 0, selectedRow);
|
|
262
|
+
return temporaryData;
|
|
263
|
+
});
|
|
264
|
+
};
|
|
265
|
+
const changeCurrentPage = (page: number) => {
|
|
266
|
+
setCurrentPage(page);
|
|
267
|
+
getPages(searchRef.current);
|
|
268
|
+
};
|
|
269
|
+
useEffect(() => {
|
|
270
|
+
if (canList) getPages();
|
|
271
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
272
|
+
}, [pageSize, canList]);
|
|
273
|
+
|
|
274
|
+
return {
|
|
275
|
+
list,
|
|
276
|
+
getPages,
|
|
277
|
+
loading,
|
|
278
|
+
setLoading,
|
|
279
|
+
|
|
280
|
+
// Pagination
|
|
281
|
+
searchText: searchRef.current,
|
|
282
|
+
changeSearch,
|
|
283
|
+
pageSize,
|
|
284
|
+
totalPages,
|
|
285
|
+
currentPage: currentPageRef.current,
|
|
286
|
+
totalRecords,
|
|
287
|
+
setCurrentPage: changeCurrentPage,
|
|
288
|
+
setPageSize,
|
|
289
|
+
|
|
290
|
+
// Form
|
|
291
|
+
widgets,
|
|
292
|
+
itemData,
|
|
293
|
+
formState,
|
|
294
|
+
getWidgets,
|
|
295
|
+
onCloseForm,
|
|
296
|
+
widgetsLoading,
|
|
297
|
+
selectedWidgets,
|
|
298
|
+
setSelectedWidgets,
|
|
299
|
+
onPageFormSubmit,
|
|
300
|
+
onChangeFormState,
|
|
301
|
+
onCofirmDeletePage,
|
|
302
|
+
onChangeWidgetSequence,
|
|
303
|
+
};
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
export default usePage;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useRef } from 'react';
|
|
2
|
+
import * as constants from '../constants/common';
|
|
3
|
+
|
|
4
|
+
interface UsePaginationProps {
|
|
5
|
+
defaultLimit?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const usePagination = ({ defaultLimit }: UsePaginationProps) => {
|
|
9
|
+
const offsetRef = useRef<number>(constants.DEFAULT_OFFSET_PAYLOAD);
|
|
10
|
+
const limitRef = useRef<number>(defaultLimit || constants.DEFAULT_LIMIT);
|
|
11
|
+
const currentPageRef = useRef<number>(constants.DEFAULT_CURRENT_PAGE);
|
|
12
|
+
const searchRef = useRef<string>('');
|
|
13
|
+
|
|
14
|
+
const setPageSize = (value: number) => {
|
|
15
|
+
limitRef.current = Number.parseInt(String(value), constants.DECIMAL_REDIX);
|
|
16
|
+
offsetRef.current = constants.DEFAULT_OFFSET_PAYLOAD;
|
|
17
|
+
currentPageRef.current = constants.DEFAULT_CURRENT_PAGE;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const changeSearch = (value: string) => {
|
|
21
|
+
searchRef.current = value;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const changeCurrentPage = (value: number) => {
|
|
25
|
+
currentPageRef.current = value;
|
|
26
|
+
offsetRef.current = value * limitRef.current;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
pageSize: limitRef.current,
|
|
31
|
+
currentPageRef,
|
|
32
|
+
limitRef,
|
|
33
|
+
offsetRef,
|
|
34
|
+
searchRef,
|
|
35
|
+
setPageSize,
|
|
36
|
+
changeSearch,
|
|
37
|
+
setCurrentPage: changeCurrentPage,
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export default usePagination;
|