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'
@@ -23,8 +23,7 @@ import {
2323import useSnackbarAlert from '~/hooks/use-snackbar-alert'
2424import useQuery from '~/hooks/use-query'
2525import useMutation from '~/hooks/use-mutation'
26-
27- type NoteAction = 'create' | 'delete' | 'update' | 'duplicate'
26+ import { noteActionMap , NoteAction } from './constant'
2827
2928const CooperationNotes : React . FC = ( ) => {
3029 const { t } = useTranslation ( )
@@ -36,25 +35,12 @@ const CooperationNotes: React.FC = () => {
3635
3736 const onSuccessResponse = useCallback (
3837 ( noteAction : NoteAction ) => {
39- let responseMessage = ''
38+ const responseMessage = noteActionMap [ noteAction ]
4039
4140 if ( noteAction === 'create' ) {
42- responseMessage = 'cooperationsPage.modalMessages.successCreation'
4341 setOpen ( false )
4442 }
4543
46- if ( noteAction === 'delete' ) {
47- responseMessage = 'cooperationsPage.modalMessages.successDeletion'
48- }
49-
50- if ( noteAction === 'update' ) {
51- responseMessage = 'cooperationsPage.modalMessages.successUpdating'
52- }
53-
54- if ( noteAction === 'duplicate' ) {
55- responseMessage = 'cooperationsPage.modalMessages.successDuplication'
56- }
57-
5844 handleAlert ( {
5945 severity : snackbarVariants . success ,
6046 message : responseMessage
@@ -125,16 +111,16 @@ const CooperationNotes: React.FC = () => {
125111 } )
126112 }
127113
128- const updateSelectedNote = ( params : {
129- noteId : string
130- data : CreateOrUpdateNoteParams
131- } ) => {
132- return CooperationNotesService . updateNote (
133- cooperationId ,
134- params . noteId ,
135- params . data
136- )
137- }
114+ const updateSelectedNote = useCallback (
115+ ( params : { noteId : string ; data : CreateOrUpdateNoteParams } ) => {
116+ return CooperationNotesService . updateNote (
117+ cooperationId ,
118+ params . noteId ,
119+ params . data
120+ )
121+ } ,
122+ [ cooperationId ]
123+ )
138124
139125 const { mutate : updateNote , isPending : updateNoteLoading } = useMutation ( {
140126 mutationFn : updateSelectedNote ,
@@ -143,15 +129,18 @@ const CooperationNotes: React.FC = () => {
143129 onSuccess : ( ) => onSuccessResponse ( 'update' )
144130 } )
145131
146- const duplicateNote = ( noteId : string ) => {
147- const note = notes ?. find ( ( item ) => item . _id === noteId )
132+ const duplicateNote = useCallback (
133+ ( noteId : string ) => {
134+ const note = notes ?. find ( ( item ) => item . _id === noteId )
148135
149- if ( ! note ) {
150- throw new Error ( 'Note with specified ID was not found' )
151- }
136+ if ( ! note ) {
137+ throw new Error ( 'Note with specified ID was not found' )
138+ }
152139
153- return CooperationNotesService . createNote ( note , cooperationId )
154- }
140+ return CooperationNotesService . createNote ( note , cooperationId )
141+ } ,
142+ [ cooperationId , notes ]
143+ )
155144
156145 const { mutate : duplicateItem } = useMutation ( {
157146 mutationFn : duplicateNote ,
0 commit comments