CreateUser.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import {
  2. makeStyles,
  3. Theme,
  4. Checkbox,
  5. FormGroup,
  6. FormControlLabel,
  7. Typography,
  8. } from '@material-ui/core';
  9. import { FC, useMemo, useState } from 'react';
  10. import { useTranslation } from 'react-i18next';
  11. import DialogTemplate from '@/components/customDialog/DialogTemplate';
  12. import CustomInput from '@/components/customInput/CustomInput';
  13. import { ITextfieldConfig } from '@/components/customInput/Types';
  14. import { useFormValidation } from '@/hooks/Form';
  15. import { formatForm } from '@/utils/Form';
  16. import { CreateUserProps, CreateUserParams } from './Types';
  17. const useStyles = makeStyles((theme: Theme) => ({
  18. input: {
  19. margin: theme.spacing(2, 0, 0.5),
  20. },
  21. dialogWrapper: {
  22. maxWidth: theme.spacing(70),
  23. '& .MuiFormControlLabel-root': {
  24. width: theme.spacing(20),
  25. },
  26. },
  27. }));
  28. const CreateUser: FC<CreateUserProps> = ({
  29. handleCreate,
  30. handleClose,
  31. roles,
  32. }) => {
  33. const { t: commonTrans } = useTranslation();
  34. const { t: userTrans } = useTranslation('user');
  35. const { t: btnTrans } = useTranslation('btn');
  36. const { t: warningTrans } = useTranslation('warning');
  37. const attuTrans = commonTrans('attu');
  38. const [form, setForm] = useState<CreateUserParams>({
  39. username: '',
  40. password: '',
  41. roles: [],
  42. });
  43. // selected Role
  44. const checkedForm = useMemo(() => {
  45. return formatForm(form);
  46. }, [form]);
  47. const { validation, checkIsValid, disabled } = useFormValidation(checkedForm);
  48. const classes = useStyles();
  49. const handleInputChange = (key: 'username' | 'password', value: string) => {
  50. setForm(v => ({ ...v, [key]: value }));
  51. };
  52. const createConfigs: ITextfieldConfig[] = [
  53. {
  54. label: attuTrans.username,
  55. key: 'username',
  56. onChange: (value: string) => handleInputChange('username', value),
  57. variant: 'filled',
  58. className: classes.input,
  59. placeholder: attuTrans.username,
  60. fullWidth: true,
  61. validations: [
  62. {
  63. rule: 'require',
  64. errorText: warningTrans('required', {
  65. name: attuTrans.username,
  66. }),
  67. },
  68. ],
  69. defaultValue: form.username,
  70. },
  71. {
  72. label: attuTrans.password,
  73. key: 'password',
  74. onChange: (value: string) => handleInputChange('password', value),
  75. variant: 'filled',
  76. className: classes.input,
  77. placeholder: attuTrans.password,
  78. fullWidth: true,
  79. type: 'password',
  80. validations: [
  81. {
  82. rule: 'require',
  83. errorText: warningTrans('required', {
  84. name: attuTrans.password,
  85. }),
  86. },
  87. ],
  88. defaultValue: form.username,
  89. },
  90. ];
  91. const handleCreateUser = () => {
  92. handleCreate(form);
  93. };
  94. return (
  95. <DialogTemplate
  96. title={userTrans('createTitle')}
  97. handleClose={handleClose}
  98. confirmLabel={btnTrans('create')}
  99. handleConfirm={handleCreateUser}
  100. confirmDisabled={disabled}
  101. dialogClass={classes.dialogWrapper}
  102. >
  103. <>
  104. {createConfigs.map(v => (
  105. <CustomInput
  106. type="text"
  107. textConfig={v}
  108. checkValid={checkIsValid}
  109. validInfo={validation}
  110. key={v.label}
  111. />
  112. ))}
  113. <Typography variant="h5" component="span">
  114. {userTrans('roles')}
  115. </Typography>
  116. <FormGroup row>
  117. {roles.map((r: any, index: number) => (
  118. <FormControlLabel
  119. control={
  120. <Checkbox
  121. onChange={(e: React.ChangeEvent<HTMLInputElement>, checked: boolean) => {
  122. let newRoles = [...form.roles];
  123. if (!checked) {
  124. newRoles = newRoles.filter(
  125. (n: string | number) => n === r.vlaue
  126. );
  127. } else {
  128. newRoles.push(r.value);
  129. }
  130. setForm(v => ({ ...v, roles: [...newRoles] }));
  131. }}
  132. />
  133. }
  134. key={index}
  135. label={r.label}
  136. value={r.value}
  137. />
  138. ))}
  139. </FormGroup>
  140. </>
  141. </DialogTemplate>
  142. );
  143. };
  144. export default CreateUser;