Skip to content

Commit fc35099

Browse files
committed
fixed comments
1 parent 8e0b339 commit fc35099

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

src/containers/my-cooperations/cooperation-notes/CooperationNotes.tsx

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useCallback, useEffect } from 'react'
1+
import { useState, useCallback, useEffect } from 'react'
22
import { useParams } from 'react-router-dom'
33
import { useTranslation } from 'react-i18next'
44
import Box from '@mui/material/Box'
@@ -22,8 +22,7 @@ import {
2222
import useSnackbarAlert from '~/hooks/use-snackbar-alert'
2323
import useQuery from '~/hooks/use-query'
2424
import useMutation from '~/hooks/use-mutation'
25-
26-
type NoteAction = 'create' | 'delete' | 'update' | 'duplicate'
25+
import { noteActionMap, NoteAction } from './constant'
2726

2827
const 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,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type NoteAction = 'create' | 'delete' | 'update' | 'duplicate'
2+
3+
export const noteActionMap: Record<NoteAction, string> = {
4+
create: 'cooperationsPage.modalMessages.successCreation',
5+
delete: 'cooperationsPage.modalMessages.successDeletion',
6+
duplicate: 'cooperationsPage.modalMessages.successDuplication',
7+
update: 'cooperationsPage.modalMessages.successUpdating'
8+
}

tests/unit/containers/my-cooperations/cooperation-notes/CooperationNotes.spec.jsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,14 @@ const appMain = {
5858
appMain: { userRole: 'tutor', userId: mockNotesData[0].author._id }
5959
}
6060

61-
const getCooperationUrl = `${URLs.cooperations.getById.replace(':id', cooperationId)}`
6261
const getNotesUrl = URLs.notes.get.replace(':id', cooperationId)
6362
const createNoteUrl = URLs.notes.create.replace(':id', cooperationId)
64-
const updateNoteUrl = `${getCooperationUrl}/notes/${mockNotesData[0]._id}`
65-
const deleteNoteUrl = `${getCooperationUrl}/notes/${mockUpdatedNotesData[0]._id}`
63+
const updateNoteUrl = URLs.notes.update
64+
.replace(':id', cooperationId)
65+
.replace(':noteId', mockNotesData[0]._id)
66+
const deleteNoteUrl = URLs.notes.delete
67+
.replace(':id', cooperationId)
68+
.replace(':noteId', mockUpdatedNotesData[0]._id)
6669
const duplicateNoteUrl = URLs.notes.create.replace(':id', cooperationId)
6770

6871
const url = getFullUrl({

0 commit comments

Comments
 (0)