1- import React , { useState , useCallback , useEffect } from 'react'
1+ import { useState , useCallback , useEffect } from 'react'
22import { useParams } from 'react-router-dom'
33import { useTranslation } from 'react-i18next'
44import Box from '@mui/material/Box'
@@ -22,8 +22,7 @@ import {
2222import useSnackbarAlert from '~/hooks/use-snackbar-alert'
2323import useQuery from '~/hooks/use-query'
2424import useMutation from '~/hooks/use-mutation'
25-
26- type NoteAction = 'create' | 'delete' | 'update' | 'duplicate'
25+ import { noteActionMap , NoteAction } from './constant'
2726
2827const CooperationNotes : React . FC = ( ) => {
2928 const { t } = useTranslation ( )
@@ -35,25 +34,12 @@ const CooperationNotes: React.FC = () => {
3534
3635 const onSuccessResponse = useCallback (
3736 ( noteAction : NoteAction ) => {
38- let responseMessage = ''
37+ const responseMessage = noteActionMap [ noteAction ]
3938
4039 if ( noteAction === 'create' ) {
41- responseMessage = 'cooperationsPage.modalMessages.successCreation'
4240 setOpen ( false )
4341 }
4442
45- if ( noteAction === 'delete' ) {
46- responseMessage = 'cooperationsPage.modalMessages.successDeletion'
47- }
48-
49- if ( noteAction === 'update' ) {
50- responseMessage = 'cooperationsPage.modalMessages.successUpdating'
51- }
52-
53- if ( noteAction === 'duplicate' ) {
54- responseMessage = 'cooperationsPage.modalMessages.successDuplication'
55- }
56-
5743 handleAlert ( {
5844 severity : snackbarVariants . success ,
5945 message : responseMessage
@@ -124,16 +110,16 @@ const CooperationNotes: React.FC = () => {
124110 } )
125111 }
126112
127- const updateSelectedNote = ( params : {
128- noteId : string
129- data : CreateOrUpdateNoteParams
130- } ) => {
131- return CooperationNotesService . updateNote (
132- cooperationId ,
133- params . noteId ,
134- params . data
135- )
136- }
113+ const updateSelectedNote = useCallback (
114+ ( params : { noteId : string ; data : CreateOrUpdateNoteParams } ) => {
115+ return CooperationNotesService . updateNote (
116+ cooperationId ,
117+ params . noteId ,
118+ params . data
119+ )
120+ } ,
121+ [ cooperationId ]
122+ )
137123
138124 const { mutate : updateNote , isPending : updateNoteLoading } = useMutation ( {
139125 mutationFn : updateSelectedNote ,
@@ -142,15 +128,18 @@ const CooperationNotes: React.FC = () => {
142128 onSuccess : ( ) => onSuccessResponse ( 'update' )
143129 } )
144130
145- const duplicateNote = ( noteId : string ) => {
146- const note = notes ?. find ( ( item ) => item . _id === noteId )
131+ const duplicateNote = useCallback (
132+ ( noteId : string ) => {
133+ const note = notes ?. find ( ( item ) => item . _id === noteId )
147134
148- if ( ! note ) {
149- throw new Error ( 'Note with specified ID was not found' )
150- }
135+ if ( ! note ) {
136+ throw new Error ( 'Note with specified ID was not found' )
137+ }
151138
152- return CooperationNotesService . createNote ( note , cooperationId )
153- }
139+ return CooperationNotesService . createNote ( note , cooperationId )
140+ } ,
141+ [ cooperationId , notes ]
142+ )
154143
155144 const { mutate : duplicateItem } = useMutation ( {
156145 mutationFn : duplicateNote ,
0 commit comments