From d2f1330c698468480666c61a22220117f44b2cfc Mon Sep 17 00:00:00 2001 From: Yaroslav Chuiko <32570823+YaroslavChuiko@users.noreply.github.com> Date: Thu, 17 Oct 2024 08:50:57 +0300 Subject: [PATCH] Use couseCooperation service in the ChangeResourceConfirmModal (#2614) * created new service * fixed tests --- src/constants/request.ts | 3 + .../ChangeResourceConfirmModal.tsx | 70 +++++++++---------- src/services/course-cooperation-service.ts | 14 ++++ .../common/interfaces/common.interfaces.ts | 9 ++- .../ChangeResourceConfirmModal.spec.jsx | 4 +- 5 files changed, 60 insertions(+), 40 deletions(-) create mode 100644 src/services/course-cooperation-service.ts diff --git a/src/constants/request.ts b/src/constants/request.ts index 83cea5bfb..720d33d9d 100644 --- a/src/constants/request.ts +++ b/src/constants/request.ts @@ -37,6 +37,9 @@ export const URLs = { create: '/courses', patch: '/courses' }, + coursesAndCooperations: { + getByResourceId: '/courses-cooperations/resource/' + }, categories: { get: '/categories', getNames: '/categories/names', diff --git a/src/containers/change-resource-confirm-modal/ChangeResourceConfirmModal.tsx b/src/containers/change-resource-confirm-modal/ChangeResourceConfirmModal.tsx index 76111cc75..98c2a8dfb 100644 --- a/src/containers/change-resource-confirm-modal/ChangeResourceConfirmModal.tsx +++ b/src/containers/change-resource-confirm-modal/ChangeResourceConfirmModal.tsx @@ -5,19 +5,12 @@ import { useTranslation } from 'react-i18next' import ErrorOutlineIcon from '@mui/icons-material/ErrorOutline' import AppButton from '~/components/app-button/AppButton' - -import { - ButtonVariantEnum, - Course, - GetCoursesParams, - ItemsWithCount, - SizeEnum -} from '~/types' import { styles } from '~/containers/change-resource-confirm-modal/ChangeResourceConfirmModal.styles' import { useModalContext } from '~/context/modal-context' import Loader from '~/components/loader/Loader' import useAxios from '~/hooks/use-axios' -import { CourseService } from '~/services/course-service' +import { CoursesAndCooperationsService } from '~/services/course-cooperation-service' +import { ButtonVariantEnum, CourseCooperationResponse, SizeEnum } from '~/types' interface ChangeResourceConfirmModalProps { resourceId?: string @@ -26,40 +19,43 @@ interface ChangeResourceConfirmModalProps { } const ChangeResourceConfirmModal = ({ - resourceId, + resourceId = '', title, onConfirm }: ChangeResourceConfirmModalProps) => { const { t } = useTranslation() const { closeModal } = useModalContext() - //! replace when new endpoint is ready - const { response: coursesResponse, loading } = useAxios< - ItemsWithCount, - GetCoursesParams - >({ - service: CourseService.getCourses, - defaultResponse: { items: [], count: 0 }, + const getCoursesAndCooperationsByResourceId = useCallback( + () => CoursesAndCooperationsService.getByResourceId(resourceId), + [resourceId] + ) + + const { response, loading } = useAxios({ + service: getCoursesAndCooperationsByResourceId, + defaultResponse: { + courses: [], + cooperations: [] + } as CourseCooperationResponse, fetchOnMount: true }) - const courseList = useMemo( - () => - coursesResponse.items - .filter((item) => item.sections[0].resources?.length) - .filter((item) => - item.sections.some((res) => - res.resources.some((val) => val.resource._id == resourceId) - ) - ) - .map((item) => ({ - id: item._id, - title: item.title, - subTitle: 'course' - })), - [coursesResponse.items, resourceId] + const courses = response.courses.map((course) => ({ + id: course._id, + title: course.title, + subTitle: 'course' + })) + + const cooperations = response?.cooperations.map((cooperation) => ({ + id: cooperation._id, + title: cooperation.title, + subTitle: 'cooperation' + })) + + const affectedItems = useMemo( + () => [...courses, ...cooperations], + [courses, cooperations] ) - ////////////////////////////////////! const handleConfirm = useCallback(() => { closeModal() @@ -67,10 +63,10 @@ const ChangeResourceConfirmModal = ({ }, [closeModal, onConfirm]) useEffect(() => { - if (!loading && !courseList?.length) { + if (!loading && !affectedItems.length) { handleConfirm() } - }, [courseList, handleConfirm, loading]) + }, [affectedItems, handleConfirm, loading]) if (loading) { return ( @@ -80,7 +76,7 @@ const ChangeResourceConfirmModal = ({ ) } - if (!loading && !courseList?.length) { + if (!loading && !affectedItems?.length) { return null } @@ -107,7 +103,7 @@ const ChangeResourceConfirmModal = ({ - {courseList.map((el) => ( + {affectedItems.map((el) => ( {el.title} diff --git a/src/services/course-cooperation-service.ts b/src/services/course-cooperation-service.ts new file mode 100644 index 000000000..e3c437e1c --- /dev/null +++ b/src/services/course-cooperation-service.ts @@ -0,0 +1,14 @@ +import { AxiosResponse } from 'axios' +import { URLs } from '~/constants/request' +import { axiosClient } from '~/plugins/axiosClient' +import { CourseCooperationResponse } from '~/types' +import { createUrlPath } from '~/utils/helper-functions' + +export const CoursesAndCooperationsService = { + getByResourceId: async ( + resourceId: string + ): Promise> => + await axiosClient.get( + createUrlPath(URLs.coursesAndCooperations.getByResourceId, resourceId) + ) +} diff --git a/src/types/common/interfaces/common.interfaces.ts b/src/types/common/interfaces/common.interfaces.ts index 4e703172c..447812d18 100644 --- a/src/types/common/interfaces/common.interfaces.ts +++ b/src/types/common/interfaces/common.interfaces.ts @@ -3,7 +3,9 @@ import { UpdateFiltersInQuery, UserResponse, UserGeneralInfo, - UserRoleEnum + UserRoleEnum, + Course, + Cooperation } from '~/types' export interface ItemsWithCount { @@ -26,6 +28,11 @@ export interface DataByRole { [UserRoleEnum.Tutor]: T } +export type CourseCooperationResponse = { + courses: Course[] + cooperations: Cooperation[] +} + export interface CategoryInterface { _id: string name: string diff --git a/tests/unit/containers/change-resource-confirm-modal/ChangeResourceConfirmModal.spec.jsx b/tests/unit/containers/change-resource-confirm-modal/ChangeResourceConfirmModal.spec.jsx index 918f755f5..4b253b07d 100644 --- a/tests/unit/containers/change-resource-confirm-modal/ChangeResourceConfirmModal.spec.jsx +++ b/tests/unit/containers/change-resource-confirm-modal/ChangeResourceConfirmModal.spec.jsx @@ -25,7 +25,7 @@ const props = { } const fakeData = { - items: [ + courses: [ { _id: 'testId1', title: 'Course 1', @@ -75,7 +75,7 @@ const fakeData = { ] } ], - count: 2 + cooperations: [] } describe('ChangeResourceConfirmModal component tests', () => {