|
@@ -5,16 +5,20 @@ import {
|
|
FormControlLabel,
|
|
FormControlLabel,
|
|
Typography,
|
|
Typography,
|
|
} from '@mui/material';
|
|
} from '@mui/material';
|
|
-import { FC, useMemo, useState } from 'react';
|
|
|
|
|
|
+import { FC, useMemo, useState, useContext } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useTranslation } from 'react-i18next';
|
|
import DialogTemplate from '@/components/customDialog/DialogTemplate';
|
|
import DialogTemplate from '@/components/customDialog/DialogTemplate';
|
|
import CustomInput from '@/components/customInput/CustomInput';
|
|
import CustomInput from '@/components/customInput/CustomInput';
|
|
|
|
+import { authContext } from '@/context';
|
|
import { useFormValidation } from '@/hooks';
|
|
import { useFormValidation } from '@/hooks';
|
|
import { formatForm } from '@/utils';
|
|
import { formatForm } from '@/utils';
|
|
import { makeStyles } from '@mui/styles';
|
|
import { makeStyles } from '@mui/styles';
|
|
import type { CreateUserProps, CreateUserParams } from '../Types';
|
|
import type { CreateUserProps, CreateUserParams } from '../Types';
|
|
import type { Option as RoleOption } from '@/components/customSelector/Types';
|
|
import type { Option as RoleOption } from '@/components/customSelector/Types';
|
|
-import type { ITextfieldConfig } from '@/components/customInput/Types';
|
|
|
|
|
|
+import type {
|
|
|
|
+ ITextfieldConfig,
|
|
|
|
+ IValidation,
|
|
|
|
+} from '@/components/customInput/Types';
|
|
|
|
|
|
const useStyles = makeStyles((theme: Theme) => ({
|
|
const useStyles = makeStyles((theme: Theme) => ({
|
|
input: {
|
|
input: {
|
|
@@ -33,12 +37,16 @@ const CreateUser: FC<CreateUserProps> = ({
|
|
handleClose,
|
|
handleClose,
|
|
roleOptions,
|
|
roleOptions,
|
|
}) => {
|
|
}) => {
|
|
|
|
+ // context
|
|
|
|
+ const { isDedicated } = useContext(authContext);
|
|
|
|
+ // i18n
|
|
const { t: commonTrans } = useTranslation();
|
|
const { t: commonTrans } = useTranslation();
|
|
const { t: userTrans } = useTranslation('user');
|
|
const { t: userTrans } = useTranslation('user');
|
|
const { t: btnTrans } = useTranslation('btn');
|
|
const { t: btnTrans } = useTranslation('btn');
|
|
const { t: warningTrans } = useTranslation('warning');
|
|
const { t: warningTrans } = useTranslation('warning');
|
|
const attuTrans = commonTrans('attu');
|
|
const attuTrans = commonTrans('attu');
|
|
|
|
|
|
|
|
+ // UI states
|
|
const [form, setForm] = useState<CreateUserParams>({
|
|
const [form, setForm] = useState<CreateUserParams>({
|
|
username: '',
|
|
username: '',
|
|
password: '',
|
|
password: '',
|
|
@@ -52,12 +60,54 @@ const CreateUser: FC<CreateUserProps> = ({
|
|
|
|
|
|
const { validation, checkIsValid, disabled } = useFormValidation(checkedForm);
|
|
const { validation, checkIsValid, disabled } = useFormValidation(checkedForm);
|
|
|
|
|
|
|
|
+ // styles
|
|
const classes = useStyles();
|
|
const classes = useStyles();
|
|
|
|
|
|
|
|
+ // UI handlers
|
|
const handleInputChange = (key: 'username' | 'password', value: string) => {
|
|
const handleInputChange = (key: 'username' | 'password', value: string) => {
|
|
setForm(v => ({ ...v, [key]: value }));
|
|
setForm(v => ({ ...v, [key]: value }));
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+ const opensourceUserPassRule: IValidation[] = [
|
|
|
|
+ {
|
|
|
|
+ rule: 'valueLength',
|
|
|
|
+ errorText: warningTrans('valueLength', {
|
|
|
|
+ name: attuTrans.password,
|
|
|
|
+ min: 6,
|
|
|
|
+ max: 256,
|
|
|
|
+ }),
|
|
|
|
+ extraParam: {
|
|
|
|
+ min: 6,
|
|
|
|
+ max: 256,
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ const cloudUserPassRule: IValidation[] = [
|
|
|
|
+ {
|
|
|
|
+ rule: 'require',
|
|
|
|
+ errorText: warningTrans('required', {
|
|
|
|
+ name: attuTrans.password,
|
|
|
|
+ }),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ rule: 'valueLength',
|
|
|
|
+ errorText: warningTrans('valueLength', {
|
|
|
|
+ name: attuTrans.password,
|
|
|
|
+ min: 8,
|
|
|
|
+ max: 64,
|
|
|
|
+ }),
|
|
|
|
+ extraParam: {
|
|
|
|
+ min: 8,
|
|
|
|
+ max: 64,
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ rule: 'cloudPassword',
|
|
|
|
+ errorText: warningTrans('cloudPassword'),
|
|
|
|
+ },
|
|
|
|
+ ];
|
|
|
|
+
|
|
const createConfigs: ITextfieldConfig[] = [
|
|
const createConfigs: ITextfieldConfig[] = [
|
|
{
|
|
{
|
|
label: attuTrans.username,
|
|
label: attuTrans.username,
|
|
@@ -90,26 +140,7 @@ const CreateUser: FC<CreateUserProps> = ({
|
|
placeholder: attuTrans.password,
|
|
placeholder: attuTrans.password,
|
|
fullWidth: true,
|
|
fullWidth: true,
|
|
type: 'password',
|
|
type: 'password',
|
|
- validations: [
|
|
|
|
- {
|
|
|
|
- rule: 'require',
|
|
|
|
- errorText: warningTrans('required', {
|
|
|
|
- name: attuTrans.password,
|
|
|
|
- }),
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- rule: 'valueLength',
|
|
|
|
- errorText: warningTrans('valueLength', {
|
|
|
|
- name: attuTrans.password,
|
|
|
|
- min: 6,
|
|
|
|
- max: 256,
|
|
|
|
- }),
|
|
|
|
- extraParam: {
|
|
|
|
- min: 6,
|
|
|
|
- max: 256,
|
|
|
|
- },
|
|
|
|
- },
|
|
|
|
- ],
|
|
|
|
|
|
+ validations: !isDedicated ? opensourceUserPassRule : cloudUserPassRule,
|
|
defaultValue: form.username,
|
|
defaultValue: form.username,
|
|
},
|
|
},
|
|
];
|
|
];
|