diff --git a/src/pages/UserEditor/index.tsx b/src/pages/UserEditor/index.tsx new file mode 100644 index 0000000..7f3227c --- /dev/null +++ b/src/pages/UserEditor/index.tsx @@ -0,0 +1,230 @@ +import { ArrowLeftOutlined, SaveOutlined } from '@ant-design/icons' +import { Button, message, Spin } from 'antd' +import { FormComponents } from 'isp-ui-kit' +import { useEffect } from 'react' +import { useForm } from 'react-hook-form' +import { useLocation, useNavigate } from 'react-router-dom' + +import { ValidationRules } from '@constants/form/validationRules.ts' + +import { findExclusiveRole } from '@utils/roleUtils.ts' + +import useRole from '@hooks/useRole.tsx' + +import roleApi from '@services/roleService.ts' +import userServiceApi from '@services/userService.ts' + +import { routePaths } from '@routes/routePaths.ts' + +import { PermissionKeysType, RoleType } from '@type/roles.type.ts' +import { UserType } from '@type/user.type.ts' + +import { LabelItem } from 'isp-ui-kit/dist/FormComponents/formTypes' + +import './user-editor.scss' + +const UserEditor = () => { + const location = useLocation() + const navigate = useNavigate() + const state = location.state as UserType + const isNew = location.pathname.includes('new') + const fromApp = location.state?.fromApp || false + const { hasPermission } = useRole() + + const hasCreateUserPermission = hasPermission(PermissionKeysType.write) + const hasUpdateUserPermission = hasPermission(PermissionKeysType.write) + + const [createUser] = userServiceApi.useCreateUserMutation() + const { data: roles = [], isLoading } = roleApi.useGetAllRolesQuery() + + const [updateUser] = userServiceApi.useUpdateUserMutation() + + const isSaveBtnAvailable = isNew + ? hasCreateUserPermission + : hasUpdateUserPermission + + const { + control, + formState: { isDirty }, + getValues, + setValue, + watch, + reset + } = useForm({ + defaultValues: isNew + ? { + description: '', + email: '', + firstName: '', + password: '', + roles: [] + } + : state, + mode: 'onChange' + }) + + useEffect(() => { + if (isNew && !fromApp) { + navigate(routePaths.users) + } + }, [fromApp]) + + useEffect(() => { + const subscription = watch((value, { name, type }) => { + if (name === 'roles' && type === 'change') { + const exclusiveRole = findExclusiveRole(roles, value.roles as number[]) + if (exclusiveRole) { + if (value.roles && value.roles.length > 1) { + message.info( + <> + Роль: {exclusiveRole.name} несовместима с + другими ролями + + ) + } + setValue('roles', [exclusiveRole.id]) + } + } + }) + return () => subscription.unsubscribe() + }, [watch]) + + const sendForm = () => { + if (isNew) { + const newUser = { + email: getValues().email, + password: getValues().password, + description: getValues().description, + firstName: getValues().firstName, + lastName: getValues().lastName, + roles: getValues().roles + } + createUser(newUser) + .unwrap() + .then(() => { + message.success('Пользователь успешно создан') + navigate(routePaths.users) + }) + .catch(() => { + message.error('Произошла ошибка создания пользователя') + }) + } else { + const formData = getValues() + delete formData.lastSessionCreatedAt + updateUser(formData) + .unwrap() + .then(() => { + message.success('Пользователь успешно изменен') + reset({}, { keepValues: true }) + }) + .catch(() => { + message.error('Произошла ошибка обновления пользователя') + }) + } + } + const createSelectOptions = (role: RoleType[]) => { + const options: LabelItem[] = role.map((r) => ({ + value: r.id, + label: r.name + })) + return options + } + if (isLoading) { + return + } + return ( +
+
+
+ +
+ + {isNew ? ( + '' + ) : ( + + {state.lastSessionCreatedAt && ( + <> + Последняя сессия   + {new Date(state.lastSessionCreatedAt).toLocaleDateString()} +   + {new Date(state.lastSessionCreatedAt).toLocaleTimeString()} + + )} + + )} +
+ +
+
+
+ +
+
+ +
+
+ +
+ + {isNew && ( +
+ +
+ )} + +
+ +
+
+
+
+ ) +} +export default UserEditor diff --git a/src/pages/UserEditor/user-editor.scss b/src/pages/UserEditor/user-editor.scss new file mode 100644 index 0000000..2b5f2a4 --- /dev/null +++ b/src/pages/UserEditor/user-editor.scss @@ -0,0 +1,66 @@ +@import "../../constants/style/styleConst"; + +.user-editor { + background: #fff; + height: 100%; + border: 1px solid $border-color; + + &__header { + border-bottom: 1px solid $border-color; + padding: $basic-padding *4 $basic-padding*3; + + &__nav { + display: flex; + align-items: center; + margin-bottom: $basic-margin* 2.5; + + &__backBtn { + margin-left: -$basic-margin; + margin-right: $basic-margin; + } + } + + &__name { + font-family: 'Roboto', sans-serif; + font-style: normal; + font-weight: 500; + font-size: 20px; + line-height: 28px; + color: #000; + + } + + &__btn { + margin-left: auto; + } + } + + &__status { + font-family: 'Roboto', sans-serif; + font-size: 16px; + line-height: 24px; + } + + &__content { + &__wrap { + overflow-x: scroll; + height: calc(100vh - 200px); + } + + .ant-form-item { + margin: 0; + } + + &__name, &__description, &__email, &__roles, &__password { + padding: $basic-padding*1.5 $basic-padding*3; + + h3 { + padding-bottom: $basic-padding*1; + font-family: 'Roboto', sans-serif; + font-weight: 400; + font-size: 14px; + line-height: 22px + } + } + } +}