Skip to content

Commit

Permalink
Some updates
Browse files Browse the repository at this point in the history
  • Loading branch information
NazariiAlieksieiev committed Oct 31, 2024
1 parent f8d13b0 commit 6b1ad0f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
12 changes: 0 additions & 12 deletions src/Store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { ErrorMessage } from './types/ErrorMessage';
import { Todo } from './types/Todo';
import { Status } from './types/Status';
export type Action =
| { type: 'download' }
| { type: 'downloadSuccess'; todos: Todo[] }
| { type: 'failure'; errorMessage: ErrorMessage | null }
| { type: 'closeNotification' }
| { type: 'filterStatus'; status: Status }
Expand Down Expand Up @@ -32,16 +30,6 @@ const initialState: RootState = {

const reducer = (state: RootState, action: Action): RootState => {
switch (action.type) {
case 'download':
return {
...state,
errorMessage: null,
};
case 'downloadSuccess':
return {
...state,
todos: action.todos,
};
case 'failure':
return {
...state,
Expand Down
27 changes: 27 additions & 0 deletions src/hook/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useState } from 'react';

export function useLocalStorage<T>(
key: string,
startValue: T,
): [T, (v: T) => void] {
const [value, setValue] = useState(() => {
const data = localStorage.getItem(key);

if (data === null) {
return startValue;
}

try {
return JSON.parse(data);
} catch {
return startValue;
}
});

const save = (newValue: T) => {
localStorage.setItem(key, JSON.stringify(newValue));
setValue(newValue);
};

return [value, save];
}

0 comments on commit 6b1ad0f

Please sign in to comment.