from dataclasses import replace from typing import List, Sequence from .todo_item import Todo, TodoDraft from .todo_list_add_todo import checked_draft def index_of_todo(todos: Sequence[Todo], id: str) -> int: """The position of the todo with this id, or the unknown-id error every list operation shares.""" for i, t in enumerate(todos): if t.id == id: return i raise ValueError(f'no todo with id "{id}"') def update_todo(todos: Sequence[Todo], id: str, draft: TodoDraft) -> List[Todo]: """Save the edit form; id, done state, timestamps, order and place are kept.""" at = index_of_todo(todos, id) clean = checked_draft(draft) out = list(todos) out[at] = replace( todos[at], title=clean.title, notes=clean.notes, priority=clean.priority, due=clean.due, tags=clean.tags, recurrence=clean.recurrence, ) return out