Skip to content

Commit

Permalink
localstorage port
Browse files Browse the repository at this point in the history
  • Loading branch information
ddecrulle committed Nov 28, 2023
1 parent db80981 commit 4fc5784
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 18 deletions.
64 changes: 55 additions & 9 deletions drama-queen/src/core/adapters/localSyncStorage/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
LocalStorageObject,
LocalSyncStorage,
} from "core/ports/LocalSyncStorage";
import { object } from "zod";
import { localStorageObjectSchema } from "./parser/localSyncObjectSchema";

//const LOCALSTORAGE_KEY = "QUEEN_SYNC_RESULT";

Expand All @@ -11,21 +11,67 @@ export function createLocalSyncStorage(params: {
}): LocalSyncStorage {
const { localStorageKey } = params;

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

const getDataFromLocalStorage = () => {
try {
const serializedData = localStorage.getItem(localStorageKey);
if (serializedData === null) {
return null;
}
return localStorageObjectSchema.parse(serializedData);
} catch (error) {
console.error("Error retrieving data from localStorage:", error);
return null;
}
};

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

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

if (existingData) {
existingData.surveyUnitsInTempZone.push(id);
saveDataToLocalStorage(existingData);
}
saveDataToLocalStorage({
error: false,
surveyUnitsInTempZone: [id],
surveyUnitsSuccess: [],
});
},
addError: (error) => {
const existingData = getDataFromLocalStorage();
if (existingData) {
saveDataToLocalStorage({ ...existingData, error: error });
}
saveDataToLocalStorage({
error: error,
surveyUnitsInTempZone: [],
surveyUnitsSuccess: [],
});
},
} satisfies LocalSyncStorage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LocalStorageObject } from "core/ports/LocalSyncStorage";
import { Equals, assert } from "tsafe";
import { z } from "zod";

export const localStorageObjectSchema = z.object({
error: z.boolean(),
surveyUnitsSuccess: z.array(z.string()),
surveyUnitsInTempZone: z.array(z.string()),
});

assert<Equals<z.infer<typeof localStorageObjectSchema>, LocalStorageObject>>();
28 changes: 20 additions & 8 deletions drama-queen/src/core/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ import { createCore, type GenericCore } from "redux-clean-architecture";
import type { Oidc } from "core/ports/Oidc";
import type { DataStore } from "core/ports/DataStore";
import type { QueenApi } from "core/ports/QueenApi";
import type { LocalSyncStorage } from "./ports/LocalSyncStorage";

type ParamsOfBootstrapCore = {
apiUrl: string;
publicUrl: string;
oidcParams:
| {
issuerUri: string;
clientId: string;
}
| undefined;
| {
issuerUri: string;
clientId: string;
}
| undefined;
};

type Context = {
paramsOfBootstrapCore: ParamsOfBootstrapCore;
oidc: Oidc;
queenApi: QueenApi;
dataStore: DataStore;
localSyncStorage: LocalSyncStorage;
};

export type Core = GenericCore<typeof usecases, Context>;
Expand All @@ -28,8 +30,9 @@ export type State = Core["types"]["State"];
export type Thunks = Core["types"]["Thunks"];
export type CreateEvt = Core["types"]["CreateEvt"];

export async function bootstrapCore(params: ParamsOfBootstrapCore): Promise<{ core: Core; }> {

export async function bootstrapCore(
params: ParamsOfBootstrapCore
): Promise<{ core: Core }> {
const { apiUrl, publicUrl, oidcParams } = params;

const oidc = await (async () => {
Expand Down Expand Up @@ -89,16 +92,25 @@ export async function bootstrapCore(params: ParamsOfBootstrapCore): Promise<{ co
});
})();

const localSyncStorage = await (async () => {
const { createLocalSyncStorage } = await import(
"core/adapters/localSyncStorage/default"
);

return createLocalSyncStorage({ localStorageKey: "QUEEN_SYNC_RESULT" });
})();

const context: Context = {
paramsOfBootstrapCore: params,
oidc,
queenApi,
dataStore,
localSyncStorage,
};

const { core } = createCore({
context,
usecases
usecases,
});

return { core };
Expand Down
2 changes: 1 addition & 1 deletion drama-queen/src/core/ports/LocalSyncStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type LocalStorageObject = {

export type LocalSyncStorage = {
saveObject: (object: LocalStorageObject) => void;
getObject: () => LocalStorageObject;
getObject: () => LocalStorageObject | null;
addIdToSurveyUnitsSuccess: (id: string) => void;
addIdToSurveyUnitsInTempZone: (id: string) => void;
addError: (error: boolean) => void;
Expand Down

0 comments on commit 4fc5784

Please sign in to comment.