-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditor.tsx
More file actions
97 lines (87 loc) · 3.53 KB
/
editor.tsx
File metadata and controls
97 lines (87 loc) · 3.53 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
import { useBackendAdminClient, useResetUserPasswordMutation } from "@frontend/common/src/hooks/useAdminAPI";
import { KeyOff } from "@mui/icons-material";
import { Button, ButtonProps, CircularProgress, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle } from "@mui/material";
import { ErrorBoundary, Suspense } from "@suspensive/react";
import * as React from "react";
import { useNavigate, useParams } from "react-router-dom";
import { PasswordResultDialog } from "./password_result_dialog";
import { addErrorSnackbar } from "../../../utils/snackbar";
import { ErrorFallback } from "../../elements/error_fallback";
import { AdminEditor } from "../../layouts/admin_editor";
type PageStateType = {
isConfirmDialogOpen: boolean;
isResultDialogOpen: boolean;
newPassword: string | null;
createdUserId: string | null;
};
export const AdminUserExtEditor: React.FC = ErrorBoundary.with(
{ fallback: ErrorFallback },
Suspense.with({ fallback: <CircularProgress /> }, () => {
const { id } = useParams<{ id?: string }>();
const navigate = useNavigate();
const [pageState, setPageState] = React.useState<PageStateType>({
isConfirmDialogOpen: false,
isResultDialogOpen: false,
newPassword: null,
createdUserId: null,
});
const openConfirmDialog = () => setPageState((ps) => ({ ...ps, isConfirmDialogOpen: true }));
const closeConfirmDialog = () => setPageState((ps) => ({ ...ps, isConfirmDialogOpen: false }));
const closeResultDialog = () => {
const userId = pageState.createdUserId;
setPageState((ps) => ({ ...ps, isResultDialogOpen: false, newPassword: null, createdUserId: null }));
if (userId) navigate(`/user/userext/${userId}`);
};
const backendAdminClient = useBackendAdminClient();
const useResetPasswordMutation = useResetUserPasswordMutation(backendAdminClient, id || "");
const resetUserPassword = () => {
closeConfirmDialog();
if (id) {
useResetPasswordMutation.mutate(undefined, {
onSuccess: (data) => {
setPageState((ps) => ({
...ps,
isResultDialogOpen: true,
newPassword: data.password,
}));
},
onError: addErrorSnackbar,
});
}
};
const onCreated = (data: Record<string, string>) => {
setPageState((ps) => ({
...ps,
isResultDialogOpen: true,
newPassword: data.password,
createdUserId: data.id,
}));
};
const resetUserPasswordButton: ButtonProps = {
variant: "outlined",
color: "error",
size: "small",
startIcon: <KeyOff />,
children: "비밀번호 초기화",
onClick: () => id && openConfirmDialog(),
};
return (
<>
<Dialog open={pageState.isConfirmDialogOpen}>
<DialogTitle>비밀번호 초기화</DialogTitle>
<DialogContent>
<DialogContentText>정말 이 사용자의 비밀번호를 초기화하시겠습니까?</DialogContentText>
</DialogContent>
<DialogActions>
<Button color="error" onClick={closeConfirmDialog} autoFocus>
취소
</Button>
<Button onClick={resetUserPassword}>초기화</Button>
</DialogActions>
</Dialog>
<PasswordResultDialog open={pageState.isResultDialogOpen} password={pageState.newPassword} onClose={closeResultDialog} />
<AdminEditor app="user" resource="userext" id={id} extraActions={[resetUserPasswordButton]} onCreated={onCreated} />
</>
);
})
);