Skip to content

Commit

Permalink
fix: updated useStorage.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
AssahBismarkabah committed Mar 20, 2024
1 parent e933383 commit cc5f16f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
8 changes: 4 additions & 4 deletions power-pay-frontend/src/hooks/StorageContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class LocalStorageService implements StorageService {
}

// Define a global constant for the key.
const STORAGE_KEY = 'key';
const STORAGE_KEY = 'new_key';

// Create the StorageContext with default functions for getItem and setItem.
const StorageContext = createContext<StorageContextData<unknown> | undefined>(undefined);
Expand All @@ -53,7 +53,7 @@ export function StorageProvider<T>({ children, storageService }: PropsWithChildr
// Initialize the state with the value from the local storage if it exists.
useEffect(() => {
storageService
.getItem<Record<string, T>>(STORAGE_KEY)
.getItem<Record<string, T>>(STORAGE_KEY)
.then((item) => {
if (item) {
setStoredValue(item);
Expand All @@ -63,7 +63,7 @@ export function StorageProvider<T>({ children, storageService }: PropsWithChildr

// Updates the local storage whenever the state changes.
useEffect(() => {
storageService.setItem(STORAGE_KEY, storedValue);
storageService.setItem(STORAGE_KEY, storedValue);
}, [storageService, storedValue]);

// Remove the item from local storage and set the stored value to undefined
Expand All @@ -89,7 +89,7 @@ export function StorageProvider<T>({ children, storageService }: PropsWithChildr
},
removeItem: async (key) => clearItem(key),
clear: async () => {
// localStorage.clear();
localStorage.clear();
setStoredValue({});
return true;
},
Expand Down
6 changes: 2 additions & 4 deletions power-pay-frontend/src/hooks/useStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ export function useStorage<T = string>({ key, initialValue }: UseStorageProps<T>

// Update the context when the storedValue changes
useEffect(() => {
if (storedValue !== undefined) {
setItem(key, storedValue);
}
setItem(key, storedValue as T);
}, [key, storedValue, setItem]);

// Clear the item from local storage and reset the stored value
Expand All @@ -30,4 +28,4 @@ export function useStorage<T = string>({ key, initialValue }: UseStorageProps<T>
};

return [storedValue, setStoredValue, clearItem];
}
}

0 comments on commit cc5f16f

Please sign in to comment.