@dakataa/crud-react 1.0.0

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 (78) hide show
  1. package/.env +3 -0
  2. package/README.md +58 -0
  3. package/babel-plugin-macros.config.js +5 -0
  4. package/index.html +12 -0
  5. package/package.json +74 -0
  6. package/public/favicon.ico +0 -0
  7. package/public/logo192.png +0 -0
  8. package/public/logo512.png +0 -0
  9. package/public/manifest.json +25 -0
  10. package/public/robots.txt +3 -0
  11. package/src/Crud.tsx +29 -0
  12. package/src/CrudContext.tsx +21 -0
  13. package/src/CrudLoader.tsx +27 -0
  14. package/src/assets/images/alert/denied.json +1 -0
  15. package/src/assets/images/alert/info.json +1 -0
  16. package/src/assets/images/alert/success.json +1 -0
  17. package/src/assets/images/alert/warning.json +1 -0
  18. package/src/assets/style/alert.scss +97 -0
  19. package/src/component/Animation.tsx +50 -0
  20. package/src/component/BaseButton.tsx +26 -0
  21. package/src/component/Button.tsx +15 -0
  22. package/src/component/Dropdown.tsx +55 -0
  23. package/src/component/EmptyView.tsx +5 -0
  24. package/src/component/Link.tsx +11 -0
  25. package/src/component/Modal.tsx +174 -0
  26. package/src/component/ModifyForm.tsx +139 -0
  27. package/src/component/TemplateBlock.tsx +21 -0
  28. package/src/component/TemplateExtend.tsx +17 -0
  29. package/src/component/TemplateParentBlock.tsx +7 -0
  30. package/src/component/Translation.tsx +7 -0
  31. package/src/component/ViewLoader.tsx +33 -0
  32. package/src/component/crud/DynamicView.tsx +42 -0
  33. package/src/component/crud/FormView.tsx +29 -0
  34. package/src/component/crud/GridTableView.tsx +205 -0
  35. package/src/component/crud/PaginatorView.tsx +80 -0
  36. package/src/component/error/ErrorBoundary.tsx +78 -0
  37. package/src/component/error/Exception.tsx +17 -0
  38. package/src/component/error/HttpException.tsx +24 -0
  39. package/src/component/form/Choice.tsx +119 -0
  40. package/src/component/form/Form.tsx +233 -0
  41. package/src/component/form/FormAlert.tsx +19 -0
  42. package/src/component/form/FormFieldError.tsx +16 -0
  43. package/src/component/form/FormGroup.tsx +53 -0
  44. package/src/component/form/FormWidget.tsx +34 -0
  45. package/src/component/form/Input.tsx +63 -0
  46. package/src/component/form/constraint/Contraint.tsx +8 -0
  47. package/src/component/form/constraint/NotBlank.tsx +20 -0
  48. package/src/component/hooks/GetData.tsx +137 -0
  49. package/src/context/ActionContext.tsx +99 -0
  50. package/src/context/AlertContext.tsx +181 -0
  51. package/src/context/ModalContext.tsx +56 -0
  52. package/src/context/SessionContext.tsx +97 -0
  53. package/src/helper/ObjectUtils.tsx +36 -0
  54. package/src/helper/RouterUtils.tsx +24 -0
  55. package/src/helper/StingUtils.tsx +25 -0
  56. package/src/index.tsx +23 -0
  57. package/src/layout/default/Base.tsx +15 -0
  58. package/src/layout/default/Error.tsx +27 -0
  59. package/src/layout/default/Main.tsx +64 -0
  60. package/src/layout/default/component/Navigation.tsx +197 -0
  61. package/src/page/List.tsx +268 -0
  62. package/src/page/Modify.tsx +156 -0
  63. package/src/react-app-env.d.ts +2 -0
  64. package/src/setupTests.ts +5 -0
  65. package/src/type/ActionType.tsx +10 -0
  66. package/src/type/ColumnType.tsx +18 -0
  67. package/src/type/ExceptionType.tsx +7 -0
  68. package/src/type/FormType.tsx +6 -0
  69. package/src/type/FormViewType.tsx +37 -0
  70. package/src/type/ListMetaType.tsx +7 -0
  71. package/src/type/ListType.tsx +20 -0
  72. package/src/type/ModifyType.tsx +10 -0
  73. package/src/type/RedirectType.tsx +6 -0
  74. package/src/type/RouteType.tsx +5 -0
  75. package/tsconfig.app.json +46 -0
  76. package/tsconfig.json +11 -0
  77. package/tsconfig.node.json +15 -0
  78. package/vite.config.ts +33 -0
@@ -0,0 +1,197 @@
1
+ import React, {
2
+ createRef,
3
+ ForwardedRef,
4
+ forwardRef,
5
+ useDeferredValue,
6
+ useEffect,
7
+ useImperativeHandle,
8
+ useRef
9
+ } from "react";
10
+ import {Link, useLocation} from "react-router-dom";
11
+ import {generateRoute} from "@src/helper/RouterUtils.tsx";
12
+ import {RouteType} from "@src/type/RouteType";
13
+
14
+ export type MenuItem = {
15
+ title: string;
16
+ route?: RouteType;
17
+ icon?: any;
18
+ items?: MenuItem[];
19
+ target?: '_blank' | '_self',
20
+ }
21
+
22
+ export type NavigationItemContextType = {
23
+ toggle: (value?: boolean) => void;
24
+ }
25
+
26
+ const NavigationItemContext = React.createContext<NavigationItemContextType | null>(null);
27
+
28
+ const UseParentNavigationItem = () => {
29
+ return React.useContext<NavigationItemContextType | null>(NavigationItemContext);
30
+ }
31
+
32
+ const MainNavigationItem = ({item, open = false}: {
33
+ item: MenuItem;
34
+ open?: boolean;
35
+ }) => {
36
+
37
+ const isGroup = !!item.items?.length;
38
+ const location = useLocation();
39
+ const currentLocationPath = location.pathname.replace(/(.*?)\/?$/i, '$1');
40
+ const itemPath = generateRoute(item.route);
41
+ const isActive = currentLocationPath.includes(itemPath, 0);
42
+ const [active, setActive] = React.useState(isActive);
43
+ const parentNavigationMenu = UseParentNavigationItem();
44
+ const containerRef = useRef<HTMLElement | null>(null);
45
+ const startAnimationTimeout = useRef<number | null>(null);
46
+
47
+ const collapseClass = 'collapse';
48
+ const animatingClass = 'collapsing';
49
+
50
+ const context = {
51
+ toggle: (value?: boolean) => {
52
+ setActive(value !== undefined ? value : !active);
53
+ }
54
+ };
55
+
56
+ const onAnimationEnd = () => {
57
+ containerRef.current?.classList.remove(animatingClass);
58
+ containerRef.current?.classList.add(collapseClass);
59
+ containerRef.current?.classList.toggle('show', active);
60
+ containerRef.current?.style.removeProperty('height');
61
+ };
62
+
63
+ const onAnimationStart = () => {
64
+ if (startAnimationTimeout.current) {
65
+ clearTimeout(startAnimationTimeout.current);
66
+ startAnimationTimeout.current = null;
67
+ }
68
+ };
69
+
70
+ useEffect(() => {
71
+ if (active && parentNavigationMenu) {
72
+ parentNavigationMenu.toggle(true);
73
+ }
74
+
75
+ // Add animation events
76
+ containerRef.current?.addEventListener('animationstart', onAnimationStart);
77
+ containerRef.current?.addEventListener('transitionstart', onAnimationStart);
78
+ containerRef.current?.addEventListener('animationend', onAnimationEnd);
79
+ containerRef.current?.addEventListener('transitionend', onAnimationEnd);
80
+
81
+ containerRef.current?.style.setProperty('height', (!active ? containerRef.current?.scrollHeight : 0) + 'px');
82
+
83
+ // Add animating class
84
+ containerRef.current?.classList.remove('show');
85
+ containerRef.current?.classList.remove(collapseClass);
86
+ containerRef.current?.classList.add(animatingClass);
87
+
88
+ // Set height depend on activity
89
+ setTimeout(() => {
90
+ containerRef.current?.style.setProperty('height', (active ? containerRef.current?.scrollHeight : 0) + 'px');
91
+ }, 10)
92
+
93
+ // End animation if not started in first 50 ms (in case no animation)
94
+ startAnimationTimeout.current = setTimeout(onAnimationEnd, 50);
95
+
96
+ return () => {
97
+ onAnimationStart();
98
+
99
+ containerRef.current?.removeEventListener('animationstart', onAnimationStart);
100
+ containerRef.current?.removeEventListener('transitionstart', onAnimationStart);
101
+ containerRef.current?.removeEventListener('animationend', onAnimationEnd);
102
+ containerRef.current?.removeEventListener('transitionend', onAnimationEnd);
103
+ };
104
+ }, [active]);
105
+
106
+ useEffect(() => {
107
+ if (open !== active) {
108
+ setActive(open);
109
+ }
110
+ }, [open]);
111
+
112
+ return (
113
+ <NavigationItemContext.Provider value={context}>
114
+ <nav className={["item", ...(active || isActive ? ['active'] : [])].join(' ')}>
115
+ <Link
116
+ to={isGroup ? '#' : generateRoute(item.route)}
117
+ {...(item.icon && {icon: item.icon})}
118
+ {...(isGroup && {
119
+ onClick: (e) => {
120
+ setActive(!active);
121
+ }
122
+ })}
123
+ >
124
+ <span className={"icon"}></span>
125
+ <span className={"title"}>{item.title}</span>
126
+ </Link>
127
+
128
+ {isGroup && !!item.items?.length && (
129
+ <nav ref={containerRef} className={collapseClass}>
130
+ <NavigationGroup items={item.items}/>
131
+ </nav>
132
+ )}
133
+ </nav>
134
+ </NavigationItemContext.Provider>
135
+ )
136
+ };
137
+
138
+ const NavigationGroup = ({items}: {
139
+ items: MenuItem[];
140
+ }) => {
141
+ return (
142
+ items.map((item) => {
143
+ const key = [parent, item.title].filter(v => v).join('-').toLowerCase();
144
+ return (
145
+ <MainNavigationItem key={key} item={item}/>
146
+ )
147
+ }
148
+ )
149
+ );
150
+ };
151
+
152
+ const Navigation = forwardRef(({items, className, open = false}: {
153
+ items: MenuItem[];
154
+ className?: string;
155
+ open?: boolean;
156
+ props?: any;
157
+ }, ref: ForwardedRef<NavigationItemContextType>) => {
158
+ const containerRef = useRef<HTMLElement | null>(null);
159
+
160
+ const [active, setActive] = React.useState(open);
161
+ ref = ref || createRef<NavigationItemContextType>();
162
+
163
+ const context = {
164
+ toggle: (value?: boolean) => {
165
+ setActive(value !== undefined ? value : !active)
166
+ }
167
+ };
168
+
169
+ useImperativeHandle(ref, () => context);
170
+
171
+ useEffect(() => {
172
+ document.body.classList.toggle('toggle-nav', active);
173
+
174
+ const onClickOutside = (e: Event) => {
175
+ if (!containerRef.current?.contains(e.target as Node)) {
176
+ setActive(false);
177
+ document.body.removeEventListener('click', onClickOutside);
178
+ }
179
+ };
180
+
181
+ if (active) {
182
+ document.body.addEventListener('click', onClickOutside);
183
+ return () => {
184
+ document.body.removeEventListener('click', onClickOutside);
185
+ }
186
+ }
187
+ }, [active]);
188
+
189
+ return (
190
+ <nav ref={containerRef}>
191
+ <NavigationGroup items={items}/>
192
+ </nav>
193
+ )
194
+
195
+ });
196
+
197
+ export default Navigation;
@@ -0,0 +1,268 @@
1
+ import React, {memo, useEffect, useRef} from "react";
2
+ import {
3
+ convertFormDataToObject,
4
+ convertObjectToURLSearchParams,
5
+ convertURLSearchParamsToObject,
6
+ default as Requester,
7
+ Method
8
+ } from "@dakataa/requester";
9
+ import GridTableView, {OnClickAction} from "@src/component/crud/GridTableView.tsx";
10
+ import PaginatorView from "@src/component/crud/PaginatorView.tsx";
11
+ import {Link, useLocation, useSearchParams} from "react-router-dom";
12
+ import {Form, FormRef, nameToId} from "@src/component/form/Form.tsx";
13
+ import Dropdown, {DropdownButton, DropdownContent} from "@src/component/Dropdown.tsx";
14
+ import Button from "@src/component/Button.tsx";
15
+ import {generateRoute} from "@src/helper/RouterUtils.tsx";
16
+ import FormView from "@src/component/crud/FormView.tsx";
17
+ import {objectRemoveEmpty} from "@src/helper/ObjectUtils.tsx";
18
+ import DynamicView from "@src/component/crud/DynamicView.tsx";
19
+ import GetData, {GetDataType} from "@src/component/hooks/GetData.tsx";
20
+ import {ListType} from "@src/type/ListType.tsx";
21
+ import {ActionType} from "@src/type/ActionType.tsx";
22
+ import {UseModal} from "@src/context/ModalContext.tsx";
23
+ import {Icon, Result, UseAlert} from "@src/context/AlertContext.tsx";
24
+ import {default as T} from "@src/component/Translation.tsx";
25
+ import {FormViewType} from "@src/type/FormViewType.tsx";
26
+
27
+ const List = memo(({action, embedded = false}: {
28
+ action: OnClickAction,
29
+ embedded?: boolean
30
+ }) => {
31
+ const location = useLocation();
32
+ const [searchParams, setSearchParams] = useSearchParams();
33
+ const sort = useRef<{ [key: string]: any } | undefined>(undefined);
34
+ const filter = useRef<{ [key: string]: any } | undefined>(convertURLSearchParamsToObject(searchParams));
35
+ const filterFormRef = useRef<FormRef | null>(null);
36
+ const {openModal} = UseModal()
37
+ const {open: openAlert} = UseAlert();
38
+
39
+ const entity = action.action.entity;
40
+
41
+ if (!entity) {
42
+ throw new Error('Invalid Entity');
43
+ }
44
+
45
+ const {results, refresh, setQueryParameters}: GetDataType & {
46
+ results: ListType | null;
47
+ } = GetData({entityAction: action.action, initParameters: action.parameters, initQueryParameters: filter.current});
48
+ const actions = (Object.values(results?.action ?? []) as ActionType[]).filter(a => !a.object && a.name !== action.action.name);
49
+
50
+ const filterData = (excludeFilterParameters?: [string]): void => {
51
+ excludeFilterParameters?.forEach((key) => {
52
+ delete filter.current?.filter?.[key];
53
+ })
54
+
55
+ const query = {
56
+ ...(filter.current && filter.current),
57
+ ...(sort.current && {sort: sort.current}),
58
+ };
59
+
60
+ const searchQuery = convertObjectToURLSearchParams(objectRemoveEmpty(query));
61
+ if (embedded) {
62
+ setQueryParameters(searchQuery)
63
+ } else {
64
+ setSearchParams(searchQuery);
65
+ }
66
+ }
67
+
68
+ const handleBatchAction = (method: string, ids: any, data: FormData) => {
69
+ console.log('method', ids, data);
70
+
71
+ openAlert({
72
+ title: 'Are you sure?',
73
+ icon: Icon.confirm,
74
+ onResult: (result: Result) => {
75
+ if (result.isConfirmed) {
76
+ (new Requester).post(generateRoute(action.action.route, action.parameters), data).catch((e:any) => {
77
+ console.log('error', e);
78
+ }).finally(() => {
79
+ console.log('done');
80
+ refresh();
81
+ });
82
+ }
83
+ }
84
+ });
85
+ }
86
+
87
+ const handleAction = (onClickAction: OnClickAction, event?: React.MouseEvent) => {
88
+ if (onClickAction.parameters !== undefined) {
89
+ onClickAction.parameters = objectRemoveEmpty(onClickAction.parameters as object);
90
+ if (!Object.keys(onClickAction.parameters as object).length) {
91
+ onClickAction.parameters = undefined;
92
+ }
93
+ }
94
+
95
+ switch (onClickAction.action.name) {
96
+ case 'filter': {
97
+ event?.preventDefault();
98
+ filter.current = onClickAction.parameters;
99
+ break;
100
+ }
101
+ case 'sort': {
102
+ event?.preventDefault();
103
+ sort.current = onClickAction.parameters;
104
+ break;
105
+ }
106
+ case 'delete': {
107
+ event?.preventDefault();
108
+ openAlert({
109
+ title: 'Are you sure?',
110
+ icon: Icon.confirm,
111
+ onResult: (result: Result) => {
112
+ if (result.isConfirmed) {
113
+ (new Requester).fetch({
114
+ url: generateRoute(onClickAction.action.route, {...action.parameters, ...onClickAction.parameters}),
115
+ method: Method.DELETE
116
+ }).catch((e:any) => {
117
+ console.log('error', e);
118
+ }).finally(() => {
119
+ refresh();
120
+ });
121
+ }
122
+ }
123
+ });
124
+ return;
125
+ }
126
+ default: {
127
+ if (embedded) {
128
+ event?.preventDefault();
129
+ openModal({
130
+ action: onClickAction, props: {
131
+ onClose: () => {
132
+ refresh();
133
+ }
134
+ }
135
+ });
136
+ }
137
+ return;
138
+ }
139
+ }
140
+
141
+ filterData();
142
+ }
143
+
144
+ useEffect(() => {
145
+ setQueryParameters(searchParams);
146
+ }, [location]);
147
+
148
+ return (
149
+ <>
150
+ <section className={"list"}>
151
+ <header className="content-header d-md-flex mb-3 justify-content-between align-items-center">
152
+ <h2>
153
+ {results?.title}
154
+ </h2>
155
+ <div className={"d-flex align-items-center"}>
156
+ {!!actions.length && (
157
+ <div className="btn-group btn-group-sm me-2">
158
+ {actions.map((item, index) => (
159
+ <Link
160
+ key={index}
161
+ to={generateRoute(item.route, action.parameters)}
162
+ onClick={(event) => handleAction({
163
+ action: item,
164
+ parameters: action.parameters
165
+ }, event)}
166
+ className="btn btn-outline-secondary">
167
+ {item.title || item.name}
168
+ </Link>
169
+ ))}
170
+ </div>
171
+ )}
172
+ {results?.form?.filter && (
173
+ <div className={"btn-group btn-group-sm"}>
174
+ <Dropdown className={"btn-group btn-group-sm"}>
175
+ <DropdownButton className={"btn-outline-dark"}><T>Filter</T></DropdownButton>
176
+ <DropdownContent>
177
+ <div className="filter">
178
+ <Form
179
+ id={"filter_" + nameToId(entity)}
180
+ ref={filterFormRef}
181
+ onSubmit={(formData: FormData) => handleAction({
182
+ action: {
183
+ name: 'filter',
184
+ object: false,
185
+ namespace: action.action.namespace,
186
+ entity: entity
187
+ }, parameters: convertFormDataToObject(formData)
188
+ })}
189
+ onReset={() => handleAction({
190
+ action: {
191
+ name: 'filter',
192
+ object: false,
193
+ namespace: action.action.namespace,
194
+ entity: entity
195
+ }
196
+ })}
197
+ >
198
+ {
199
+ results?.form?.filter && (
200
+ <FormView view={results.form.filter.view}/>
201
+ )
202
+ }
203
+ <button className={"btn btn-primary me-2"} type={"submit"}>
204
+ <T>Submit</T>
205
+ </button>
206
+ </Form>
207
+ </div>
208
+ </DropdownContent>
209
+ </Dropdown>
210
+ {!!Object.values(filter.current?.filter || []).length && (
211
+ <Button onClick={() => {
212
+ filterFormRef.current?.reset();
213
+ }} className="btn btn-outline-dark">x</Button>
214
+ )}
215
+ </div>
216
+ )}
217
+ </div>
218
+ </header>
219
+ {results?.form?.filter && (
220
+ <FiltersView formView={results.form.filter.view} onClick={(key) => filterData([key])}/>
221
+ )}
222
+ <DynamicView namespace={action.action.namespace} key={"modify"} prefix={"modify"} view={"content"} data={results}>
223
+ <div className={"table-responsive"}>
224
+ <GridTableView
225
+ data={results}
226
+ onClick={handleAction}
227
+ onBatchClick={handleBatchAction}
228
+ namespace={action.action.namespace}
229
+ routeParams={action.parameters}/>
230
+ </div>
231
+ {results && <PaginatorView meta={results.entity.data.meta}/>}
232
+ </DynamicView>
233
+ </section>
234
+ </>
235
+ );
236
+ })
237
+
238
+ const FiltersView = ({formView, onClick}: { formView: FormViewType, onClick: (key: string) => void }) => {
239
+ const getValue = (view: FormViewType) => {
240
+ if (view.choices !== undefined) {
241
+ return (view.choices ? Object.values(view.data instanceof Object ? view.data : [view.data]).map((k: any) => view.choices?.[k]?.label ?? k).join(', ') : view.data);
242
+ } else if (view.checked !== undefined) {
243
+ return view.checked ? 'Yes' : 'No';
244
+ }
245
+
246
+ return view.data;
247
+ }
248
+
249
+ return (
250
+ <div className={"filters d-flex mb-sm overflow-auto"}>
251
+ {Object.values<FormViewType>(formView.children || []).filter(item => item.data !== null).map((item, index) => (
252
+ <div key={index} className="filters-item d-flex text-nowrap flex-column me-2 mb-2">
253
+ <small className="mb-2">{item.label}</small>
254
+ <div className="btn btn-sm btn-primary me-1 mb-1">
255
+ {getValue(item)}
256
+ {onClick && (
257
+ <span onClick={() => onClick(item.name)} className={"ms-2"}>
258
+ &times;
259
+ </span>
260
+ )}
261
+ </div>
262
+ </div>
263
+ ))}
264
+ </div>
265
+ )
266
+ }
267
+
268
+ export default List;
@@ -0,0 +1,156 @@
1
+ import React, {ReactNode, useEffect, useRef} from "react";
2
+ import {Link, useParams} from "react-router-dom";
3
+ import TemplateBlock from "@src/component/TemplateBlock.tsx";
4
+ import {UseActions} from "@src/context/ActionContext.tsx";
5
+ import ModifyForm, {ModifyFormRefType} from "@src/component/ModifyForm.tsx";
6
+ import {ModifyType} from "@src/type/ModifyType.tsx";
7
+ import GetData from "@src/component/hooks/GetData.tsx";
8
+ import {ActionType} from "@src/type/ActionType.tsx";
9
+ import TemplateExtend from "@src/component/TemplateExtend.tsx";
10
+ import Modal, {ModalRefType} from "@src/component/Modal.tsx";
11
+ import {generateRoute} from "@src/helper/RouterUtils.tsx";
12
+ import {Icon as AlertIcon, UseAlert} from "@src/context/AlertContext.tsx";
13
+ import {ExceptionType} from "@src/type/ExceptionType.tsx";
14
+ import {OnClickAction} from "@src/component/crud/GridTableView.tsx";
15
+ import DynamicView from "@src/component/crud/DynamicView.tsx";
16
+
17
+ const DefaultModifyTemplate = ({action, children, routeParams, results}: {
18
+ action: ActionType;
19
+ children?: ReactNode;
20
+ routeParams?: { [key: string]: any };
21
+ results?: ModifyType;
22
+ }) => {
23
+
24
+ const {getAction} = UseActions();
25
+ const listAction = getAction(action.entity, 'list', action.namespace);
26
+
27
+ return (
28
+ <section className="edit">
29
+ <header>
30
+ <h2 className="title">
31
+ {listAction && (
32
+ <Link to={generateRoute(listAction.route, routeParams)}>&larr;</Link>
33
+ )}
34
+ <TemplateBlock name={"title"} content={children} data={results}/>
35
+ </h2>
36
+ <nav>
37
+ <TemplateBlock name={"navigation"} content={children} data={results}/>
38
+ </nav>
39
+ </header>
40
+
41
+ <main>
42
+ <TemplateBlock name={"content"} content={children} data={results}/>
43
+ </main>
44
+ </section>
45
+ )
46
+ }
47
+
48
+ const Modify = ({action, children, onSuccess, modal, props}: {
49
+ action: OnClickAction;
50
+ children?: ReactNode;
51
+ onSuccess?: (event: CustomEvent, data: ModifyType) => void;
52
+ modal?: boolean;
53
+ props?: any;
54
+ }) => {
55
+ const routeParams = {...(action.parameters || {}), ...useParams()}
56
+ const modifyFormRef = useRef<ModifyFormRefType>(undefined);
57
+ const modalRef = useRef<ModalRefType>(undefined);
58
+ const {results, setParameters}: any & { results: ModifyType } = GetData({
59
+ entityAction: action.action,
60
+ initParameters: routeParams
61
+ });
62
+ const {open: openAlert} = UseAlert();
63
+
64
+ useEffect(() => {
65
+ setParameters(routeParams);
66
+ }, [JSON.stringify(routeParams)]);
67
+
68
+ useEffect(() => {
69
+ modalRef.current?.open();
70
+ }, [JSON.stringify(results)]);
71
+
72
+ const ComponentTemplate = modal ? Modal : DefaultModifyTemplate;
73
+
74
+ return (
75
+ <ComponentTemplate ref={modalRef} {...props} action={action} routeParams={routeParams}>
76
+ <TemplateExtend name={"title"}>
77
+ <TemplateBlock name={"title"} content={children} data={results}>
78
+ {results?.title || 'Title'}
79
+ </TemplateBlock>
80
+ </TemplateExtend>
81
+
82
+ <TemplateExtend name={"navigation"}>
83
+ <DynamicView namespace={action.action.namespace} view={"navigation"} prefix={"modify"}>
84
+ <TemplateBlock name={"navigation"} content={children} data={results}>
85
+ original {action.action.namespace}
86
+ </TemplateBlock>
87
+ </DynamicView>
88
+ </TemplateExtend>
89
+
90
+ <TemplateExtend name={"content"}>
91
+ <DynamicView namespace={action.action.namespace} view={"content"} prefix={"modify"}>
92
+ <TemplateBlock name={"content"} content={children} data={results}>
93
+ <ModifyForm
94
+ ref={modifyFormRef}
95
+ data={results}
96
+ action={action.action}
97
+ onSuccess={(data: ModifyType) => {
98
+ modalRef.current?.close();
99
+ const event = new CustomEvent('success', {detail: data})
100
+ onSuccess && onSuccess(event, data);
101
+
102
+ if (event.defaultPrevented) {
103
+ return;
104
+ }
105
+
106
+ openAlert({
107
+ title: 'Success',
108
+ text: Object.values(data.messages?.success ?? []).join(' ') ?? 'Item was saved successful!',
109
+ icon: AlertIcon.success,
110
+ actions: {
111
+ close: {
112
+ label: 'OK'
113
+ }
114
+ }
115
+ });
116
+ }}
117
+ onError={(error: ExceptionType) => {
118
+ openAlert({
119
+ title: error.status + ' ' + error.detail,
120
+ text: error.detail,
121
+ icon: AlertIcon.denied,
122
+ actions: {
123
+ close: {
124
+ label: 'OK'
125
+ }
126
+ }
127
+ });
128
+ }}
129
+ onLoad={() => {
130
+ console.log('loaded');
131
+ // modalRef.current?.open();
132
+ }}
133
+ embedded={modal}
134
+ parameters={routeParams}
135
+ >
136
+ {modal && <TemplateExtend name={"actions"}></TemplateExtend>}
137
+ </ModifyForm>
138
+ </TemplateBlock>
139
+ </DynamicView>
140
+ </TemplateExtend>
141
+ <TemplateExtend name={"actions"}>
142
+ <TemplateBlock name={"actions"} content={children} data={results}>
143
+ <button
144
+ type={"submit"}
145
+ className={"btn btn-primary"}
146
+ onClick={() => modifyFormRef.current?.getFormRef()?.submit()}>
147
+ Submit
148
+ </button>
149
+ </TemplateBlock>
150
+ </TemplateExtend>
151
+
152
+ </ComponentTemplate>
153
+ );
154
+ };
155
+
156
+ export default Modify;
@@ -0,0 +1,2 @@
1
+ /// <reference types="react-scripts" />
2
+ declare module '*';
@@ -0,0 +1,5 @@
1
+ // jest-dom adds custom jest matchers for asserting on DOM nodes.
2
+ // allows you to do things like:
3
+ // expect(element).toHaveTextContent(/react/i)
4
+ // learn more: https://github.com/testing-library/jest-dom
5
+ import '@testing-library/jest-dom';
@@ -0,0 +1,10 @@
1
+ import {RouteType} from "@src/type/RouteType";
2
+
3
+ export type ActionType = {
4
+ entity: string;
5
+ namespace?: string;
6
+ name: string;
7
+ title?: string;
8
+ object?: boolean;
9
+ route?: RouteType
10
+ }
@@ -0,0 +1,18 @@
1
+ export type ColumnType = {
2
+ field: string;
3
+ label: string;
4
+ options: string[];
5
+ searchable: boolean | null;
6
+ sortable: boolean;
7
+ identifier: boolean;
8
+ value?: CallableFunction;
9
+ group?: string | false | null;
10
+ view?: {
11
+ header?: {
12
+ attr?: {[key: string]: string}
13
+ },
14
+ content?: {
15
+ attr?: {[key: string]: string}
16
+ }
17
+ }
18
+ }
@@ -0,0 +1,7 @@
1
+ import {ExceptionTraceType} from "@src/component/error/HttpException.tsx";
2
+
3
+ export type ExceptionType = {
4
+ status: number;
5
+ detail: string;
6
+ trace?: ExceptionTraceType[]
7
+ }
@@ -0,0 +1,6 @@
1
+ import {FormViewType} from "@src/type/FormViewType";
2
+
3
+ export type FormType = {
4
+ view: FormViewType;
5
+ message?: string;
6
+ }