Skip to content

Commit

Permalink
🚚(frontend) api/joanie as joanieLegacyClient
Browse files Browse the repository at this point in the history
We'll implement a api client generated from joanie's openApi schema.
The current client needs to change place.
  • Loading branch information
rlecellier committed Mar 6, 2024
1 parent a55cc68 commit b009ed9
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 19 deletions.
15 changes: 15 additions & 0 deletions src/frontend/js/api/joanie/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import context from 'utils/context';

/**
* Flag which determines if joanie is enabled.
*/
export const isJoanieEnabled = !!context.joanie_backend;

export {
default as JoanieLegacyClient,
getResponseBody as getResponseBodyLegacy,
checkStatus as checkStatusLegacy,
getAPIEndpoint as getAPIEndpointLegacy,
getRoutes as getRoutesLegacy,
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
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,6 @@ export const getRoutes = () => {
};
};

/**
* Flag which determines if joanie is enabled.
*/
export const isJoanieEnabled = !!context.joanie_backend;

const filterEmptyEntry = ([, value]: [PropertyKey, any]) => {
if (value == null) return false; // Value is null/undefined
if (value?.length !== undefined && value.length === 0) return false; // Value is an empty array/string
Expand Down
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
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.organizations.courses.get
.replace(':organization_id', organizationId)
.replace(':id/', '');
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/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 b009ed9

Please sign in to comment.