MSP-3391 add error handler for roles and users

This commit is contained in:
veselovnk committed 2024-06-04 15:38:41 +03:00
1 parent 6d95da4f20
commit fecc74f6e0
4 files changed
+56 -14

No files matched your search

+4 -2
View File
@@ -1,4 +1,4 @@
import { Divider, Table, message } from 'antd'
import { Divider, message, Table } from 'antd'
import { ColumnsType } from 'antd/es/table'
import { FormComponents } from 'isp-ui-kit'
import { useEffect, useState } from 'react'
@@ -19,6 +19,7 @@ import { PermissionKeysType, RoleType } from '@type/roles.type.ts'
import './roles-content.scss'
const newRole: Partial<RoleType> = {
externalGroup: '',
name: '',
@@ -46,6 +47,7 @@ const RolesContent = ({
const {
control,
formState: { isDirty },
setError,
getValues,
reset
} = useForm<RoleType>({
@@ -79,7 +81,7 @@ const RolesContent = ({
}
const formValue = getValues()
formValue.changeMessage = changeMessage
saveRole(formValue)
saveRole(formValue, setError)
setOpenModal(false)
}
@@ -1,3 +1,5 @@
import { UseFormSetError } from 'react-hook-form'
import { NewRoleType, PermissionType, RoleType } from '@type/roles.type.ts'
export interface RolesContentPropsType {
@@ -5,5 +7,8 @@ export interface RolesContentPropsType {
title?: string
immutable?: boolean
permissions: PermissionType[]
saveRole: (formValue: RoleType | NewRoleType) => void
saveRole: (
formValue: RoleType | NewRoleType,
setError: UseFormSetError<RoleType | NewRoleType>
) => void
}
+27 -6
View File
@@ -1,6 +1,7 @@
import { List, Spin, message, Tooltip } from 'antd'
import { List, message, Spin, Tooltip } from 'antd'
import { Layout } from 'isp-ui-kit'
import { useEffect } from 'react'
import { UseFormSetError } from 'react-hook-form'
import {
createSearchParams,
useLocation,
@@ -24,6 +25,7 @@ import { NewRoleType, PermissionKeysType, RoleType } from '@type/roles.type.ts'
import './roles-page.scss'
const { Column, EmptyData, NoData } = Layout
const RolesPage = () => {
@@ -89,25 +91,44 @@ const RolesPage = () => {
const handeOnAddItem = () => setSelectedItemId('new')
const handleUpdateRole = (formValue: RoleType | NewRoleType) => {
const handleUpdateRole = (
formValue: RoleType | NewRoleType,
setError: UseFormSetError<RoleType>
) => {
updateRole(formValue as RoleType)
.unwrap()
.then(() => {
message.success('Элемент успешно добавлен').then()
})
.catch(() => message.error('Не удалось сохранить роль'))
.catch((error) => {
const { status } = error
if (status === 409) {
setError('name', {
message: 'Роль с таким названием уже существует'
})
}
message.error('Не удалось сохранить роль')
})
}
const handleCreateRole = (formValue: NewRoleType) => {
const handleCreateRole = (
formValue: NewRoleType,
setError: UseFormSetError<NewRoleType>
) => {
createRole(formValue)
.unwrap()
.then((res) => {
message.success('Элемент успешно добавлен').then()
setSelectedItemId(res.id.toString())
})
.catch(() => {
.catch((error) => {
const { status } = error
if (status === 409) {
setError('name', {
message: 'Роль с таким названием уже существует'
})
}
message.error('Не удалось добавить роль').then()
setSelectedItemId('')
})
}
+19 -5
View File
@@ -52,7 +52,7 @@ const UserEditor = () => {
getValues,
setValue,
watch,
reset
setError
} = useForm<UserType>({
defaultValues: isNew
? {
@@ -108,7 +108,13 @@ const UserEditor = () => {
message.success('Пользователь успешно создан')
navigate(routePaths.users)
})
.catch(() => {
.catch((error) => {
const { status } = error
if (status === 409) {
setError('email', {
message: 'Пользователь с таким Email уже существует'
})
}
message.error('Произошла ошибка создания пользователя')
})
} else {
@@ -118,9 +124,15 @@ const UserEditor = () => {
.unwrap()
.then(() => {
message.success('Пользователь успешно изменен')
reset({}, { keepValues: true })
navigate(routePaths.users)
})
.catch(() => {
.catch((error) => {
const { status } = error
if (status === 409) {
setError('email', {
message: 'Пользователь с таким Email уже существует'
})
}
message.error('Произошла ошибка обновления пользователя')
})
}
@@ -169,7 +181,9 @@ const UserEditor = () => {
{state.lastSessionCreatedAt && (
<>
Последняя сессия &nbsp;
{dayjs(state.lastSessionCreatedAt).format(dateFormats.fullFormat)}
{dayjs(state.lastSessionCreatedAt).format(
dateFormats.fullFormat
)}
</>
)}
</span>