forked from dmm-com/pagoda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityControlMenu.tsx
145 lines (138 loc) · 4.22 KB
/
EntityControlMenu.tsx
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutline";
import {
ListItemIcon,
ListItemText,
Menu,
MenuItem,
Typography,
} from "@mui/material";
import { useSnackbar } from "notistack";
import React, { FC } from "react";
import { Link, useNavigate } from "react-router-dom";
import { RateLimitedClickable } from "../common/RateLimitedClickable";
import {
aclPath,
entityHistoryPath,
editEntityPath,
entitiesPath,
restoreEntryPath,
topPath,
entityEntriesPath,
aclHistoryPath,
} from "Routes";
import { Confirmable } from "components/common/Confirmable";
import { aironeApiClient } from "repository/AironeApiClient";
type ExportFormatType = "YAML" | "CSV";
interface Props {
entityId: number;
anchorElem: HTMLButtonElement | null;
handleClose: (entityId: number) => void;
setOpenImportModal: (isOpened: boolean) => void;
setToggle?: () => void;
}
export const EntityControlMenu: FC<Props> = ({
entityId,
anchorElem,
handleClose,
setOpenImportModal,
setToggle,
}) => {
const { enqueueSnackbar } = useSnackbar();
const navigate = useNavigate();
const handleDelete = async (entityId: number) => {
await aironeApiClient
.deleteEntity(entityId)
.then(() => {
enqueueSnackbar("モデルの削除が完了しました", {
variant: "success",
});
// A magic to reload the entity list with keeping snackbar
navigate(topPath(), { replace: true });
navigate(entitiesPath(), { replace: true });
setToggle && setToggle();
})
.catch(() => {
enqueueSnackbar("モデルの削除が失敗しました", {
variant: "error",
});
});
};
const handleExport = async (entityId: number, format: ExportFormatType) => {
try {
await aironeApiClient.exportEntries(entityId, format);
enqueueSnackbar("モデルのエクスポートのジョブ登録が成功しました", {
variant: "success",
});
} catch (e) {
enqueueSnackbar("モデルのエクスポートのジョブ登録が失敗しました", {
variant: "error",
});
}
};
return (
<Menu
id={`entityControlMenu-${entityId}`}
open={Boolean(anchorElem)}
onClose={() => handleClose(entityId)}
anchorEl={anchorElem}
anchorOrigin={{
vertical: "bottom",
horizontal: "right",
}}
transformOrigin={{
vertical: "top",
horizontal: "right",
}}
>
<MenuItem component={Link} to={entityEntriesPath(entityId)}>
<Typography>アイテム一覧</Typography>
</MenuItem>
<MenuItem component={Link} to={editEntityPath(entityId)}>
<Typography>編集</Typography>
</MenuItem>
<MenuItem component={Link} to={aclPath(entityId)}>
<Typography>ACL 設定</Typography>
</MenuItem>
<MenuItem component={Link} to={entityHistoryPath(entityId)}>
<Typography>変更履歴</Typography>
</MenuItem>
<MenuItem component={Link} to={aclHistoryPath(entityId)}>
<Typography>ACL 変更履歴</Typography>
</MenuItem>
<RateLimitedClickable
intervalSec={5}
onClick={handleExport.bind(null, entityId, "YAML")}
>
<MenuItem>
<Typography>エクスポート(YAML)</Typography>
</MenuItem>
</RateLimitedClickable>
<RateLimitedClickable
intervalSec={5}
onClick={handleExport.bind(null, entityId, "CSV")}
>
<MenuItem>
<Typography>エクスポート(CSV)</Typography>
</MenuItem>
</RateLimitedClickable>
<MenuItem onClick={() => setOpenImportModal(true)}>
<Typography>インポート</Typography>
</MenuItem>
<MenuItem component={Link} to={restoreEntryPath(entityId)}>
<Typography>削除アイテムの復旧</Typography>
</MenuItem>
<Confirmable
componentGenerator={(handleOpen) => (
<MenuItem onClick={handleOpen}>
<ListItemText>削除</ListItemText>
<ListItemIcon>
<DeleteOutlineIcon />
</ListItemIcon>
</MenuItem>
)}
dialogTitle="本当に削除しますか?"
onClickYes={() => handleDelete(entityId)}
/>
</Menu>
);
};