Skip to content

Commit e75d9a3

Browse files
author
Mike Nikles
committed
Add PATCH endpoint - mark as done.
1 parent 8d7a54c commit e75d9a3

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

src/lib/todo-item.svelte

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
opacity: 1;
7171
}
7272
73-
/* TODO: Uncomment when the API endpoints are available
7473
.done {
7574
transform: none;
7675
opacity: 0.4;
@@ -79,13 +78,13 @@
7978
8079
.done .toggle {
8180
background-image: url("data:image/svg+xml,%3Csvg width='22' height='16' viewBox='0 0 22 16' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M20.5 1.5L7.4375 14.5L1.5 8.5909' stroke='%23676778' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
82-
} */
81+
}
8382
</style>
8483

85-
<div class="todo">
86-
<form action="" method="">
87-
<input type="hidden" name="done" value="" />
88-
<button aria-label="Mark done/not done" class="toggle"></button>
84+
<div class="todo" class:done={todo.done}>
85+
<form action="/todos/{todo.uid}.json?_method=patch" method="post">
86+
<input type="hidden" name="done" value="{todo.done ? '' : 'true'}" />
87+
<button aria-label="Mark todo as {todo.done ? 'not done' : 'done'}" class="toggle"></button>
8988
</form>
9089

9190
<form action="/todos/{todo.uid}.json?_method=patch" method="post" class="text">

src/routes/todos/[uid].json.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const del: RequestHandler = (request) => {
77

88
export const patch: RequestHandler<{}, FormData> = (request) => {
99
return api(request, {
10-
text: request.body.get("text")
10+
text: request.body.get("text"),
11+
done: request.body.has("done") ? !!request.body.get("done") : undefined
1112
});
1213
}

src/routes/todos/_api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export const api = (request: Request, data?: Record<string, unknown>) => {
2424
case "PATCH":
2525
todos = todos.map(todo => {
2626
if (todo.uid === request.params.uid) {
27-
todo.text = data.text as string;
27+
if (data.text) todo.text = data.text as string;
28+
else todo.done = data.done as boolean;
2829
}
2930
return todo;
3031
});

0 commit comments

Comments
 (0)