diff --git a/power-pay-frontend/src/hooks/StorageContext.tsx b/power-pay-frontend/src/hooks/StorageContext.tsx index 0e874155..22ff5391 100644 --- a/power-pay-frontend/src/hooks/StorageContext.tsx +++ b/power-pay-frontend/src/hooks/StorageContext.tsx @@ -1,20 +1,6 @@ import { createContext, PropsWithChildren, useEffect } from 'react'; import { useStorage } from './useStorage'; -interface StorageService { - getItem: (key: string) => Promise; - setItem: (key: string, value: T) => Promise; - removeItem: (key: string) => Promise; - 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. @@ -38,51 +24,71 @@ const fakeStorage: StorageContextData = { }; +// Create the StorageContextData interface. +interface StorageContextData { + getItem: (key: string) => Promise; + setItem: (key: string, value: T) => Promise; + removeItem: (key: string) => Promise; + clear: () => Promise; +} + +interface StorageService { + getItem: (key: string) => Promise; + setItem: (key: string, value: T) => Promise; + removeItem: (key: string) => Promise; + clear: () => Promise; +} + + // Create the StorageContext with default functions for getItem and setItem. -const StorageContext = createContext | undefined>(fakeStorage); +const StorageContext = createContext | undefined>(undefined); // StorageProvider: A React component that wraps the application and provides the storage context with getItem and setItem methods. export function StorageProvider({ initialValue, children }: PropsWithChildren<{ initialValue?: T }>) { - const [storedValue, setStoredValue] = useStorage({ initialValue: { ...initialValue } }); + const [storedValue, setStoredValue] = useStorage({ key: 'your_key_here', initialValue }); -// Define a global constant for the key. + // Define a global constant for the key. const STORAGE_KEY = 'key'; // Initializes the state with the value from the local storage if it exists. useEffect(() => { - const item = localStorage.getItem('STORAGE_KEY'); + const item = localStorage.getItem(STORAGE_KEY); if (item) { setStoredValue(JSON.parse(item)); } - }, []); + }, [setStoredValue]); // Updates the local storage whenever the state changes. useEffect(() => { - if (storedValue) { - setItemWrapper(STORAGE_KEY, storedValue); + if (storedValue !== undefined) { + localStorage.setItem(STORAGE_KEY, JSON.stringify(storedValue)); } }, [storedValue]); - // Defining a wrapper function for the setItem method that updates the local storage and the state with the new value. - const setItemWrapper = async (key: string, value: T | undefined) => { - fakeStorage.setItem(key, value); + // Defining a wrapper function for the setItem method that updates the local storage and the state with the new value. + const setItemWrapper = async (key: string, value: T | undefined) => { + if (value !== undefined) { + localStorage.setItem(key, JSON.stringify(value)); + } else { + localStorage.removeItem(key); + } setStoredValue(value); return true; }; -// Destructure getItem and setItem before using them in the StorageContext.Provider value prop. -const contextValue: StorageService = { - getItem: fakeStorage.getItem, - setItem: setItemWrapper, - removeItem: fakeStorage.removeItem, - clear: fakeStorage.clear, -}; -return ( - }> - {children} - -); -} + // Destructure getItem and setItem before using them in the StorageContext.Provider value prop. + const contextValue: StorageService = { + getItem: (key) => fakeStorage.getItem(key), + setItem: (key, value) => setItemWrapper(key, value), + removeItem: (key) => fakeStorage.removeItem(key), + clear: () => fakeStorage.clear(), + }; + return ( + }> + {children} + + ); +} -export default StorageContext; \ No newline at end of file +export default StorageContext;