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 の更新機能の実装」の一例 #6

Merged
merged 1 commit 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
13 changes: 11 additions & 2 deletions src/features/todos/components/TodoList.tsx
@@ -1,6 +1,6 @@
import type { FC } from 'react';
import { useAppSelector, useAppDispatch } from '../../../app/hooks';
import { remove, selectTodos } from '../todosSlice';
import { remove, update, selectTodos } from '../todosSlice';

export const TodoList: FC = () => {
const todos = useAppSelector(selectTodos);
Expand Down Expand Up @@ -43,7 +43,16 @@ export const TodoList: FC = () => {
<td>
<button
onClick={() => {
//TODO: 更新機能の実装
// ここでは決め打ちでtitleとbodyのみを更新しているが、
// 最終的にはtitle, body, statusに好きな値を入力できるようにする
const updateAction = update({
id: todo.id,
input: {
title: '更新したtitle' + Date.now(),
body: '更新したbody ' + Date.now(),
},
});
dispatch(updateAction);
}}
>
更新
Expand Down
1 change: 1 addition & 0 deletions src/features/todos/crud/index.ts
@@ -1,2 +1,3 @@
export { createTodo } from './create';
export { removeTodo } from './remove';
export { updateTodo } from './update';
9 changes: 9 additions & 0 deletions src/features/todos/crud/update.ts
@@ -0,0 +1,9 @@
import type { Todo } from '../types';
import { getCurrentDateTime } from '../utils/date';

export const updateTodo = (todo: Todo): Todo => {
return {
...todo,
updatedAt: getCurrentDateTime(),
};
};
17 changes: 14 additions & 3 deletions src/features/todos/todosSlice.ts
@@ -1,8 +1,8 @@
import { createSlice } from '@reduxjs/toolkit';
import type { PayloadAction } from '@reduxjs/toolkit';
import type { RootState } from '../../app/store';
import type { TodoInput, Todo, TodoId } from './types';
import { createTodo, removeTodo } from './crud';
import type { TodoInput, Todo, TodoId, TodoUpdatePayload } from './types';
import { createTodo, removeTodo, updateTodo } from './crud';

export type TodoState = {
todos: Todo[];
Expand Down Expand Up @@ -32,10 +32,21 @@ export const todoSlice = createSlice({

state.todos[index] = removeTodo(todo);
},
update: (state, action: PayloadAction<TodoUpdatePayload>) => {
const { id, input } = action.payload;
const index = state.todos.findIndex((todo) => todo.id === id);
const todo = state.todos[index];
if (!todo) return;

state.todos[index] = updateTodo({
...todo,
...input,
});
},
},
});

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

export const selectTodos = (state: RootState) =>
state.todos.todos.filter((todo) => todo.deletedAt === undefined);
Expand Down
9 changes: 9 additions & 0 deletions src/features/todos/types.ts
@@ -1,5 +1,7 @@
export type TodoId = string;

export type DateTime = string;

const TODO_STATUSES = [
'waiting',
'working',
Expand All @@ -8,6 +10,7 @@ const TODO_STATUSES = [
'completed',
] as const;
export type TodoStatus = typeof TODO_STATUSES[number];

export type TodoInput = {
id?: TodoId;
title: string;
Expand All @@ -17,6 +20,7 @@ export type TodoInput = {
updatedAt?: DateTime;
deletedAt?: DateTime;
};

export type Todo = {
id: TodoId;
title: string;
Expand All @@ -26,3 +30,8 @@ export type Todo = {
updatedAt?: DateTime;
deletedAt?: DateTime;
};

export type TodoUpdatePayload = {
id: TodoId;
input: Partial<TodoInput>;
};