mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 10:19:22 +03:00
refactor: resume course status if error in request
This commit is contained in:
parent
d74b539395
commit
63beba1e0e
@ -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>
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user