From c8a63cae595cae0cc050de9f509ca1d5a7500ca9 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Sat, 11 Dec 2021 23:48:26 +0300 Subject: [PATCH] feat: detaching students from table --- .../StudentTableList/StudentTableList.tsx | 15 ++++++++++++--- src/services/CourseService.ts | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/pages/courses/edit/components/StudentTableList/StudentTableList.tsx b/src/pages/courses/edit/components/StudentTableList/StudentTableList.tsx index 5ca7191..5c60fd4 100644 --- a/src/pages/courses/edit/components/StudentTableList/StudentTableList.tsx +++ b/src/pages/courses/edit/components/StudentTableList/StudentTableList.tsx @@ -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; @@ -107,8 +107,17 @@ const StudentTableList: React.FC = 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); + } }; }; diff --git a/src/services/CourseService.ts b/src/services/CourseService.ts index 2919e74..eeaf1bf 100644 --- a/src/services/CourseService.ts +++ b/src/services/CourseService.ts @@ -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): Promise>> { 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): Promise>> { + 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);