feat: detaching students from table

This commit is contained in:
Sergey Yarkov 2021-12-11 23:48:26 +03:00
parent 08d2e0ccde
commit c8a63cae59
2 changed files with 27 additions and 3 deletions

View File

@ -18,7 +18,7 @@ import type { ICourse } from '@educt/interfaces';
*/
import { useCallback, useState } from 'react';
import { useDisclosure } from '@chakra-ui/react';
import { UserServiceInstance } from '@educt/services';
import { CourseServiceInstance, UserServiceInstance } from '@educt/services';
type StudentTableListPropsType = {
render: React.FC<StudentTableRowPropsType>;
@ -107,8 +107,17 @@ const StudentTableList: React.FC<StudentTableListPropsType> = props => {
* Remove from course student handler
*/
const handleRemove = (id: string) => {
return () => {
console.log(`removing id: ${id}`);
return async () => {
try {
setIsLoading(true);
await CourseServiceInstance.detachStudentsList(course.id, [id]);
toast({ title: 'Student removed.', status: 'info' });
setRows(prev => prev.filter(s => s.id !== id));
} catch (error) {
console.error(error);
} finally {
setIsLoading(false);
}
};
};

View File

@ -97,6 +97,13 @@ class CourseService {
return result.data;
}
/**
* Attach students to course by id
*
* @param courseId Course id
* @param ids List ids of students
* @returns Empty data
*/
public async attachStudentsList(courseId: string, ids: Array<string>): Promise<IApiRespose<Record<string, never>>> {
const result = await this.api.post(
`/v1/courses/${courseId}/attach-student-list`,
@ -104,6 +111,14 @@ class CourseService {
);
return result.data;
}
public async detachStudentsList(courseId: string, ids: Array<string>): Promise<IApiRespose<Record<string, never>>> {
const result = await this.api.patch(
`/v1/courses/${courseId}/detach-student-list`,
helpers.transformToFormData({ students: ids })
);
return result.data;
}
}
export const CourseServiceInstance = new CourseService(ApiServiceInstance.api);