Skip to content

Commit

Permalink
Merge pull request #16 from michaela9/7-persisted-history
Browse files Browse the repository at this point in the history
create persisted items with custom useLocalStorage hook
  • Loading branch information
tomastauer authored Apr 21, 2024
2 parents 01bef19 + 987c75d commit 466074a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/app/hooks/useItemsStore.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
'use client';

import { useState } from 'react';
import { useLocalStorage } from './useLocalStorage';

export type TodoItem = {
content: string;
isDone: boolean;
id: number;
};

const defaultItems = [
export const defaultItems = [
{ content: 'Feed a dog', id: 1, isDone: true },
{ content: 'Pick up kids from school', id: 2, isDone: false },
{ content: 'Water plants', id: 3, isDone: false },
];

export function useItemsStore() {
const [items, setItems] = useState<TodoItem[]>(defaultItems);
const [items, setItems] = useLocalStorage('items', defaultItems);

const addItem = (content: string) => {
setItems(
Expand Down
28 changes: 28 additions & 0 deletions src/app/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use client';

import { useState } from 'react';

import { TodoItem } from './useItemsStore';

function getInitialValue(key: string, defaultItems: TodoItem[]) {
try {
const item = localStorage.getItem(key);

return item ? JSON.parse(item) : defaultItems;
} catch {
return [];
}
}

export function useLocalStorage(key: string, defaultItems: TodoItem[]) {
const [value, setValue] = useState<TodoItem[]>(
getInitialValue(key, defaultItems),
);

const storeValue = (items: TodoItem[]) => {
setValue(items);
localStorage.setItem(key, JSON.stringify(items));
};

return [value, storeValue] as const;
}
4 changes: 2 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client';

import { Background } from './branding/background';
import { AddItem } from './components/add-item';
import { useItemsStore } from './hooks/useItemsStore';
import { Header } from './components/header';
import { ItemsList } from './components/items-list';
import { Background } from './branding/background';
import { useItemsStore } from './hooks/useItemsStore';

export default function TodoList() {
const { items, addItem, toggleItem, deleteItem } = useItemsStore();
Expand Down
4 changes: 4 additions & 0 deletions test/todo-list.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import userEvent from '@testing-library/user-event';
import TodoList from '../src/app/page';
import { useItemsStore } from '../src/app/hooks/useItemsStore';

afterEach(() => {
localStorage.clear();
});

it('should append new item at the end of the list by clicking the button', async () => {
render(<TodoList />);

Expand Down

0 comments on commit 466074a

Please sign in to comment.