|
@@ -0,0 +1,150 @@
|
|
|
+import { UserHttp } from '../../http/User';
|
|
|
+import React, { useContext, useEffect, useState } from 'react';
|
|
|
+import AttuGrid from 'insight_src/components/grid/Grid';
|
|
|
+import {
|
|
|
+ ColDefinitionsType,
|
|
|
+ ToolBarConfig,
|
|
|
+} from 'insight_src/components/grid/Types';
|
|
|
+import { makeStyles, Theme } from '@material-ui/core';
|
|
|
+import { CreateUserParams, DeleteUserParams, UserData } from './Types';
|
|
|
+import { rootContext } from 'insight_src/context/Root';
|
|
|
+import CreateUser from './Create';
|
|
|
+import { useTranslation } from 'react-i18next';
|
|
|
+import DeleteTemplate from 'insight_src/components/customDialog/DeleteDialogTemplate';
|
|
|
+
|
|
|
+const useStyles = makeStyles((theme: Theme) => ({
|
|
|
+ wrapper: {
|
|
|
+ height: '100%',
|
|
|
+ },
|
|
|
+ icon: {
|
|
|
+ fontSize: '20px',
|
|
|
+ marginLeft: theme.spacing(0.5),
|
|
|
+ },
|
|
|
+ highlight: {
|
|
|
+ color: theme.palette.primary.main,
|
|
|
+ backgroundColor: 'transparent',
|
|
|
+ },
|
|
|
+}));
|
|
|
+
|
|
|
+const Users = () => {
|
|
|
+ const classes = useStyles();
|
|
|
+ const [users, setUsers] = useState<UserData[]>([]);
|
|
|
+ const [selectedUser, setSelectedUser] = useState<UserData[]>([]);
|
|
|
+ const { setDialog, handleCloseDialog, openSnackBar } =
|
|
|
+ useContext(rootContext);
|
|
|
+ const { t: successTrans } = useTranslation('success');
|
|
|
+ const { t: userTrans } = useTranslation('user');
|
|
|
+ const { t: btnTrans } = useTranslation('btn');
|
|
|
+ const { t: dialogTrans } = useTranslation('dialog');
|
|
|
+
|
|
|
+ const fetchUsers = async () => {
|
|
|
+ const res = await UserHttp.getUsers();
|
|
|
+
|
|
|
+ setUsers(res.usernames.map((v: string) => ({ name: v })));
|
|
|
+ };
|
|
|
+
|
|
|
+ const handleCreate = async (data: CreateUserParams) => {
|
|
|
+ await UserHttp.createUser(data);
|
|
|
+ fetchUsers();
|
|
|
+ openSnackBar(successTrans('create', { name: userTrans('user') }));
|
|
|
+ handleCloseDialog();
|
|
|
+ };
|
|
|
+
|
|
|
+ const handleDelete = async () => {
|
|
|
+ for (const user of selectedUser) {
|
|
|
+ const param: DeleteUserParams = {
|
|
|
+ username: user.name,
|
|
|
+ };
|
|
|
+ await UserHttp.deleteUser(param);
|
|
|
+ }
|
|
|
+
|
|
|
+ openSnackBar(successTrans('delete', { name: userTrans('user') }));
|
|
|
+ fetchUsers();
|
|
|
+ handleCloseDialog();
|
|
|
+ };
|
|
|
+
|
|
|
+ const toolbarConfigs: ToolBarConfig[] = [
|
|
|
+ {
|
|
|
+ label: 'Create user',
|
|
|
+ onClick: () => {
|
|
|
+ setDialog({
|
|
|
+ open: true,
|
|
|
+ type: 'custom',
|
|
|
+ params: {
|
|
|
+ component: (
|
|
|
+ <CreateUser
|
|
|
+ handleCreate={handleCreate}
|
|
|
+ handleClose={handleCloseDialog}
|
|
|
+ />
|
|
|
+ ),
|
|
|
+ },
|
|
|
+ });
|
|
|
+ },
|
|
|
+ icon: 'add',
|
|
|
+ },
|
|
|
+
|
|
|
+ {
|
|
|
+ type: 'iconBtn',
|
|
|
+ onClick: () => {
|
|
|
+ setDialog({
|
|
|
+ open: true,
|
|
|
+ type: 'custom',
|
|
|
+ params: {
|
|
|
+ component: (
|
|
|
+ <DeleteTemplate
|
|
|
+ label={btnTrans('delete')}
|
|
|
+ title={dialogTrans('deleteTitle', { type: userTrans('user') })}
|
|
|
+ text={userTrans('deleteWarning')}
|
|
|
+ handleDelete={handleDelete}
|
|
|
+ />
|
|
|
+ ),
|
|
|
+ },
|
|
|
+ });
|
|
|
+ },
|
|
|
+ label: '',
|
|
|
+ icon: 'delete',
|
|
|
+ },
|
|
|
+ ];
|
|
|
+
|
|
|
+ const colDefinitions: ColDefinitionsType[] = [
|
|
|
+ {
|
|
|
+ id: 'name',
|
|
|
+ align: 'left',
|
|
|
+ disablePadding: false,
|
|
|
+ label: 'Name',
|
|
|
+ },
|
|
|
+ ];
|
|
|
+
|
|
|
+ const handleSelectChange = (value: UserData[]) => {
|
|
|
+ setSelectedUser(value);
|
|
|
+ };
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ fetchUsers();
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div className="page-wrapper">
|
|
|
+ <AttuGrid
|
|
|
+ toolbarConfigs={toolbarConfigs}
|
|
|
+ colDefinitions={colDefinitions}
|
|
|
+ rows={users}
|
|
|
+ rowCount={users.length}
|
|
|
+ primaryKey="name"
|
|
|
+ showPagination={false}
|
|
|
+ selected={selectedUser}
|
|
|
+ setSelected={handleSelectChange}
|
|
|
+ // page={currentPage}
|
|
|
+ // onChangePage={handlePageChange}
|
|
|
+ // rowsPerPage={pageSize}
|
|
|
+ // setRowsPerPage={handlePageSize}
|
|
|
+ // isLoading={loading}
|
|
|
+ // order={order}
|
|
|
+ // orderBy={orderBy}
|
|
|
+ // handleSort={handleGridSort}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+};
|
|
|
+
|
|
|
+export default Users;
|