MSP-3105 create UserEditor
This commit is contained in:
1 parent
76f2e6d3c3
commit
8cd3b86a2c
2 files changed
+296
No files matched your search
@@ -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<UserType>({
|
||||
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(
|
||||
<>
|
||||
Роль: <strong>{exclusiveRole.name}</strong> несовместима с
|
||||
другими ролями
|
||||
</>
|
||||
)
|
||||
}
|
||||
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 <Spin />
|
||||
}
|
||||
return (
|
||||
<section className="user-editor">
|
||||
<header className="user-editor__header">
|
||||
<div className="user-editor__header__nav">
|
||||
<Button
|
||||
data-cy="user-editor__header__nav__backBtn"
|
||||
className="user-editor__header__nav__backBtn"
|
||||
type="text"
|
||||
onClick={() => navigate(routePaths.users)}
|
||||
icon={<ArrowLeftOutlined />}
|
||||
/>
|
||||
|
||||
<h2 className="user-editor__header__name">
|
||||
{isNew ? 'Новый пользователь' : `${state.firstName}`}
|
||||
</h2>
|
||||
<Button
|
||||
disabled={!isDirty || !isSaveBtnAvailable}
|
||||
data-cy="user-editor__header__btn"
|
||||
className="user-editor__header__btn"
|
||||
onClick={() => sendForm()}
|
||||
type="primary"
|
||||
icon={<SaveOutlined />}
|
||||
>
|
||||
Сохранить
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{isNew ? (
|
||||
''
|
||||
) : (
|
||||
<span data-cy="user-editor__status" className="user-editor__status">
|
||||
{state.lastSessionCreatedAt && (
|
||||
<>
|
||||
Последняя сессия
|
||||
{new Date(state.lastSessionCreatedAt).toLocaleDateString()}
|
||||
|
||||
{new Date(state.lastSessionCreatedAt).toLocaleTimeString()}
|
||||
</>
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
</header>
|
||||
|
||||
<form className="user-editor__content">
|
||||
<div className="user-editor__content__wrap">
|
||||
<div className="user-editor__content__name">
|
||||
<FormComponents.FormInput
|
||||
label="Пользователь"
|
||||
rules={{ required: ValidationRules.required }}
|
||||
control={control}
|
||||
name="firstName"
|
||||
/>
|
||||
</div>
|
||||
<div className="user-editor__content__description">
|
||||
<FormComponents.FormInput
|
||||
label="Описание"
|
||||
control={control}
|
||||
name="description"
|
||||
/>
|
||||
</div>
|
||||
<div className="user-editor__content__email">
|
||||
<FormComponents.FormInput
|
||||
label="Email"
|
||||
rules={{ required: ValidationRules.required }}
|
||||
control={control}
|
||||
name="email"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{isNew && (
|
||||
<div className="user-editor__content__password">
|
||||
<FormComponents.FormInputPassword
|
||||
label="Пароль"
|
||||
rules={{ required: ValidationRules.required }}
|
||||
control={control}
|
||||
name="password"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="user-editor__content__roles">
|
||||
<FormComponents.FormSelect
|
||||
label="Добавить роль"
|
||||
optionFilterProp="label"
|
||||
disabled={!hasPermission(PermissionKeysType.write)}
|
||||
control={control}
|
||||
name="roles"
|
||||
options={createSelectOptions(roles)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
export default UserEditor
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user