Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ddecrulle committed Dec 12, 2023
1 parent 4fc5784 commit c5c3991
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 254 deletions.
53 changes: 26 additions & 27 deletions drama-queen/src/core/adapters/localSyncStorage/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import type {
} from "core/ports/LocalSyncStorage";
import { localStorageObjectSchema } from "./parser/localSyncObjectSchema";

//const LOCALSTORAGE_KEY = "QUEEN_SYNC_RESULT";

export function createLocalSyncStorage(params: {
localStorageKey: string;
}): LocalSyncStorage {
Expand All @@ -26,7 +24,7 @@ export function createLocalSyncStorage(params: {
if (serializedData === null) {
return null;
}
return localStorageObjectSchema.parse(serializedData);
return localStorageObjectSchema.parse(JSON.parse(serializedData));
} catch (error) {
console.error("Error retrieving data from localStorage:", error);
return null;
Expand All @@ -38,40 +36,41 @@ export function createLocalSyncStorage(params: {
getObject: getDataFromLocalStorage,
addIdToSurveyUnitsSuccess: (id) => {
const existingData = getDataFromLocalStorage();

if (existingData) {
existingData.surveyUnitsSuccess.push(id);
saveDataToLocalStorage(existingData);
if (existingData === null) {
return saveDataToLocalStorage({
error: false,
surveyUnitsInTempZone: [],
surveyUnitsSuccess: [id],
});
}
saveDataToLocalStorage({
error: false,
surveyUnitsInTempZone: [],
surveyUnitsSuccess: [id],
});
existingData.surveyUnitsSuccess.push(id);
saveDataToLocalStorage(existingData);
},
addIdToSurveyUnitsInTempZone: (id) => {
const existingData = getDataFromLocalStorage();

if (existingData) {
existingData.surveyUnitsInTempZone.push(id);
saveDataToLocalStorage(existingData);
if (existingData === null) {
return saveDataToLocalStorage({
error: false,
surveyUnitsInTempZone: [id],
surveyUnitsSuccess: [],
});
}
saveDataToLocalStorage({
error: false,
surveyUnitsInTempZone: [id],
surveyUnitsSuccess: [],
});

existingData.surveyUnitsInTempZone.push(id);
saveDataToLocalStorage(existingData);
},
addError: (error) => {
const existingData = getDataFromLocalStorage();
if (existingData) {
saveDataToLocalStorage({ ...existingData, error: error });

if (existingData === null) {
return saveDataToLocalStorage({
error: error,
surveyUnitsInTempZone: [],
surveyUnitsSuccess: [],
});
}
saveDataToLocalStorage({
error: error,
surveyUnitsInTempZone: [],
surveyUnitsSuccess: [],
});
saveDataToLocalStorage({ ...existingData, error: error });
},
} satisfies LocalSyncStorage;
}
50 changes: 29 additions & 21 deletions drama-queen/src/core/usecases/synchronizeData/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const thunks = {
download:
() =>
async (...args) => {
const [dispatch, getState, { queenApi, dataStore }] = args;
const [dispatch, getState, { queenApi, dataStore, localSyncStorage }] =
args;

{
const state = getState()[name];
Expand Down Expand Up @@ -38,8 +39,6 @@ export const thunks = {
* SurveyUnit
*/

const surveyUnitSuccess: string[] = [];

const prSurveyUnit = campaignsIds.map((campaignId) =>
queenApi
.getSurveyUnitsIdsAndQuestionnaireIdsByCampaign(campaignId)
Expand All @@ -55,7 +54,7 @@ export const thunks = {
.getSurveyUnit(id)
.then((surveyUnit) => dataStore.updateSurveyUnit(surveyUnit))
.then(() => {
surveyUnitSuccess.push(id);
localSyncStorage.addIdToSurveyUnitsSuccess(id);
dispatch(actions.downloadSurveyUnitCompleted());
})
)
Expand All @@ -65,7 +64,6 @@ export const thunks = {

await Promise.all(prSurveyUnit);

//TODO -> Save surveyUnitSuccess
/*
* Survey
*/
Expand All @@ -79,7 +77,6 @@ export const thunks = {
return questionnaire;
})
.catch(() => {
//TODO Handle error
console.error(
` Questionnaire : An error occurred and we were unable to retrieve survey ${questionnaireId}`
);
Expand Down Expand Up @@ -111,7 +108,6 @@ export const thunks = {
queenApi
.getNomenclature(nomenclatureId)
.catch(() => {
//TODO Handle Errors
console.error(
`Nomenclature : An error occurred and we were unable to retrieve nomenclature ${nomenclatureId}`
);
Expand All @@ -125,7 +121,8 @@ export const thunks = {
upload:
() =>
async (...args) => {
const [dispatch, getState, { dataStore, queenApi }] = args;
const [dispatch, getState, { dataStore, queenApi, localSyncStorage }] =
args;

{
const state = getState()[name];
Expand All @@ -137,10 +134,16 @@ export const thunks = {

dispatch(actions.runningUpload());

// If localStorageData exists, we refresh it; otherwise, we initialize it.
localSyncStorage.saveObject({
error: false,
surveyUnitsInTempZone: [],
surveyUnitsSuccess: [],
});

try {
const prSurveyUnits = dataStore.getAllSurveyUnits();
const surveyUnits = await prSurveyUnits;
const surveyUnitsInTemp: string[] = [];

if (surveyUnits) {
dispatch(actions.setUploadTotal({ total: surveyUnits.length ?? 0 }));
Expand All @@ -155,31 +158,36 @@ export const thunks = {
) {
return queenApi
.postSurveyUnitInTemp(surveyUnit)
.then(() => surveyUnitsInTemp.push(surveyUnit.id));
} else {
throw error;
.then(() =>
localSyncStorage.addIdToSurveyUnitsInTempZone(
surveyUnit.id
)
)
.catch((postError: Error) => {
console.error(
"Error: Unable to post surveyUnit in tempZone",
postError
);
throw postError;
});
}
throw error;
})
.then((result) => {
return dataStore.deleteSurveyUnit(surveyUnit.id);
})
.then(() => dataStore.deleteSurveyUnit(surveyUnit.id))
.then(() => {
dispatch(actions.uploadSurveyUnitCompleted());
})
.catch((error) => {
// TODO: Handle the error as needed -> Save LocalStorage
console.error(error);
dispatch(actions.uploadError());
console.error("Error: Unable to upload data", error);
throw error;
})
);
await Promise.all(surveyUnitPromises);
}

dispatch(actions.uploadCompleted());
dispatch(thunks.download());
} catch (error) {
// TODO : Handle errors from prSurveyUnits
console.error(error);
localSyncStorage.addError(true);
dispatch(actions.uploadError());
}
},
Expand Down
5 changes: 3 additions & 2 deletions drama-queen/src/hooks/useTranslate.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const texts = {
"sync": "Synchronisation en cours",
sync: "Synchronisation en cours",
"sync.download": "Téléchargement des données...",
"sync.download.surveyUnits": "Unités enquêtées",
"sync.download.nomenclatures": "Nomenclatures",
"sync.download.questionnaires": "Questionnaires",
"sync.upload": "Envoi des données",
vizu: "Page de visualisation de questionnaire",
} as const;

const getTranslation = (s: keyof typeof texts) => texts[s] ?? s;

export function useTranslate() {
return {
__: getTranslation,
t: getTranslation,
};
}
13 changes: 2 additions & 11 deletions drama-queen/src/ui/pages/queenMapping/SurveyUnitMapping.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { db } from 'core/indexedDb'
import { Navigate, useParams } from 'react-router-dom'
import { useLiveQuery } from "dexie-react-hooks";
import { useParams } from 'react-router-dom'
import { READ_ONLY } from "ui/constants";

type Params = {
Expand All @@ -11,13 +9,6 @@ type Params = {
export function SurveyUnitMapping() {
const { readonly, id } = useParams<Params>();

const surveyUnit = useLiveQuery(
() => db.surveyUnit.get({ id: id }), [id]
)


if (!surveyUnit) return <div>In Progress</div>


return <div>Survey Unit Mapping id : {id}, readonly : {JSON.stringify(surveyUnit)}</div>
return <div>Survey Unit Mapping id : {id}</div>
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export function VisualisationMapping() {

useEffect(() => {
isQueenV2Survey(questionnaireUrl).then((r) => {
console.log("response", r);
setIsQueenV2(r);
setIsSurveyFetched(true);
}).catch((e) => {
Expand Down
102 changes: 0 additions & 102 deletions drama-queen/src/ui/pages/synchronize-old/SynchronizePage.tsx

This file was deleted.

1 change: 0 additions & 1 deletion drama-queen/src/ui/pages/synchronize-old/index.ts

This file was deleted.

Loading

0 comments on commit c5c3991

Please sign in to comment.