Skip to content

Commit

Permalink
🚚(frontend) api/joanie as joanieLegacyClient
Browse files Browse the repository at this point in the history
  • Loading branch information
rlecellier committed Nov 22, 2023
1 parent 9956438 commit ba69ccb
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 18 deletions.
10 changes: 10 additions & 0 deletions src/frontend/js/api/joanie/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/frontend/js/api/lms/joanie.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
4 changes: 2 additions & 2 deletions src/frontend/js/contexts/JoanieApiContext/index.tsx
Original file line number Diff line number Diff line change
@@ -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<Maybe<Joanie.API>>(undefined);
Expand All @@ -10,7 +10,7 @@ const JoanieApiContext = createContext<Maybe<Joanie.API>>(undefined);
* Provider to access to the Joanie API interface.
*/
const JoanieApiProvider = ({ children }: PropsWithChildren<{}>) => {
const api = API();
const api = JoanieApi();

return <JoanieApiContext.Provider value={api}>{children}</JoanieApiContext.Provider>;
};
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/js/contexts/SessionContext/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -19,7 +19,7 @@ export const SessionProvider = ({ children, ...props }: PropsWithChildren<any>)

return (
<Suspense fallback="loading...">
{isJoanieEnabled ? (
{isJoanieEnabledLegacy ? (
<LazyJoanieSessionProvider {...props}>{children}</LazyJoanieSessionProvider>
) : (
<LazyBaseSessionProvider {...props}>{children}</LazyBaseSessionProvider>
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/js/hooks/useCourseProductUnion/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 '.';
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 '.';
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('useSessionMutation', () => {
const session = useSession();
const mutation = useSessionMutation<unknown, void, unknown>({
mutationFn: () =>
fetch('http://api.endpoint/orders/create', { method: 'POST' }).then(checkStatus),
fetch('http://api.endpoint/orders/create', { method: 'POST' }).then(checkStatusLegacy),
onError: handleError,
});

Expand Down
10 changes: 7 additions & 3 deletions src/frontend/js/utils/react-query/useSessionQuery/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/js/widgets/SyllabusCourseRunsList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -74,7 +74,7 @@ const SyllabusCourseRunsList = ({
<div className="course-detail__row course-detail__runs course-detail__runs--open">
<div className="course-detail__empty">
<FormattedMessage {...messages.noOpenedCourseRuns} />
{isJoanieEnabled && Boolean(context?.features.WISHLIST) && (
{isJoanieEnabledLegacy && Boolean(context?.features.WISHLIST) && (
<CourseWishButton course={course} />
)}
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/mocks/handlers/contracts.ts
Original file line number Diff line number Diff line change
@@ -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<PaginatedResponse<Contract>>({
count: 250,
results: ContractFactory().many(PER_PAGE.teacherContractList),
Expand Down

0 comments on commit ba69ccb

Please sign in to comment.