Skip to content

Commit

Permalink
fix: added fakeimpl
Browse files Browse the repository at this point in the history
  • Loading branch information
AssahBismarkabah committed Mar 13, 2024
1 parent 4ab0b57 commit e1d7d33
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions power-pay-frontend/src/hooks/StorageContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,38 @@ interface StorageService<T> {
clear: () => Promise<boolean>;
}

// Create the StorageContextData interface.
interface StorageContextData<T> {
getItem: (key: string) => Promise<unknown | undefined>;
setItem: (key: string, value: T) => Promise<boolean>;
removeItem: (key: string) => Promise<boolean>;
clear: () => Promise<boolean>;
}


// Creating a fake implementation of the StorageContextData interface.
const fakeStorage: StorageContextData<unknown> = {
getItem: async (key: string): Promise< unknown | undefined> => {
const item = localStorage.getItem(key);
return item ? JSON.parse(item): undefined;
},
setItem: async (key: string, value: unknown): Promise<boolean> => {
localStorage.setItem(key, JSON.stringify(value));
return true;
},
removeItem: async (key: string): Promise<boolean> => {
localStorage.removeItem(key);
return true;
},
clear: async (): Promise<boolean> => {
localStorage.clear();
return true;
},
};


// Create the StorageContext with default functions for getItem and setItem.
const StorageContext = createContext<StorageService<unknown> | undefined>(undefined);
const StorageContext = createContext<StorageService<unknown> | undefined>(fakeStorage);

// StorageProvider: A React component that wraps the application and provides the storage context with getItem and setItem methods.
export function StorageProvider<T extends Record<string, T> | undefined>({ initialValue = {} as T, children }: PropsWithChildren<{ initialValue?: T }>) {
Expand All @@ -30,37 +60,9 @@ export function StorageProvider<T extends Record<string, T> | undefined>({ initi
}
}, [storedValue]);

// Define the getItem method that retrieves the value from the local storage or returns undefined if it doesn't exist.
const getItem = async (key: string): Promise<T | undefined> => {
const item = localStorage.getItem(key);
return item ? JSON.parse(item) : undefined;
}

// Define the setItem method that updates the local storage and the state with the new value.
const setItem = async (key: string, value: T): Promise<boolean> => {
localStorage.setItem(key, JSON.stringify(value));
setStoredValue({ ...storedValue, [key]: value });
return true;
};

const removeItem = async (key: string): Promise<boolean> => {
localStorage.removeItem(key);
setStoredValue((prevData) => {
const newData = { ...prevData };
delete newData[key];
return newData;
});
return true;
};

const clear = async (): Promise<boolean> => {
localStorage.clear();
setStoredValue({});
return true;
};

// Destructure getItem and setItem before using them in the StorageContext.Provider value prop.
const contextValue: StorageService<T> = { getItem, setItem, removeItem, clear };
const contextValue: StorageService<T> = { getItem: fakeStorage.getItem, setItem: fakeStorage.setItem, removeItem: fakeStorage.removeItem, clear: fakeStorage.clear };

return (
<StorageContext.Provider value={contextValue as StorageService<unknown>}>
Expand Down

0 comments on commit e1d7d33

Please sign in to comment.