Skip to content

Commit

Permalink
break point
Browse files Browse the repository at this point in the history
  • Loading branch information
ddecrulle committed Nov 27, 2023
1 parent e7bf905 commit db80981
Show file tree
Hide file tree
Showing 31 changed files with 190 additions and 570 deletions.
2 changes: 1 addition & 1 deletion drama-queen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@tanstack/react-query": "^4.29.19",
"@tanstack/react-query-devtools": "^4.33.0",
"@types/memoizee": "^0.4.8",
"axios": "^1.4.0",
"axios": "^1.6.2",
"dexie": "^3.2.4",
"dexie-react-hooks": "^1.1.6",
"evt": "^2.5.2",
Expand Down
31 changes: 31 additions & 0 deletions drama-queen/src/core/adapters/localSyncStorage/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type {
LocalStorageObject,
LocalSyncStorage,
} from "core/ports/LocalSyncStorage";
import { object } from "zod";

//const LOCALSTORAGE_KEY = "QUEEN_SYNC_RESULT";

export function createLocalSyncStorage(params: {
localStorageKey: string;
}): LocalSyncStorage {
const { localStorageKey } = params;

const saveDataToLocalStorage = (data: LocalStorageObject): void => {
try {
const serializedData = JSON.stringify(data);
localStorage.setItem(localStorageKey, serializedData);
} catch (error) {
console.error("Error saving data to localStorage:", error);
}
};

return {
saveObject: (object) => undefined,
getObject: () => ({} as LocalStorageObject),
addIdToSurveyUnitsSuccess: (id) => undefined,
addIdToSurveyUnitsInTempZone: (id) => undefined,
addError: (error) => undefined,

};
}
139 changes: 59 additions & 80 deletions drama-queen/src/core/adapters/queenApi/default.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import axios, {
type AxiosError,
type AxiosRequestConfig,
type AxiosResponse,
} from "axios";
import axios, { AxiosError } from "axios";
import memoize from "memoizee";
import type { QueenApi } from "core/ports/QueenApi";
import {
Expand Down Expand Up @@ -30,12 +26,14 @@ export function createApiClient(params: {
const { axiosInstance } = (() => {
const axiosInstance = axios.create({ baseURL: apiUrl, timeout: 120_000 });

const onRequest = (config: AxiosRequestConfig) => {
//console.info(`[request] [${JSON.stringify(config)}]`);
// Type issue https://github.com/axios/axios/issues/5494
const onRequest = (config: any) => {
return {
...(config as any),
...config,
headers: {
...config.headers,
"Content-Type": "application/json;charset=utf-8",
Accept: "application/json;charset=utf-8",
...(() => {
const accessToken = getAccessToken();

Expand All @@ -48,28 +46,11 @@ export function createApiClient(params: {
};
})(),
},
"Content-Type": "application/json;charset=utf-8",
Accept: "application/json;charset=utf-8",
};
};

const onRequestError = (error: AxiosError): Promise<AxiosError> => {
console.error(`[request error] [${JSON.stringify(error)}]`);
return Promise.reject(error);
};

const onResponse = (response: AxiosResponse): AxiosResponse => {
//console.info(`[response] [${JSON.stringify(response)}]`);
return response;
};

const onResponseError = (error: AxiosError) => {
console.error(`[response error] [${JSON.stringify(error)}]`);
return Promise.reject(error);
};
axiosInstance.interceptors.request.use(onRequest);

axiosInstance.interceptors.request.use(onRequest, onRequestError);
axiosInstance.interceptors.response.use(onResponse, onResponseError);
return { axiosInstance };
})();

Expand All @@ -81,69 +62,67 @@ export function createApiClient(params: {
.then(({ data }) => idAndQuestionnaireIdSchema.array().parse(data)),
{ promise: true }
),
getSurveyUnits: memoize(
() =>
axiosInstance
.get<SurveyUnit[]>(`/api/survey-units/interviewer`)
.then(({ data }) =>
data.map((surveyUnit) => surveyUnitSchema.parse(surveyUnit))
),
{ promise: true }
),
getSurveyUnit: memoize(
(idSurveyUnit) =>
axiosInstance
.get<Omit<SurveyUnit, "id">>(`/api/survey-unit/${idSurveyUnit}`)
.then(({ data }) =>
surveyUnitSchema.parse({ id: idSurveyUnit, ...data })
),
{ promise: true }
),
getSurveyUnits: () =>
axiosInstance
.get<SurveyUnit[]>(`/api/survey-units/interviewer`)
.then(({ data }) =>
data.map((surveyUnit) => surveyUnitSchema.parse(surveyUnit))
),

getSurveyUnit: (idSurveyUnit) =>
axiosInstance
.get<Omit<SurveyUnit, "id">>(`/api/survey-unit/${idSurveyUnit}`)
.then(({ data }) =>
surveyUnitSchema.parse({ id: idSurveyUnit, ...data })
),

putSurveyUnit: (surveyUnit) =>
axiosInstance
.put(`api/survey-unit/${surveyUnit.id}`, surveyUnit)
.put<typeof surveyUnit>(`api/survey-unit/${surveyUnit.id}`, surveyUnit)
.then(() => undefined),
// .catch((error: Error | AxiosError) => {
// if (
// axios.isAxiosError(error) &&
// error.response &&
// [400, 403, 404, 500].includes(error.response.status)
// ) {
// return
// } else {
// throw error;
// }
// })
putSurveyUnitsData: (surveyUnitsData) =>
axiosInstance
.put(`/api/survey-units/data`, surveyUnitsData)
.then(() => undefined),
postSurveyUnitInTemp: (idSurveyUnit, surveyUnit) =>

postSurveyUnitInTemp: (surveyUnit) =>
axiosInstance
.post(
`api/survey-unit/${idSurveyUnit}/temp-zone`,
surveyUnit
)
.post(`api/survey-unit/${surveyUnit.id}/temp-zone`, surveyUnit)
.then(() => undefined),
getCampaigns: memoize(
() =>
axiosInstance
.get<Campaign>(`api/campaigns`)
.then(({ data }) => campaignSchema.array().parse(data)),
{ promise: true }
),
getQuestionnaire: memoize(
(idSurvey) =>
axiosInstance
.get<{ value: Questionnaire }>(`/api/questionnaire/${idSurvey}`)
.then(({ data }) => data.value),
{ promise: true }
),
getRequiredNomenclaturesByCampaign: memoize(
(idNomenclature) =>
axiosInstance
.get<RequiredNomenclatures>(
`/api/questionnaire/${idNomenclature}/required-nomenclatures`
)
.then(({ data }) => requiredNomenclaturesSchema.parse(data)),
{ promise: true }
),
getNomenclature: memoize(
(idNomenclature) =>
axiosInstance
.get<Nomenclature>(`/api/nomenclature/${idNomenclature}`)
.then(({ data }) => nomenclatureSchema.parse(data)),
{ promise: true }
),

getCampaigns: () =>
axiosInstance
.get<Campaign>(`api/campaigns`)
.then(({ data }) => campaignSchema.array().parse(data)),

getQuestionnaire: (idSurvey) =>
axiosInstance
.get<{ value: Questionnaire }>(`/api/questionnaire/${idSurvey}`)
.then(({ data }) => data.value),

getRequiredNomenclaturesByCampaign: (idNomenclature) =>
axiosInstance
.get<RequiredNomenclatures>(
`/api/questionnaire/${idNomenclature}/required-nomenclatures`
)
.then(({ data }) => requiredNomenclaturesSchema.parse(data)),

getNomenclature: (idNomenclature) =>
axiosInstance
.get<Nomenclature>(`/api/nomenclature/${idNomenclature}`)
.then(({ data }) => nomenclatureSchema.parse(data)),

postParadata: (paradata) =>
axiosInstance
.post<typeof paradata>(`/api/paradata`, paradata)
Expand Down
21 changes: 12 additions & 9 deletions drama-queen/src/core/adapters/queenApi/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ export function createApiClient(): QueenApi {
]),
getSurveyUnit: (idSurveyUnit) =>
Promise.resolve(createSUMocked({ idSu: idSurveyUnit })),
putSurveyUnit: (idSurveyUnit, surveyUnit) =>
console.log("putSurveyUnit", `id: ${idSurveyUnit}`, surveyUnit),
putSurveyUnitsData: (surveyUnitsData) => {
console.log("putSurveyUnits");
console.table(surveyUnitsData);
},
postSurveyUnitInTemp: (idSurveyUnit, surveyUnit) =>
console.log("postSurveyUnitInTemp", `id: ${idSurveyUnit}`, surveyUnit),
putSurveyUnit: (surveyUnit) =>
Promise.resolve(
console.log("putSurveyUnit", `id: ${surveyUnit.id}`, surveyUnit)
),
putSurveyUnitsData: (surveyUnitsData) =>
Promise.resolve(console.table(surveyUnitsData)),
postSurveyUnitInTemp: (surveyUnit) =>
Promise.resolve(
console.log("postSurveyUnitInTemp", `id: ${surveyUnit.id}`, surveyUnit)
),
getCampaigns: () =>
Promise.resolve([
{
Expand All @@ -31,7 +33,8 @@ export function createApiClient(): QueenApi {
getRequiredNomenclaturesByCampaign: () => Promise.resolve([]),
getNomenclature: (idNomenclature) =>
Promise.resolve([{ id: `${idNomenclature}`, label: "label" }]),
postParadata: (paradata) => console.log("postParadata", paradata),
postParadata: (paradata) =>
Promise.resolve(console.log("postParadata", paradata)),
};
}

Expand Down
13 changes: 0 additions & 13 deletions drama-queen/src/core/indexedDb/index.ts

This file was deleted.

11 changes: 0 additions & 11 deletions drama-queen/src/core/indexedDb/tables/paradata.ts

This file was deleted.

13 changes: 0 additions & 13 deletions drama-queen/src/core/indexedDb/tables/surveyUnit.ts

This file was deleted.

Empty file.
13 changes: 13 additions & 0 deletions drama-queen/src/core/ports/LocalSyncStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type LocalStorageObject = {
error: boolean;
surveyUnitsSuccess: string[];
surveyUnitsInTempZone: string[];
};

export type LocalSyncStorage = {
saveObject: (object: LocalStorageObject) => void;
getObject: () => LocalStorageObject;
addIdToSurveyUnitsSuccess: (id: string) => void;
addIdToSurveyUnitsInTempZone: (id: string) => void;
addError: (error: boolean) => void;
};
1 change: 0 additions & 1 deletion drama-queen/src/core/ports/QueenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export type QueenApi = {
surveyUnitsData: Omit<SurveyUnit, "questionnaireId">[]
) => Promise<void>;
postSurveyUnitInTemp: (
idSurveyUnit: string,
surveyUnit: SurveyUnit
) => Promise<void>;
getCampaigns: () => Promise<Campaign[]>;
Expand Down
31 changes: 0 additions & 31 deletions drama-queen/src/core/tools/EventEmitter.ts

This file was deleted.

19 changes: 0 additions & 19 deletions drama-queen/src/core/tools/jwt.ts

This file was deleted.

17 changes: 0 additions & 17 deletions drama-queen/src/core/tools/listenActivity.ts

This file was deleted.

Loading

0 comments on commit db80981

Please sign in to comment.