Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

day3 「Todo の復元機能の実装」の一例 #7

Merged
merged 2 commits into from Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/features/todos/components/TodoContainer.tsx
@@ -1,13 +1,22 @@
import type { FC } from 'react';
import { useAppSelector } from '../../../app/hooks';
import { selectTodos, selectDeletedTodos } from '../todosSlice';
import { TodoForm } from './TodoForm';
import { TodoList } from './TodoList';

export const TodoContainer: FC = () => {
const todos = useAppSelector(selectTodos);
const deletedTodos = useAppSelector(selectDeletedTodos);

return (
<div>
<TodoForm />
<hr />
<TodoList />
<h2>Todo一覧</h2>
<TodoList todos={todos} />
<hr />
<h2>削除されたTodo一覧</h2>
<TodoList todos={deletedTodos} />
</div>
);
};
40 changes: 29 additions & 11 deletions src/features/todos/components/TodoList.tsx
@@ -1,9 +1,13 @@
import type { FC } from 'react';
import { useAppSelector, useAppDispatch } from '../../../app/hooks';
import { remove, update, selectTodos } from '../todosSlice';
import { useAppDispatch } from '../../../app/hooks';
import { remove, update, restore } from '../todosSlice';
import { Todo } from '../types';

export const TodoList: FC = () => {
const todos = useAppSelector(selectTodos);
type Props = {
todos: Todo[];
};

export const TodoList: FC<Props> = ({ todos }) => {
const dispatch = useAppDispatch();

return (
Expand Down Expand Up @@ -59,13 +63,23 @@ export const TodoList: FC = () => {
</button>
</td>
<td>
<button
onClick={() => {
dispatch(remove(todo.id));
}}
>
削除
</button>
{isDeletedTodo(todo) ? (
<button
onClick={() => {
dispatch(restore(todo.id));
}}
>
復元
</button>
) : (
<button
onClick={() => {
dispatch(remove(todo.id));
}}
>
削除
</button>
)}
</td>
</tr>
);
Expand All @@ -77,4 +91,8 @@ export const TodoList: FC = () => {
);
};

const isDeletedTodo = (todo: Todo) => {
return todo.deletedAt !== undefined;
};

export default TodoList;
1 change: 1 addition & 0 deletions src/features/todos/crud/index.ts
@@ -1,3 +1,4 @@
export { createTodo } from './create';
export { removeTodo } from './remove';
export { updateTodo } from './update';
export { restoreTodo } from './restore';
8 changes: 8 additions & 0 deletions src/features/todos/crud/restore.ts
@@ -0,0 +1,8 @@
import type { Todo } from '../types';

export const restoreTodo = (todo: Todo): Todo => {
return {
...todo,
deletedAt: undefined,
};
};
15 changes: 13 additions & 2 deletions src/features/todos/todosSlice.ts
Expand Up @@ -2,7 +2,7 @@ import { createSlice } from '@reduxjs/toolkit';
import type { PayloadAction } from '@reduxjs/toolkit';
import type { RootState } from '../../app/store';
import type { TodoInput, Todo, TodoId, TodoUpdatePayload } from './types';
import { createTodo, removeTodo, updateTodo } from './crud';
import { createTodo, removeTodo, updateTodo, restoreTodo } from './crud';

export type TodoState = {
todos: Todo[];
Expand Down Expand Up @@ -43,12 +43,23 @@ export const todoSlice = createSlice({
...input,
});
},
restore: (state, action: PayloadAction<TodoId>) => {
const id = action.payload;
const index = state.todos.findIndex((todo) => todo.id === id);
const todo = state.todos[index];
if (!todo) return;

state.todos[index] = restoreTodo(todo);
},
},
});

export const { create, remove, update } = todoSlice.actions;
export const { create, remove, update, restore } = todoSlice.actions;

export const selectTodos = (state: RootState) =>
state.todos.todos.filter((todo) => todo.deletedAt === undefined);

export const selectDeletedTodos = (state: RootState) =>
state.todos.todos.filter((todo) => todo.deletedAt !== undefined);

export default todoSlice.reducer;