refactor: resume course status if error in request

This commit is contained in:
Sergey Yarkov 2021-10-17 23:33:22 +03:00
parent d74b539395
commit 63beba1e0e
2 changed files with 50 additions and 41 deletions

View File

@ -51,9 +51,7 @@ const CourseItem: React.FC<CourseItemPropsType> = ({ course, onDelete, onSetStat
{(me.isAdmin || me.isTeacher) && (
<Flex justifyContent='flex-end'>
<Box position='absolute' zIndex='1' padding='10px'>
<Menu>
{({ isOpen, onClose }) => (
<>
<Menu isLazy>
<MenuButton
as={IconButton}
backgroundColor={colorMode === 'dark' ? 'gray.700' : 'gray.100'}
@ -82,8 +80,6 @@ const CourseItem: React.FC<CourseItemPropsType> = ({ course, onDelete, onSetStat
Delete
</MenuItem>
</MenuList>
</>
)}
</Menu>
</Box>
</Flex>

View File

@ -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;
}
}