-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEmployeePage.tsx
More file actions
107 lines (98 loc) · 2.56 KB
/
EmployeePage.tsx
File metadata and controls
107 lines (98 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {
FetchView,
IOutlet,
OutletView,
getErrorMessage,
trycatch,
useSubject,
} from "react-declarative";
import HistoryPage from "./pages/HistoryPage";
import ListPage from "./pages/common/ListPage";
import OnePage from "./pages/OnePage";
import hasRouteMatch from "../../../utils/hasRouteMatch";
import ioc from "../../../lib/ioc";
import { useEffect } from "react";
interface IEmployeePageProps {
id?: string;
}
const routes: IOutlet[] = [
{
id: "list",
element: ListPage,
isActive: (pathname) =>
hasRouteMatch(["/employee_active", "/employee_inactive"], pathname),
isAvailable: () => false,
},
{
id: "employee",
element: OnePage,
isActive: (pathname) => {
return hasRouteMatch(
["/employee/:id", "/employee/:id/employee"],
pathname
);
},
},
{
id: "history",
element: HistoryPage,
isActive: (pathname) =>
hasRouteMatch(["/employee/:id/history"], pathname),
},
];
export const EmployeePage = ({ id = "never" }: IEmployeePageProps) => {
const handleSubmit = async (data: Record<string, any>) => {
let isOk = true;
try {
await ioc.employeeViewService.update(id, data.employee);
ioc.alertService.notify("Saved");
} catch (error) {
isOk = false;
const msg = getErrorMessage(error);
ioc.alertService.notify(msg);
} finally {
return isOk;
}
};
const fetchState = async () => [
await trycatch(ioc.employeeViewService.read)(id),
await ioc.permissionService.getFeatures(),
await ioc.permissionService.getVisibility(),
];
const changeSubject = useSubject();
useEffect(
() =>
ioc.employeeViewService.updateSubject.subscribe(([chunk_id, chunk]) => {
if (id !== chunk_id) {
return;
}
changeSubject.next(["employee", chunk]);
}),
[]
);
return (
<FetchView state={fetchState} fallback={ioc.errorService.handleGlobalError}>
{async ([employee, features, visibility]) => (
<OutletView
changeSubject={changeSubject}
history={ioc.routerService}
onLoadStart={() => ioc.layoutService.setAppbarLoader(true)}
onLoadEnd={() => ioc.layoutService.setAppbarLoader(false)}
routes={routes}
params={{ id }}
initialData={{
employee,
}}
payload={() => ({
features,
visibility,
id,
})}
onChange={console.log}
onSubmit={handleSubmit}
/>
)}
</FetchView>
);
};
export default EmployeePage;