Integrated redux to application. Task list now is using list from the store. Added task marking as done and removal

This commit is contained in:
Dmitriy Shishkov 2021-10-17 17:10:09 +03:00
parent 0a135dc17c
commit 32d388423b
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
3 changed files with 31 additions and 25 deletions

View File

@ -1,25 +1,17 @@
import React from "react"; import React from "react";
import { Provider } from "react-redux";
import { Layout } from "./components/Layout"; import { Layout } from "./components/Layout";
import { TodoList } from "./components/TodoList"; import { TodoList } from "./components/TodoList";
import { AppBar } from "./components/AppBar"; import { AppBar } from "./components/AppBar";
import { TaskItemT } from "./types";
import { AddTask } from "./components/AddTask"; import { AddTask } from "./components/AddTask";
import store from "./store";
const tasks: TaskItemT[] = [
{ text: "test", done: false, id: 0 },
{ text: "Long, a bit tooooooo0000000000oooooo long test", done: true, id: 1 },
];
export const App = () => { export const App = () => {
return ( return (
<> <Provider store={store}>
<Layout <Layout appBar={<AppBar />} title="My tasks" content={<TodoList />} />
appBar={<AppBar />}
title="My tasks"
content={<TodoList tasks={tasks} />}
/>
<AddTask /> <AddTask />
</> </Provider>
); );
}; };

View File

@ -9,17 +9,24 @@ import TextField from "@mui/material/TextField";
import DeleteOutlined from "@mui/icons-material/DeleteOutlined"; import DeleteOutlined from "@mui/icons-material/DeleteOutlined";
import { TaskItemT } from "../types"; import { TaskItemT } from "../types";
import { useAppDispatch } from "../hooks";
import { markDone, remove } from "../store/slices/todo";
export type TodoItemProps = { task: TaskItemT }; export type TodoItemProps = { task: TaskItemT; index: number };
export const TodoItem: React.FC<TodoItemProps> = ({ task }) => { export const TodoItem: React.FC<TodoItemProps> = ({ task, index }) => {
const { done, text } = task; const { done, text } = task;
const dispatch = useAppDispatch();
const [editing, setEditing] = useState(false); const [editing, setEditing] = useState(false);
return ( return (
<ListItem> <ListItem>
<Checkbox checked={done} /> <Checkbox
checked={done}
onClick={() => dispatch(markDone({ index, value: !done }))}
/>
{editing ? ( {editing ? (
<TextField <TextField
fullWidth fullWidth
@ -30,10 +37,19 @@ export const TodoItem: React.FC<TodoItemProps> = ({ task }) => {
multiline multiline
/> />
) : ( ) : (
<ListItemText onClick={() => setEditing(true)} primary={text} /> <ListItemText
sx={{
textDecoration: done ? "line-through" : undefined,
}}
onClick={() => setEditing(true)}
primary={text}
/>
)} )}
<ListItemSecondaryAction> <ListItemSecondaryAction>
<IconButton aria-label="Delete Todo"> <IconButton
aria-label="Delete Todo"
onClick={() => dispatch(remove(index))}
>
<DeleteOutlined /> <DeleteOutlined />
</IconButton> </IconButton>
</ListItemSecondaryAction> </ListItemSecondaryAction>

View File

@ -4,18 +4,16 @@ import List from "@mui/material/List";
import Paper from "@mui/material/Paper"; import Paper from "@mui/material/Paper";
import { TodoItem } from "./TodoItem"; import { TodoItem } from "./TodoItem";
import { TaskItemT } from "../types"; import { useAppSelector } from "../hooks";
export type TodoListProps = { export const TodoList: React.FC = () => {
tasks: TaskItemT[]; const tasks = useAppSelector((state) => state.todo.tasks);
};
export const TodoList: React.FC<TodoListProps> = ({ tasks }) => {
return ( return (
<Paper variant="outlined"> <Paper variant="outlined">
<List> <List>
{tasks.map((task) => ( {tasks.map((task, index) => (
<TodoItem key={task.id} task={task} /> <TodoItem key={task.id} task={task} index={index} />
))} ))}
</List> </List>
</Paper> </Paper>