Skip to content

Commit

Permalink
feat: TodoList.edit에서 prev와 next 정보 업데이트 하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
n-ryu committed Nov 28, 2022
1 parent dec1aa8 commit 0926e7d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions client/src/core/todo/todoList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,24 @@ export class TodoList {
}

async edit(id: string, todo: InputTodo): Promise<TodoList> {
const newTodoList = this.todoList.filter((el) => el.id !== id);
newTodoList.push(new Todo({ ...todo, id }));
const oldTodo = this.todoList.find((el) => el.id === id);
if (oldTodo === undefined) throw new Error('ERROR: 수정하려는 ID의 Todo가 존재하지 않습니다.');
const newTodo = new Todo(todo);

this.getPrev(oldTodo).forEach((el) => el.removeNext(oldTodo.id));
this.getPrev(newTodo).forEach((el) => el.addNext(newTodo.id));

this.updateTodoState(newTodo);

this.getNext(oldTodo).forEach((el) => el.removePrev(oldTodo.id));
this.getNext(newTodo).forEach((el) => el.addPrev(newTodo.id));

this.getNext(oldTodo).forEach((el) => this.updateTodoState(el));
this.getNext(newTodo).forEach((el) => this.updateTodoState(el));

const newTodoList = this.todoList.filter((el) => el !== oldTodo);
newTodoList.push(newTodo);

return new TodoList(newTodoList.map((el) => el.toPlain()));
}

Expand Down

0 comments on commit 0926e7d

Please sign in to comment.