User.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { UserHttp } from '../../http/User';
  2. import React, { useContext, useEffect, useState } from 'react';
  3. import AttuGrid from 'insight_src/components/grid/Grid';
  4. import {
  5. ColDefinitionsType,
  6. ToolBarConfig,
  7. } from 'insight_src/components/grid/Types';
  8. import { makeStyles, Theme } from '@material-ui/core';
  9. import {
  10. CreateUserParams,
  11. DeleteUserParams,
  12. UpdateUserParams,
  13. UserData,
  14. } from './Types';
  15. import { rootContext } from 'insight_src/context/Root';
  16. import CreateUser from './Create';
  17. import { useTranslation } from 'react-i18next';
  18. import DeleteTemplate from 'insight_src/components/customDialog/DeleteDialogTemplate';
  19. import UpdateUser from './Update';
  20. import { useNavigationHook } from 'insight_src/hooks/Navigation';
  21. import { ALL_ROUTER_TYPES } from 'insight_src/router/Types';
  22. const useStyles = makeStyles((theme: Theme) => ({
  23. actionButton: {
  24. position: 'relative',
  25. left: ' -10px',
  26. '& .MuiButton-root': {
  27. color: theme.palette.primary.main,
  28. },
  29. },
  30. }));
  31. const Users = () => {
  32. useNavigationHook(ALL_ROUTER_TYPES.USER);
  33. const classes = useStyles();
  34. const [users, setUsers] = useState<UserData[]>([]);
  35. const [selectedUser, setSelectedUser] = useState<UserData[]>([]);
  36. const { setDialog, handleCloseDialog, openSnackBar } =
  37. useContext(rootContext);
  38. const { t: successTrans } = useTranslation('success');
  39. const { t: userTrans } = useTranslation('user');
  40. const { t: btnTrans } = useTranslation('btn');
  41. const { t: dialogTrans } = useTranslation('dialog');
  42. const fetchUsers = async () => {
  43. const res = await UserHttp.getUsers();
  44. setUsers(res.usernames.map((v: string) => ({ name: v })));
  45. };
  46. const handleCreate = async (data: CreateUserParams) => {
  47. await UserHttp.createUser(data);
  48. fetchUsers();
  49. openSnackBar(successTrans('create', { name: userTrans('user') }));
  50. handleCloseDialog();
  51. };
  52. const handleUpdate = async (data: UpdateUserParams) => {
  53. await UserHttp.updateUser(data);
  54. fetchUsers();
  55. openSnackBar(successTrans('update', { name: userTrans('user') }));
  56. handleCloseDialog();
  57. };
  58. const handleDelete = async () => {
  59. for (const user of selectedUser) {
  60. const param: DeleteUserParams = {
  61. username: user.name,
  62. };
  63. await UserHttp.deleteUser(param);
  64. }
  65. openSnackBar(successTrans('delete', { name: userTrans('user') }));
  66. fetchUsers();
  67. handleCloseDialog();
  68. };
  69. const toolbarConfigs: ToolBarConfig[] = [
  70. {
  71. label: 'Create user',
  72. onClick: () => {
  73. setDialog({
  74. open: true,
  75. type: 'custom',
  76. params: {
  77. component: (
  78. <CreateUser
  79. handleCreate={handleCreate}
  80. handleClose={handleCloseDialog}
  81. />
  82. ),
  83. },
  84. });
  85. },
  86. icon: 'add',
  87. },
  88. {
  89. type: 'iconBtn',
  90. onClick: () => {
  91. setDialog({
  92. open: true,
  93. type: 'custom',
  94. params: {
  95. component: (
  96. <DeleteTemplate
  97. label={btnTrans('delete')}
  98. title={dialogTrans('deleteTitle', { type: userTrans('user') })}
  99. text={userTrans('deleteWarning')}
  100. handleDelete={handleDelete}
  101. />
  102. ),
  103. },
  104. });
  105. },
  106. label: '',
  107. icon: 'delete',
  108. },
  109. ];
  110. const colDefinitions: ColDefinitionsType[] = [
  111. {
  112. id: 'name',
  113. align: 'left',
  114. disablePadding: false,
  115. label: 'Name',
  116. },
  117. {
  118. id: 'action',
  119. disablePadding: false,
  120. label: 'Action',
  121. showActionCell: true,
  122. actionBarConfigs: [
  123. {
  124. onClick: (e: React.MouseEvent, row: UserData) => {
  125. setDialog({
  126. open: true,
  127. type: 'custom',
  128. params: {
  129. component: (
  130. <UpdateUser
  131. username={row.name}
  132. handleUpdate={handleUpdate}
  133. handleClose={handleCloseDialog}
  134. />
  135. ),
  136. },
  137. });
  138. },
  139. text: 'Update password',
  140. className: classes.actionButton,
  141. },
  142. ],
  143. },
  144. ];
  145. const handleSelectChange = (value: UserData[]) => {
  146. setSelectedUser(value);
  147. };
  148. useEffect(() => {
  149. fetchUsers();
  150. }, []);
  151. return (
  152. <div className="page-wrapper">
  153. <AttuGrid
  154. toolbarConfigs={toolbarConfigs}
  155. colDefinitions={colDefinitions}
  156. rows={users}
  157. rowCount={users.length}
  158. primaryKey="name"
  159. showPagination={false}
  160. selected={selectedUser}
  161. setSelected={handleSelectChange}
  162. // page={currentPage}
  163. // onChangePage={handlePageChange}
  164. // rowsPerPage={pageSize}
  165. // setRowsPerPage={handlePageSize}
  166. // isLoading={loading}
  167. // order={order}
  168. // orderBy={orderBy}
  169. // handleSort={handleGridSort}
  170. />
  171. </div>
  172. );
  173. };
  174. export default Users;