11import { useParams , useNavigate } from 'react-router-dom'
2- import { useEditPersonMutation , useGetPersonDetailsQuery } from '../../redux/personReducer'
2+ import {
3+ useEditPersonMutation ,
4+ useGetPersonDetailsIdMutation ,
5+ useGetPersonDetailsQuery ,
6+ } from '../../redux/personReducer'
37import { CircularProgress } from '@mui/material'
48import { DetailView , TabType } from '../DetailView/DetailView'
59import { PersonTab } from './Tabs/PersonTab'
@@ -8,6 +12,7 @@ import { EditDataType, PersonDetailsType, Role, ValidationErrors } from '@/share
812import { validatePerson } from '@/shared/validators/person'
913import { useNotify } from '@/hooks/notification'
1014import { useEffect } from 'react'
15+ import { emptyPerson } from '../DetailView/common/defaultValues'
1116
1217export const PersonDetails = ( ) => {
1318 const { id : idFromUrl } = useParams ( )
@@ -21,8 +26,10 @@ export const PersonDetails = () => {
2126 // We designate special id 'user-page' instead of normal initials to mean current user's own page.
2227 const isUserPage = idFromUrl === 'user-page'
2328 const id = isUserPage ? user . initials : idFromUrl
29+ const isNew = idFromUrl === 'new'
2430
25- const { isLoading, isError, data } = useGetPersonDetailsQuery ( id ! )
31+ const { isLoading, isError, data } = useGetPersonDetailsQuery ( id ! , { skip : isNew } )
32+ const [ getPersonDetailsId ] = useGetPersonDetailsIdMutation ( )
2633
2734 useEffect ( ( ) => {
2835 if ( ! isUserPage && user . role !== Role . Admin ) {
@@ -32,8 +39,23 @@ export const PersonDetails = () => {
3239 // eslint-disable-next-line react-hooks/exhaustive-deps
3340 } , [ ] )
3441
42+ const personExists = async ( initials : string ) => {
43+ try {
44+ const isPerson = await getPersonDetailsId ( initials ) . unwrap ( )
45+ if ( isPerson ) return true
46+ return false
47+ } catch {
48+ return false
49+ }
50+ }
51+
3552 const onWrite = async ( editData : EditDataType < PersonDetailsType > ) => {
53+ if ( ! editData . initials ) return
3654 try {
55+ if ( isNew && ( await personExists ( editData . initials ) ) ) {
56+ notify ( 'Initials already exists. Select Edit.' , 'error' )
57+ return
58+ }
3759 const { initials } = await editPersonRequest ( editData ) . unwrap ( )
3860 notify ( 'Saved person successfully.' )
3961 if ( isUserPage ) {
@@ -48,9 +70,9 @@ export const PersonDetails = () => {
4870 }
4971
5072 if ( isError ) return < div > Error loading data</ div >
51- if ( isLoading || ! data || mutationLoading ) return < CircularProgress />
73+ if ( isLoading || ( ! data && ! isNew ) || mutationLoading ) return < CircularProgress />
5274
53- document . title = `User - ${ data . user ?. user_name } `
75+ document . title = isNew ? 'New person' : `User - ${ data ! . user ?. user_name } `
5476
5577 const tabs : TabType [ ] = [
5678 {
@@ -62,10 +84,11 @@ export const PersonDetails = () => {
6284 return (
6385 < DetailView
6486 onWrite = { onWrite }
87+ isNew = { isNew }
6588 isUserPage = { isUserPage }
6689 isPersonPage = { true }
6790 tabs = { tabs }
68- data = { data }
91+ data = { isNew ? emptyPerson : data ! }
6992 validator = { validatePerson }
7093 />
7194 )
0 commit comments