mirror of
https://github.com/LucasVbr/todo-list.git
synced 2026-12-05 17:30:49 +00:00
Rebuild the app using React ⚛️
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import {FormEvent, useState} from 'react';
|
||||
import {useDispatch} from 'react-redux';
|
||||
import {addTodo} from '../../features/todo/todoSlice.ts';
|
||||
|
||||
export default function TodoAddItemForm() {
|
||||
const [name, setName] = useState('');
|
||||
const dispatch = useDispatch();
|
||||
|
||||
const handleSubmit = (evt: FormEvent) => {
|
||||
evt.preventDefault();
|
||||
if (!name) return;
|
||||
dispatch(addTodo(name));
|
||||
setName("");
|
||||
};
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className={'TodoAddItemForm'}>
|
||||
<div className={'form-control'}>
|
||||
<div className={'input-group'}>
|
||||
<input type={'text'} value={name} placeholder={'Type here your task...'}
|
||||
className={'input input-bordered w-full'}
|
||||
onChange={evt => setName(evt.target.value)}
|
||||
/>
|
||||
<button type={'submit'} className={'btn btn-square'}>
|
||||
<img className={'h-6 w-6'} src={'/icons/plus.svg'}
|
||||
alt="Plus icon"/>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import TodoItemList from './TodoItemList.tsx';
|
||||
import TodoAddItemForm from './TodoAddItemForm.tsx';
|
||||
import TodoCardFooter from './TodoCardFooter.tsx';
|
||||
import {useSelector} from 'react-redux';
|
||||
import {RootState} from '../../store.ts';
|
||||
|
||||
export default function TodoCard() {
|
||||
const todos = useSelector((state: RootState) => state.todo);
|
||||
|
||||
return (
|
||||
<div className={'TodoCard card bg-base-100 shadow-xl p-5 gap-7 transition ease-in-out delay-150'}>
|
||||
<TodoAddItemForm/>
|
||||
{todos.length > 0 && (
|
||||
<>
|
||||
<TodoItemList todos={todos}/>
|
||||
<TodoCardFooter/>
|
||||
</>
|
||||
)}
|
||||
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import {useDispatch, useSelector} from 'react-redux';
|
||||
import {deleteSelectedTodos} from '../../features/todo/todoSlice.ts';
|
||||
import {RootState} from '../../store.ts';
|
||||
import TodoState from '../../models/TodoState.ts';
|
||||
|
||||
export default function TodoCardFooter() {
|
||||
|
||||
const dispatch = useDispatch();
|
||||
const selectedTodosCount: number = useSelector((state: RootState) => state.todo.filter((todo: TodoState) => !todo.checked).length);
|
||||
|
||||
const handleDelete = () => dispatch(deleteSelectedTodos());
|
||||
|
||||
return (
|
||||
<div className={'flex justify-between items-center gap-5'}>
|
||||
<p>You have {selectedTodosCount} pending task(s)</p>
|
||||
<button onClick={handleDelete} className={'btn btn-error'}>Clear
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import type TodoState from '../../models/TodoState.ts';
|
||||
import clsx from 'clsx';
|
||||
import {useDispatch} from 'react-redux';
|
||||
import {checkTodo} from '../../features/todo/todoSlice.ts';
|
||||
|
||||
type Props = {
|
||||
todo: TodoState
|
||||
}
|
||||
|
||||
export default function TodoItem({todo}: Props) {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
const handleCheck = () => dispatch(checkTodo(todo.id));
|
||||
|
||||
return (
|
||||
<label className={clsx('TodoItem', 'shadow rounded-lg p-4 w-full flex items-center justify-between cursor-pointer')}>
|
||||
{todo.name}
|
||||
<input type={"checkbox"} onChange={handleCheck} checked={todo.checked} className={"checkbox checkbox-primary"}/>
|
||||
</label>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import TodoItem from './TodoItem.tsx';
|
||||
import type TodoState from '../../models/TodoState.ts';
|
||||
|
||||
type Props = {
|
||||
todos: TodoState[]
|
||||
}
|
||||
|
||||
export default function TodoItemList({todos}: Props) {
|
||||
return (
|
||||
<div className={'TodoItemList w-full flex flex-col gap-2'}>
|
||||
{todos.map((todo: TodoState, index) => <TodoItem todo={todo} key={index}/>)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user