Skip to content

Commit

Permalink
refactor: typescript /api
Browse files Browse the repository at this point in the history
  • Loading branch information
chloe-renaud committed Jan 14, 2025
1 parent e552c7c commit 48749be
Show file tree
Hide file tree
Showing 20 changed files with 275 additions and 223 deletions.
3 changes: 2 additions & 1 deletion src/actions/app-state.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getContextFromCampaign, putQuestionnaire } from '../api/remote-api';
import { putQuestionnaire } from '../api/questionnaires';
import { getContextFromCampaign } from '../api/search';
import { getVersion } from '../api/versions';
import { getVisualization } from '../api/visualize';
import { TCM } from '../constants/pogues-constants';
Expand Down
8 changes: 3 additions & 5 deletions src/actions/metadata.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {
getCampaigns,
getNomenclature,
getNomenclatures,
getOperations,
getQuestionnaire,
getSeries,
getUnitsList,
getVariablesById,
} from '../api/remote-api';
} from '../api/metadata';
import { getNomenclature, getNomenclatures } from '../api/nomenclatures';
import { getQuestionnaire, getVariablesById } from '../api/questionnaires';
import { DIMENSION_LENGTH } from '../constants/pogues-constants';

const { NON_DYNAMIC } = DIMENSION_LENGTH;
Expand Down
2 changes: 1 addition & 1 deletion src/actions/questionnaire-list.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getQuestionnaireList } from '../api/remote-api';
import { getQuestionnaireList } from '../api/questionnaires';
import { questionnaireListRemoteToStores } from '../model/remote-to-stores';

export const LOAD_QLIST = 'LOAD_QLIST';
Expand Down
2 changes: 1 addition & 1 deletion src/actions/questionnaire.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
deleteQuestionnaire,
getQuestionnaire,
postQuestionnaire,
} from '../api/remote-api';
} from '../api/questionnaires';
import { COMPONENT_TYPE } from '../constants/pogues-constants';
import { Component } from '../model';
import { questionnaireRemoteToStores } from '../model/remote-to-stores';
Expand Down
8 changes: 4 additions & 4 deletions src/actions/search.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSearchResults } from '../api/remote-api';
import { getSearchResults } from '../api/search';

export const LOAD_SEARCH_RESULT = 'LOAD_SEARCH_RESULT';
export const LOAD_SEARCH_RESULT_SUCCESS = 'LOAD_SEARCH_RESULT_SUCCESS';
Expand Down Expand Up @@ -47,17 +47,17 @@ export const loadSearchResultFailure = (err) => ({
* Load search results list
* @param {string} token The user token.
* @param {string} typeItem The type of item to search.
* @param {object} criterias The list of criterias.
* @param {object} criteria The list of criteria.
* @param {string} filter The text to filter.
* @return {object} LOAD_SEARCH_RESULT action
*/
export const loadSearchResult =
(token, typeItem, criterias, filter) => (dispatch) => {
(token, typeItem, criteria, filter) => (dispatch) => {
dispatch({
type: LOAD_SEARCH_RESULT,
payload: null,
});
return getSearchResults(token, typeItem, criterias, filter)
return getSearchResults(token, typeItem, criteria, filter)
.then((resultsList) => dispatch(loadSearchResultSuccess(resultsList)))
.catch((err) => dispatch(loadSearchResultFailure(err)));
};
13 changes: 13 additions & 0 deletions src/api/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getBaseURI } from './utils';

type Init = {
isSearchDisable: boolean;
};

export async function getInit(): Promise<Init> {
const url = `${getBaseURI()}/init`;
const headers = new Headers();
headers.append('Accept', 'application/json');

return fetch(url, { headers }).then((res) => res.json() as Promise<Init>);
}
43 changes: 43 additions & 0 deletions src/api/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { computeAuthorizationHeader, getBaseURI } from './utils';

export async function getSeries(token: string): Promise<unknown> {
const url = `${getBaseURI()}/search/series`;
const headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Authorization', computeAuthorizationHeader(token));

return fetch(url, { headers }).then((res) => res.json());
}

export async function getOperations(
id: string,
token: string,
): Promise<unknown> {
const url = `${getBaseURI()}/search/series/${id}/operations`;
const headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Authorization', computeAuthorizationHeader(token));

return fetch(url, { headers }).then((res) => res.json());
}

export async function getCampaigns(
id: string,
token: string,
): Promise<unknown> {
const url = `${getBaseURI()}/search/operations/${id}/collections`;
const headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Authorization', computeAuthorizationHeader(token));

return fetch(url, { headers }).then((res) => res.json());
}

export async function getUnitsList(token: string): Promise<unknown> {
const url = `${getBaseURI()}/meta-data/units`;
const headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Authorization', computeAuthorizationHeader(token));

return fetch(url, { headers }).then((res) => res.json());
}
15 changes: 15 additions & 0 deletions src/api/nomenclatures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import getNomenclaturesContent from '../utils/codes-lists/__mocks__/get-nomenclatures.json';

type NomenclaturesJSON = {
nomenclatures: { [key: string]: unknown };
};

const nomenclaturesJSON: NomenclaturesJSON = getNomenclaturesContent;

export function getNomenclatures(): NomenclaturesJSON {
return nomenclaturesJSON;
}

export function getNomenclature(id: string): unknown {
return nomenclaturesJSON.nomenclatures[id];
}
117 changes: 117 additions & 0 deletions src/api/questionnaires.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { computeAuthorizationHeader, getBaseURI } from './utils';

const pathQuestionnaire = 'persistence/questionnaire';
const pathQuestionnaireList = 'persistence/questionnaires';

/**
* Retrieve questionnaires associated to the provided stamp (e.g. "DR59-SNDI59")
*/
export async function getQuestionnaireList(
stamp: string,
token: string,
): Promise<unknown[]> {
const url = `${getBaseURI()}/${pathQuestionnaireList}/search/meta?owner=${stamp}`;
const headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Authorization', computeAuthorizationHeader(token));

return fetch(url, {
headers,
}).then((res) => res.json() as Promise<unknown[]>);
}

export async function getStampsList(token: string): Promise<unknown> {
const url = `${getBaseURI()}/${pathQuestionnaireList}/stamps`;
const headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Authorization', computeAuthorizationHeader(token));

return fetch(url, {
headers,
}).then((res) => res.json());
}

/**
* Create new questionnaire
*/
export async function postQuestionnaire(qr: unknown, token: string) {
const url = `${getBaseURI()}/${pathQuestionnaireList}`;
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', computeAuthorizationHeader(token));

return fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(qr),
}).then((res) => {
if (res.ok) return res;
throw new Error(`Network request failed :${res.statusText}`);
});
}

/**
* Update questionnaire by id
*/
export async function putQuestionnaire(id: string, qr: unknown, token: string) {
const url = `${getBaseURI()}/${pathQuestionnaire}/${id}`;
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', computeAuthorizationHeader(token));

return fetch(url, {
method: 'PUT',
headers,
body: JSON.stringify(qr),
}).then((res) => {
if (res.ok) return res;
throw new Error(`Network request failed :${res.statusText}`);
});
}

/**
* Retrieve questionnaire by id
*/
export async function getQuestionnaire(
id: string,
token: string,
): Promise<unknown> {
const url = `${getBaseURI()}/${pathQuestionnaire}/${id}`;
const headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Authorization', computeAuthorizationHeader(token));

return fetch(url, {
headers,
}).then((res) => res.json());
}

/**
* Will send a DELETE request in order to remove an existing questionnaire
*
* @param {deleteQuestionnaire} id The id of the questionnaire we want to delete
*/
export async function deleteQuestionnaire(id: string, token: string) {
const url = `${getBaseURI()}/${pathQuestionnaire}/${id}`;
const headers = new Headers();
headers.append('Authorization', computeAuthorizationHeader(token));

return fetch(url, {
method: 'DELETE',
headers,
});
}

export async function getVariablesById(
id: string,
token: string,
): Promise<unknown> {
const url = `${getBaseURI()}/${pathQuestionnaire}/${id}/variables`;
const headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Authorization', computeAuthorizationHeader(token));

return fetch(url, {
headers,
}).then((res) => res.json());
}
Loading

0 comments on commit 48749be

Please sign in to comment.