@ -16,7 +16,7 @@ import Page from '../../components/basic/Page';
import DepartmentsAgentsTable from './DepartmentsAgentsTable' ;
import { formsSubscription } from '../additionalForms' ;
import { useComponentDidUpdate } from '../../hooks/useComponentDidUpdate' ;
import { isEmail } from '../../../app/utils' ;
export default function EditDepartmentWithData ( { id , reload , title } ) {
const t = useTranslation ( ) ;
@ -63,7 +63,7 @@ export function EditDepartment({ data, id, title, reload }) {
const [ tags , setTags ] = useState ( ( department && department . chatClosingTags ) || [ ] ) ;
const [ tagsText , setTagsText ] = useState ( ) ;
const { values , handlers } = useForm ( {
const { values , handlers , hasUnsavedChanges } = useForm ( {
name : ( department && department . name ) || '' ,
email : ( department && department . email ) || '' ,
description : ( department && department . description ) || '' ,
@ -147,6 +147,9 @@ export function EditDepartment({ data, id, title, reload }) {
useComponentDidUpdate ( ( ) => {
setEmailError ( ! email ? t ( 'The_field_is_required' , 'email' ) : '' ) ;
} , [ t , email ] ) ;
useComponentDidUpdate ( ( ) => {
setEmailError ( ! isEmail ( email ) ? t ( 'Validate_email_address' ) : '' ) ;
} , [ t , email ] ) ;
useComponentDidUpdate ( ( ) => {
setTagError ( requestTagBeforeClosingChat && ( ! tags || tags . length === 0 ) ? t ( 'The_field_is_required' , 'name' ) : '' ) ;
} , [ requestTagBeforeClosingChat , t , tags ] ) ;
@ -162,6 +165,10 @@ export function EditDepartment({ data, id, title, reload }) {
setEmailError ( t ( 'The_field_is_required' , 'email' ) ) ;
error = true ;
}
if ( ! isEmail ( email ) ) {
setEmailError ( t ( 'Validate_email_address' ) ) ;
error = true ;
}
if ( requestTagBeforeClosingChat && ( ! tags || tags . length === 0 ) ) {
setTagError ( t ( 'The_field_is_required' , 'tags' ) ) ;
error = true ;
@ -203,7 +210,7 @@ export function EditDepartment({ data, id, title, reload }) {
} else {
await saveDepartmentInfo ( id , payload , agentList ) ;
}
dispatchToastMessage ( { type : 'success' , message : t ( 's aved' ) } ) ;
dispatchToastMessage ( { type : 'success' , message : t ( 'S aved' ) } ) ;
reload ( ) ;
agentsRoute . push ( { } ) ;
} catch ( error ) {
@ -214,8 +221,7 @@ export function EditDepartment({ data, id, title, reload }) {
const handleReturn = useMutableCallback ( ( ) => {
router . push ( { } ) ;
} ) ;
const invalidForm = ! name || ! email || ( requestTagBeforeClosingChat && ( ! tags || tags . length === 0 ) ) ;
const invalidForm = ! name || ! email || ! isEmail ( email ) || ! hasUnsavedChanges || ( requestTagBeforeClosingChat && ( ! tags || tags . length === 0 ) ) ;
const formId = useUniqueId ( ) ;
@ -223,7 +229,7 @@ export function EditDepartment({ data, id, title, reload }) {
< Page >
< Page . Header title = { title } >
< ButtonGroup >
< Button onClick = { handleReturn } > { t ( 'Back' ) } < / B u t t o n >
< Button onClick = { handleReturn } > < Icon name = 'back' / > { t ( 'Back' ) } < / B u t t o n >
< Button type = 'submit' form = { formId } primary disabled = { invalidForm } > { t ( 'Save' ) } < / B u t t o n >
< / B u t t o n G r o u p >
< / P a g e . H e a d e r >
@ -277,11 +283,21 @@ export function EditDepartment({ data, id, title, reload }) {
< SelectFiltered flexGrow = { 1 } options = { channelOpts } value = { offlineMessageChannelName } onChange = { handleOfflineMessageChannelName } placeholder = { t ( 'Channel_name' ) } / >
< / F i e l d . R o w >
< / F i e l d >
{ MaxChats && < MaxChats value = { maxNumberSimultaneousChat } handler = { handleMaxNumberSimultaneousChat } label = { 'Max_number_of_chats_per_agent' } placeholder = 'Max_number_of_chats_per_agent_description' / > }
{ VisitorInactivity && < VisitorInactivity value = { visitorInactivityTimeoutInSeconds } handler = { handleVisitorInactivityTimeoutInSeconds } label = { 'How_long_to_wait_to_consider_visitor_abandonment_in_seconds' } placeholder = 'Number_in_seconds' / > }
{ AbandonedMessageInput && < AbandonedMessageInput value = { abandonedRoomsCloseCustomMessage } handler = { handleAbandonedRoomsCloseCustomMessage } label = { 'Livechat_abandoned_rooms_closed_custom_message' } placeholder = 'Enter_a_custom_message' / > }
{ WaitingQueueMessageInput && < WaitingQueueMessageInput value = { waitingQueueMessage } handler = { handleWaitingQueueMessage } label = { 'Waiting_queue_message' } / > }
{ DepartmentForwarding && < DepartmentForwarding value = { departmentsAllowedToForward } handler = { handleDepartmentsAllowedToForward } label = { 'List_of_departments_for_forward_description' } placeholder = 'Enter_a_department_name' / > }
{ MaxChats && < Field >
< MaxChats value = { maxNumberSimultaneousChat } handler = { handleMaxNumberSimultaneousChat } label = { 'Max_number_of_chats_per_agent' } placeholder = 'Max_number_of_chats_per_agent_description' / >
< / F i e l d > }
{ VisitorInactivity && < Field >
< VisitorInactivity value = { visitorInactivityTimeoutInSeconds } handler = { handleVisitorInactivityTimeoutInSeconds } label = { 'How_long_to_wait_to_consider_visitor_abandonment_in_seconds' } placeholder = 'Number_in_seconds' / >
< / F i e l d > }
{ AbandonedMessageInput && < Field >
< AbandonedMessageInput value = { abandonedRoomsCloseCustomMessage } handler = { handleAbandonedRoomsCloseCustomMessage } label = { 'Livechat_abandoned_rooms_closed_custom_message' } placeholder = 'Enter_a_custom_message' / >
< / F i e l d > }
{ WaitingQueueMessageInput && < Field >
< WaitingQueueMessageInput value = { waitingQueueMessage } handler = { handleWaitingQueueMessage } label = { 'Waiting_queue_message' } / >
< / F i e l d > }
{ DepartmentForwarding && < Field >
< DepartmentForwarding value = { departmentsAllowedToForward } handler = { handleDepartmentsAllowedToForward } label = { 'List_of_departments_for_forward' } placeholder = 'Enter_a_department_name' / >
< / F i e l d > }
< Field >
< Box display = 'flex' flexDirection = 'row' >
< Field . Label > { t ( 'Request_tag_before_closing_chat' ) } < / F i e l d . L a b e l >
@ -303,7 +319,9 @@ export function EditDepartment({ data, id, title, reload }) {
{ tags . map ( ( tag , i ) => < Chip key = { i } onClick = { handleTagChipClick ( tag ) } mie = 'x8' > { tag } < / C h i p > ) }
< / F i e l d . R o w > }
< / F i e l d > }
{ DepartmentBusinessHours && < DepartmentBusinessHours bhId = { department && department . businessHourId } / > }
{ DepartmentBusinessHours && < Field >
< DepartmentBusinessHours bhId = { department && department . businessHourId } / >
< / F i e l d > }
< Divider mb = 'x16' / >
< Field >
< Field . Label mb = 'x4' > { t ( 'Agents' ) } : < / F i e l d . L a b e l >