From e1d7d335d69327decc4ddc189a5c238ccfa0c2d1 Mon Sep 17 00:00:00 2001 From: AssahBismarkabah Date: Wed, 13 Mar 2024 12:15:26 +0100 Subject: [PATCH] fix: added fakeimpl --- .../src/hooks/StorageContext.tsx | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/power-pay-frontend/src/hooks/StorageContext.tsx b/power-pay-frontend/src/hooks/StorageContext.tsx index be6d889a..85e04fa8 100644 --- a/power-pay-frontend/src/hooks/StorageContext.tsx +++ b/power-pay-frontend/src/hooks/StorageContext.tsx @@ -8,8 +8,38 @@ interface StorageService { clear: () => Promise; } +// Create the StorageContextData interface. +interface StorageContextData { + getItem: (key: string) => Promise; + setItem: (key: string, value: T) => Promise; + removeItem: (key: string) => Promise; + clear: () => Promise; +} + + +// Creating a fake implementation of the StorageContextData interface. +const fakeStorage: StorageContextData = { + 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 => { + localStorage.setItem(key, JSON.stringify(value)); + return true; + }, + removeItem: async (key: string): Promise => { + localStorage.removeItem(key); + return true; + }, + clear: async (): Promise => { + localStorage.clear(); + return true; + }, +}; + + // Create the StorageContext with default functions for getItem and setItem. -const StorageContext = createContext | undefined>(undefined); +const StorageContext = createContext | undefined>(fakeStorage); // StorageProvider: A React component that wraps the application and provides the storage context with getItem and setItem methods. export function StorageProvider | undefined>({ initialValue = {} as T, children }: PropsWithChildren<{ initialValue?: T }>) { @@ -30,37 +60,9 @@ export function StorageProvider | 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 => { - 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 => { - localStorage.setItem(key, JSON.stringify(value)); - setStoredValue({ ...storedValue, [key]: value }); - return true; - }; - - const removeItem = async (key: string): Promise => { - localStorage.removeItem(key); - setStoredValue((prevData) => { - const newData = { ...prevData }; - delete newData[key]; - return newData; - }); - return true; - }; - - const clear = async (): Promise => { - localStorage.clear(); - setStoredValue({}); - return true; - }; // Destructure getItem and setItem before using them in the StorageContext.Provider value prop. - const contextValue: StorageService = { getItem, setItem, removeItem, clear }; + const contextValue: StorageService = { getItem: fakeStorage.getItem, setItem: fakeStorage.setItem, removeItem: fakeStorage.removeItem, clear: fakeStorage.clear }; return ( }>