From 63beba1e0e8fcbad7f25b3a0468e06767a9a76b5 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Sun, 17 Oct 2021 23:33:22 +0300 Subject: [PATCH] refactor: resume course status if error in request --- .../components/CourseList/CourseItem.tsx | 62 +++++++++---------- src/stores/CourseStore.ts | 29 ++++++--- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/src/pages/courses/components/CourseList/CourseItem.tsx b/src/pages/courses/components/CourseList/CourseItem.tsx index db9abfb..5acc994 100644 --- a/src/pages/courses/components/CourseList/CourseItem.tsx +++ b/src/pages/courses/components/CourseList/CourseItem.tsx @@ -51,39 +51,35 @@ const CourseItem: React.FC = ({ course, onDelete, onSetStat {(me.isAdmin || me.isTeacher) && ( - - {({ isOpen, onClose }) => ( - <> - } - _hover={{ backgroundColor: colorMode === 'dark' ? 'gray.600' : 'gray.200' }} - _active={{ backgroundColor: colorMode === 'dark' ? 'gray.600' : 'gray.200' }} - /> - - }>Edit course - {course.status === CourseStatusEnum.DRAFT && ( - onSetStatus(course.id, CourseStatusEnum.PUBLISHED)} icon={}> - Publish - - )} - {course.status === CourseStatusEnum.PUBLISHED && ( - onSetStatus(course.id, CourseStatusEnum.DRAFT)} icon={}> - Mark as Draft - - )} - onDelete({ id: course.id, title: course.title })} - icon={} - color='red.500' - > - Delete - - - - )} + + } + _hover={{ backgroundColor: colorMode === 'dark' ? 'gray.600' : 'gray.200' }} + _active={{ backgroundColor: colorMode === 'dark' ? 'gray.600' : 'gray.200' }} + /> + + }>Edit course + {course.status === CourseStatusEnum.DRAFT && ( + onSetStatus(course.id, CourseStatusEnum.PUBLISHED)} icon={}> + Publish + + )} + {course.status === CourseStatusEnum.PUBLISHED && ( + onSetStatus(course.id, CourseStatusEnum.DRAFT)} icon={}> + Mark as Draft + + )} + onDelete({ id: course.id, title: course.title })} + icon={} + color='red.500' + > + Delete + + diff --git a/src/stores/CourseStore.ts b/src/stores/CourseStore.ts index e0b5295..bd0ca02 100644 --- a/src/stores/CourseStore.ts +++ b/src/stores/CourseStore.ts @@ -77,24 +77,37 @@ export default class CourseStore { } public async setCourseStatus(id: string, status: CourseStatusEnum) { - try { - const result = await this.courseService.setStatus(id, status); + const course = this.courses?.find(course => course.id === id); + /** + * Save previous course status before sending request + */ + const { status: prevCourseStatus } = course ?? {}; + + try { /** * Update status in store */ runInAction(() => { - if (this.courses !== null) { - const courseIndex = this.courses.findIndex(course => course.id === id); - - if (courseIndex !== -1) { - this.courses[courseIndex].status = status; - } + if (this.courses !== null && course !== undefined) { + course.status = status; } }); + /** + * Make request on update course status + */ + const result = await this.courseService.setStatus(id, status); return result; } catch (error: any) { + /** + * Resume status if there was an error on request + */ + runInAction(() => { + if (this.courses !== null && course !== undefined && prevCourseStatus !== undefined) { + course.status = prevCourseStatus; + } + }); throw error; } }