Skip to content

Commit caef248

Browse files
ArtemHolikovlizabre
authored andcommitted
fixed comments
1 parent 0473843 commit caef248

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'
@@ -23,8 +23,7 @@ import {
2323
import useSnackbarAlert from '~/hooks/use-snackbar-alert'
2424
import useQuery from '~/hooks/use-query'
2525
import useMutation from '~/hooks/use-mutation'
26-
27-
type NoteAction = 'create' | 'delete' | 'update' | 'duplicate'
26+
import { noteActionMap, NoteAction } from './constant'
2827

2928
const 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,
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)