Skip to content

Commit

Permalink
fix: extract todo func logo (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
ooade authored Dec 18, 2024
1 parent a5bb208 commit 3a9e4d3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
31 changes: 2 additions & 29 deletions components/Todo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,11 @@ import TextField from '@mui/material/TextField'
import Grid from '@mui/material/Grid'
import TodoItem from '../TodoItem'
import styles from './index.module.css'
import { useTodo } from '../../hooks/useTodo'

const Todo = () => {
const initialState = [
{
id: 'vnode',
text: 'A simple initial todo',
completed: false,
},
]
const [todos, setTodos] = useState(initialState)
const [text, setText] = useState('')

const addTodo = (text) => {
const todo = {
id: Math.random().toString(36).substring(2),
text,
completed: false,
}
setTodos([...todos, todo])
}

const removeTodo = (todo) => {
const filteredTodos = todos.filter((v) => v !== todo)
setTodos(filteredTodos)
}

const updateTodo = (todo) => {
const updatedTodos = todos.map((v) => (v.id === todo.id ? todo : v))
setTodos(updatedTodos)
}

const completedTodos = todos.filter((todo) => todo.completed)
const { todos, addTodo, updateTodo, removeTodo, completedTodos } = useTodo()

const handleAddTodo = (e) => {
e.preventDefault()
Expand Down
41 changes: 41 additions & 0 deletions hooks/useTodo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useState } from 'react'

export const useTodo = () => {
const initialState = [
{
id: 'vnode',
text: 'A simple initial todo',
completed: false,
},
]
const [todos, setTodos] = useState(initialState)

const addTodo = (text) => {
const todo = {
id: Math.random().toString(36).substring(2),
text,
completed: false,
}
setTodos([...todos, todo])
}

const removeTodo = (todo) => {
const filteredTodos = todos.filter((v) => v !== todo)
setTodos(filteredTodos)
}

const updateTodo = (todo) => {
const updatedTodos = todos.map((v) => (v.id === todo.id ? todo : v))
setTodos(updatedTodos)
}

const completedTodos = todos.filter((todo) => todo.completed)

return {
todos,
addTodo,
removeTodo,
updateTodo,
completedTodos,
}
}

0 comments on commit 3a9e4d3

Please sign in to comment.