mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 10:19:22 +03:00
54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
/**
|
|
* Hooks
|
|
*/
|
|
import { CourseStatusEnum } from '@educt/enums';
|
|
import { useErrorHandler } from 'react-error-boundary';
|
|
import useAsync from '../../useAsync';
|
|
import { useRootStore } from '../../useRootStore';
|
|
import { useToast } from '@chakra-ui/toast';
|
|
|
|
type SetCourseStatusResultDataType = {} | undefined;
|
|
export type SetCourseStatusFnType = (
|
|
id: string,
|
|
newStatus: CourseStatusEnum
|
|
) => Promise<SetCourseStatusResultDataType | undefined>;
|
|
|
|
const useSetCourseStatus = () => {
|
|
const { courseStore } = useRootStore();
|
|
const toast = useToast();
|
|
const handleError = useErrorHandler();
|
|
|
|
const setCourseStatus = async (id: string, newStatus: CourseStatusEnum) => {
|
|
try {
|
|
const result = await courseStore.setCourseStatus(id, newStatus);
|
|
switch (newStatus) {
|
|
case CourseStatusEnum.PUBLISHED:
|
|
toast({ title: 'Course has been published.', status: 'success' });
|
|
break;
|
|
case CourseStatusEnum.DRAFT:
|
|
toast({ title: 'Course has been marked as draft.', status: 'info' });
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return result.data;
|
|
} catch (error: any) {
|
|
if (error.response) {
|
|
toast({ title: error.message, status: 'error' });
|
|
} else {
|
|
handleError(error);
|
|
}
|
|
|
|
return Promise.reject(error);
|
|
}
|
|
};
|
|
|
|
const { execute, ...state } = useAsync<SetCourseStatusResultDataType, Parameters<typeof setCourseStatus>>(
|
|
setCourseStatus
|
|
);
|
|
|
|
return { setCourseStatus: execute, ...state };
|
|
};
|
|
|
|
export { useSetCourseStatus };
|