From ba69ccb5fa4e979c3da3dfe884a26f41cb7673fc Mon Sep 17 00:00:00 2001 From: Romain Le Cellier Date: Tue, 21 Nov 2023 14:08:22 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=9A(frontend)=20api/joanie=20as=20joan?= =?UTF-8?q?ieLegacyClient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/js/api/joanie/index.ts | 10 ++++++++++ .../joanieLegacyClient.spec.ts} | 2 +- .../js/api/{joanie.ts => joanie/joanieLegacyClient.ts} | 0 src/frontend/js/api/lms/joanie.ts | 2 +- src/frontend/js/contexts/JoanieApiContext/index.tsx | 4 ++-- src/frontend/js/contexts/SessionContext/index.tsx | 4 ++-- .../js/hooks/useCourseProductUnion/index.spec.tsx | 6 +++--- .../react-query/useSessionMutation/index.spec.tsx | 4 ++-- .../utils/react-query/useSessionQuery/index.spec.tsx | 10 +++++++--- .../js/widgets/SyllabusCourseRunsList/index.tsx | 4 ++-- src/frontend/mocks/handlers/contracts.ts | 4 ++-- 11 files changed, 32 insertions(+), 18 deletions(-) rename src/frontend/js/api/{joanie.spec.ts => joanie/joanieLegacyClient.spec.ts} (93%) rename src/frontend/js/api/{joanie.ts => joanie/joanieLegacyClient.ts} (100%) diff --git a/src/frontend/js/api/joanie/index.ts b/src/frontend/js/api/joanie/index.ts index 32fff2c654..27b0f5f352 100644 --- a/src/frontend/js/api/joanie/index.ts +++ b/src/frontend/js/api/joanie/index.ts @@ -31,3 +31,13 @@ export const joanieApi = new ApiClientJoanie(config); export const isApiError = (error: unknown): error is ApiError => { return (error as ApiError).name === 'ApiError'; }; + +export { + default as JoanieLegacyClient, + getResponseBody as getResponseBodyLegacy, + checkStatus as checkStatusLegacy, + getAPIEndpoint as getAPIEndpointLegacy, + getRoutes as getRoutesLegacy, + isJoanieEnabled as isJoanieEnabledLegacy, + buildApiUrl as buildApiUrlLegacy, +} from './joanieLegacyClient'; diff --git a/src/frontend/js/api/joanie.spec.ts b/src/frontend/js/api/joanie/joanieLegacyClient.spec.ts similarity index 93% rename from src/frontend/js/api/joanie.spec.ts rename to src/frontend/js/api/joanie/joanieLegacyClient.spec.ts index c8b2e7ae1a..b94937de96 100644 --- a/src/frontend/js/api/joanie.spec.ts +++ b/src/frontend/js/api/joanie/joanieLegacyClient.spec.ts @@ -1,7 +1,7 @@ import fetchMock from 'fetch-mock'; import { ResourcesQuery } from 'hooks/useResources'; import { HttpStatusCode } from 'utils/errors/HttpError'; -import { buildApiUrl, getResponseBody } from './joanie'; +import { buildApiUrl, getResponseBody } from './joanieLegacyClient'; describe('api/joanie', () => { it('getResponse should handle empty response body', async () => { diff --git a/src/frontend/js/api/joanie.ts b/src/frontend/js/api/joanie/joanieLegacyClient.ts similarity index 100% rename from src/frontend/js/api/joanie.ts rename to src/frontend/js/api/joanie/joanieLegacyClient.ts diff --git a/src/frontend/js/api/lms/joanie.ts b/src/frontend/js/api/lms/joanie.ts index 047f814bba..d50863bb67 100644 --- a/src/frontend/js/api/lms/joanie.ts +++ b/src/frontend/js/api/lms/joanie.ts @@ -1,5 +1,5 @@ import { matchPath, PathMatch } from 'react-router-dom'; -import JoanieApi from 'api/joanie'; +import { JoanieLegacyClient as JoanieApi } from 'api/joanie'; import { LMSBackend } from 'types/commonDataProps'; import { APIBackend, APILms } from 'types/api'; diff --git a/src/frontend/js/contexts/JoanieApiContext/index.tsx b/src/frontend/js/contexts/JoanieApiContext/index.tsx index 11e62cba32..4d2bf88b7f 100644 --- a/src/frontend/js/contexts/JoanieApiContext/index.tsx +++ b/src/frontend/js/contexts/JoanieApiContext/index.tsx @@ -1,7 +1,7 @@ import type { PropsWithChildren } from 'react'; import { createContext, useContext } from 'react'; import type * as Joanie from 'types/Joanie'; -import API from 'api/joanie'; +import { JoanieLegacyClient as JoanieApi } from 'api/joanie'; import type { Maybe } from 'types/utils'; const JoanieApiContext = createContext>(undefined); @@ -10,7 +10,7 @@ const JoanieApiContext = createContext>(undefined); * Provider to access to the Joanie API interface. */ const JoanieApiProvider = ({ children }: PropsWithChildren<{}>) => { - const api = API(); + const api = JoanieApi(); return {children}; }; diff --git a/src/frontend/js/contexts/SessionContext/index.tsx b/src/frontend/js/contexts/SessionContext/index.tsx index 078d8e66e1..92757b61a7 100644 --- a/src/frontend/js/contexts/SessionContext/index.tsx +++ b/src/frontend/js/contexts/SessionContext/index.tsx @@ -1,5 +1,5 @@ import { lazy, PropsWithChildren, Suspense, useContext } from 'react'; -import { isJoanieEnabled } from 'api/joanie'; +import { isJoanieEnabledLegacy } from 'api/joanie'; import { AuthenticationApi } from 'api/authentication'; import { handle } from 'utils/errors/handle'; import { Session } from './SessionContext'; @@ -19,7 +19,7 @@ export const SessionProvider = ({ children, ...props }: PropsWithChildren) return ( - {isJoanieEnabled ? ( + {isJoanieEnabledLegacy ? ( {children} ) : ( {children} diff --git a/src/frontend/js/hooks/useCourseProductUnion/index.spec.tsx b/src/frontend/js/hooks/useCourseProductUnion/index.spec.tsx index f4ccf21ade..2568cbac68 100644 --- a/src/frontend/js/hooks/useCourseProductUnion/index.spec.tsx +++ b/src/frontend/js/hooks/useCourseProductUnion/index.spec.tsx @@ -8,7 +8,7 @@ import { History, HistoryContext } from 'hooks/useHistory'; import { RichieContextFactory as mockRichieContextFactory } from 'utils/test/factories/richie'; import { createTestQueryClient } from 'utils/test/createTestQueryClient'; import { SessionProvider } from 'contexts/SessionContext'; -import { getRoutes } from 'api/joanie'; +import { getRoutesLegacy } from 'api/joanie'; import { mockPaginatedResponse } from 'utils/test/mockPaginatedResponse'; import { CourseListItemFactory, CourseProductRelationFactory } from 'utils/test/factories/joanie'; import { useCourseProductUnion } from '.'; @@ -74,7 +74,7 @@ describe('useCourseProductUnion', () => { }); it('should call courses and coursesProductRelation endpoints', async () => { - const ROUTES = getRoutes(); + const ROUTES = getRoutesLegacy(); const coursesUrl = ROUTES.courses.get.replace(':id/', ''); const courseProductRelationsUrl = ROUTES.courseProductRelations.get.replace(':id/', ''); fetchMock.get( @@ -100,7 +100,7 @@ describe('useCourseProductUnion', () => { it('should call organization courses and organization coursesProductRelation endpoints', async () => { const organizationId = 'DUMMY_ORGANIZATION_ID'; - const ROUTES = getRoutes(); + const ROUTES = getRoutesLegacy(); const organizationCoursesUrl = ROUTES.user.organizations.courses.get.replace( ':id', organizationId, diff --git a/src/frontend/js/utils/react-query/useSessionMutation/index.spec.tsx b/src/frontend/js/utils/react-query/useSessionMutation/index.spec.tsx index ecd9d195ca..75c663dfba 100644 --- a/src/frontend/js/utils/react-query/useSessionMutation/index.spec.tsx +++ b/src/frontend/js/utils/react-query/useSessionMutation/index.spec.tsx @@ -5,7 +5,7 @@ import { renderHook, waitFor } from '@testing-library/react'; import { RichieContextFactory as mockRichieContextFactory } from 'utils/test/factories/richie'; import BaseSessionProvider from 'contexts/SessionContext/BaseSessionProvider'; import { useSession } from 'contexts/SessionContext'; -import { checkStatus } from 'api/joanie'; +import { checkStatusLegacy } from 'api/joanie'; import { createTestQueryClient } from 'utils/test/createTestQueryClient'; import { HttpStatusCode } from 'utils/errors/HttpError'; import { useSessionMutation } from '.'; @@ -41,7 +41,7 @@ describe('useSessionMutation', () => { const session = useSession(); const mutation = useSessionMutation({ mutationFn: () => - fetch('http://api.endpoint/orders/create', { method: 'POST' }).then(checkStatus), + fetch('http://api.endpoint/orders/create', { method: 'POST' }).then(checkStatusLegacy), onError: handleError, }); diff --git a/src/frontend/js/utils/react-query/useSessionQuery/index.spec.tsx b/src/frontend/js/utils/react-query/useSessionQuery/index.spec.tsx index 042c456579..8be4a75e67 100644 --- a/src/frontend/js/utils/react-query/useSessionQuery/index.spec.tsx +++ b/src/frontend/js/utils/react-query/useSessionQuery/index.spec.tsx @@ -4,7 +4,7 @@ import { PropsWithChildren } from 'react'; import { renderHook, waitFor } from '@testing-library/react'; import { RichieContextFactory as mockRichieContextFactory } from 'utils/test/factories/richie'; import BaseSessionProvider from 'contexts/SessionContext/BaseSessionProvider'; -import { checkStatus } from 'api/joanie'; +import { checkStatusLegacy } from 'api/joanie'; import { useSession } from 'contexts/SessionContext'; import { createTestQueryClient } from 'utils/test/createTestQueryClient'; import { HttpError, HttpStatusCode } from 'utils/errors/HttpError'; @@ -43,7 +43,9 @@ describe('useSessionQuery', () => { const useHooks = () => { const session = useSession(); - useSessionQuery(['orders'], () => fetch('http://api.endpoint/orders/').then(checkStatus)); + useSessionQuery(['orders'], () => + fetch('http://api.endpoint/orders/').then(checkStatusLegacy), + ); return session; }; @@ -73,7 +75,9 @@ describe('useSessionQuery', () => { const useHooks = () => { const session = useSession(); - useSessionQuery(['orders'], () => fetch('http://api.endpoint/orders/').then(checkStatus)); + useSessionQuery(['orders'], () => + fetch('http://api.endpoint/orders/').then(checkStatusLegacy), + ); return session; }; diff --git a/src/frontend/js/widgets/SyllabusCourseRunsList/index.tsx b/src/frontend/js/widgets/SyllabusCourseRunsList/index.tsx index 6ad3994bae..5a0db52f69 100644 --- a/src/frontend/js/widgets/SyllabusCourseRunsList/index.tsx +++ b/src/frontend/js/widgets/SyllabusCourseRunsList/index.tsx @@ -7,7 +7,7 @@ import { computeStates } from 'utils/CourseRuns'; import { SyllabusAsideList } from 'widgets/SyllabusCourseRunsList/components/SyllabusAsideList'; import { SyllabusCourseRun } from 'widgets/SyllabusCourseRunsList/components/SyllabusCourseRun'; import { DjangoCMSPluginsInit } from 'components/DjangoCMSTemplate'; -import { isJoanieEnabled } from 'api/joanie'; +import { isJoanieEnabledLegacy } from 'api/joanie'; import context from 'utils/context'; import CourseWishButton from './components/CourseWishButton'; @@ -74,7 +74,7 @@ const SyllabusCourseRunsList = ({
- {isJoanieEnabled && Boolean(context?.features.WISHLIST) && ( + {isJoanieEnabledLegacy && Boolean(context?.features.WISHLIST) && ( )}
diff --git a/src/frontend/mocks/handlers/contracts.ts b/src/frontend/mocks/handlers/contracts.ts index 0eeb51786c..04c73e48de 100644 --- a/src/frontend/mocks/handlers/contracts.ts +++ b/src/frontend/mocks/handlers/contracts.ts @@ -1,11 +1,11 @@ import { http, HttpResponse } from 'msw'; -import { getAPIEndpoint } from 'api/joanie'; +import { getAPIEndpointLegacy } from 'api/joanie'; import { Contract, PaginatedResponse } from 'types/Joanie'; import { ContractFactory } from 'utils/test/factories/joanie'; import { PER_PAGE } from 'settings'; export default [ - http.get(`${getAPIEndpoint()}/contracts/`, () => { + http.get(`${getAPIEndpointLegacy()}/contracts/`, () => { return HttpResponse.json>({ count: 250, results: ContractFactory().many(PER_PAGE.teacherContractList),