From 78263656ca13110ebec39745069cde2d46931f04 Mon Sep 17 00:00:00 2001 From: Olia Slisarchuk Date: Fri, 17 Nov 2023 15:13:19 +0200 Subject: [PATCH] added previous values on edit lesson (#1415) --- src/components/file-editor/FileEditor.tsx | 3 -- .../CreateOrEditLesson.constants.tsx | 1 + .../CreateOrEditLesson.tsx | 30 ++++++++----------- src/services/resource-service.ts | 3 +- .../lesson/interfaces/lesson.interfaces.ts | 7 ++--- 5 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/components/file-editor/FileEditor.tsx b/src/components/file-editor/FileEditor.tsx index 34e707958..2a1e5b111 100644 --- a/src/components/file-editor/FileEditor.tsx +++ b/src/components/file-editor/FileEditor.tsx @@ -1,8 +1,6 @@ import { FC } from 'react' import { Editor } from '@tinymce/tinymce-react' -import { initialFileValue } from '~/components/file-editor/FileEditor.constants' - interface FileEditorProps { onEdit: (content: string) => void value: string @@ -23,7 +21,6 @@ const FileEditor: FC = ({ onEdit, value }) => { content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:14px }' }} - initialValue={initialFileValue} onEditorChange={onEdit} value={value} /> diff --git a/src/pages/create-or-edit-lesson/CreateOrEditLesson.constants.tsx b/src/pages/create-or-edit-lesson/CreateOrEditLesson.constants.tsx index aa3d75bb3..ac598fdab 100644 --- a/src/pages/create-or-edit-lesson/CreateOrEditLesson.constants.tsx +++ b/src/pages/create-or-edit-lesson/CreateOrEditLesson.constants.tsx @@ -10,6 +10,7 @@ export const validations = { export const initialValues = { title: '', description: '', + content: '', attachments: [] } diff --git a/src/pages/create-or-edit-lesson/CreateOrEditLesson.tsx b/src/pages/create-or-edit-lesson/CreateOrEditLesson.tsx index 00a04f4b5..8a09b7ca7 100644 --- a/src/pages/create-or-edit-lesson/CreateOrEditLesson.tsx +++ b/src/pages/create-or-edit-lesson/CreateOrEditLesson.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect } from 'react' +import { useEffect } from 'react' import { useTranslation } from 'react-i18next' import { Link, useNavigate, useParams } from 'react-router-dom' import { AxiosResponse } from 'axios' @@ -49,8 +49,6 @@ const CreateOrEditLesson = () => { const { openModal } = useModalContext() const navigate = useNavigate() - const [attachments, setAttachments] = useState([]) - const [content, setContent] = useState('') const { id } = useParams() const handleResponseError = (error: ErrorResponse) => { @@ -71,39 +69,35 @@ const CreateOrEditLesson = () => { } const handleAddAttachments = (attachments: Attachment[]) => { - setAttachments(attachments) + handleNonInputValueChange('attachments', attachments) } const handleOpenAddAttachmentsModal = () => { openModal({ component: ( ) }) } - const handleRemoveAttachment = (attachment: Attachment) => { - setAttachments((prevAttachments) => - prevAttachments.filter( - (prevAttachment) => prevAttachment._id !== attachment._id + const handleRemoveAttachment = (attachmentToDelete: Attachment) => { + handleNonInputValueChange( + 'attachments', + data.attachments.filter( + (attachment) => attachment._id !== attachmentToDelete._id ) ) } const handleEdit = (content: string) => { - setContent(content) + handleNonInputValueChange('content', content) } const addLesson = (): Promise => { - const lesson = { - ...data, - content, - attachments: attachments.map((attachment) => attachment._id) - } - return ResourceService.addLesson(lesson) + return ResourceService.addLesson(data) } const { fetchData: fetchAddLesson } = useAxios({ @@ -172,7 +166,7 @@ const CreateOrEditLesson = () => { return } - const attachmentsList = attachments.map((attachment) => ( + const attachmentsList = data.attachments.map((attachment) => ( { > {t('lesson.labels.attachments')} - + {attachmentsList} diff --git a/src/services/resource-service.ts b/src/services/resource-service.ts index 809d33f63..1ce5fa99d 100644 --- a/src/services/resource-service.ts +++ b/src/services/resource-service.ts @@ -10,7 +10,6 @@ import { ItemsWithCount, LessonData, Lesson, - NewLesson, UpdateAttachmentParams, Question, Categories, @@ -33,7 +32,7 @@ export const ResourceService = { await axiosClient.get(createUrlPath(URLs.resources.lessons.get, id)), deleteLesson: async (id: string): Promise> => await axiosClient.delete(createUrlPath(URLs.resources.lessons.delete, id)), - addLesson: async (data: NewLesson): Promise => + addLesson: async (data: LessonData): Promise => await axiosClient.post(URLs.resources.lessons.add, data), editLesson: async (data: LessonData, id?: string): Promise => await axiosClient.patch( diff --git a/src/types/lesson/interfaces/lesson.interfaces.ts b/src/types/lesson/interfaces/lesson.interfaces.ts index c3dd0278b..ae00e554e 100644 --- a/src/types/lesson/interfaces/lesson.interfaces.ts +++ b/src/types/lesson/interfaces/lesson.interfaces.ts @@ -1,9 +1,8 @@ +import { Attachment } from '~/types' + export interface LessonData { title: string description: string - attachments: string[] -} - -export interface NewLesson extends LessonData { content: string + attachments: Attachment[] }