Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip: favorite
  • Loading branch information
hi-ogawa committed Apr 7, 2024
commit e61933d08b988e62ce364268d3571a30323e34d1
10 changes: 10 additions & 0 deletions src/routes/_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ export async function actionUpdateContact(
throw redirect(`/contacts/${contact.id}`);
}

export async function actoinFavorite(formData: FormData) {
const data = Object.fromEntries(formData) as any;
const contact = await fakeContacts.get(data.id);
tinyassert(contact);
await fakeContacts.set(contact.id, {
...contact,
favorite: data.favorite === "true",
});
}

export async function actionDeleteContact(
this: ActionContext,
formData: FormData,
Expand Down
6 changes: 3 additions & 3 deletions src/routes/contacts/[contactId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createError, type PageProps } from "@hiogawa/react-server/server";
import { getContact, type ContactRecord } from "../../_data";
import { Link } from "@hiogawa/react-server/client";
import { actionDeleteContact } from "../../_action";
import { actionDeleteContact, actoinFavorite } from "../../_action";

export default async function Contact(props: PageProps) {
const contact = await getContact(props.params["contactId"]);
Expand Down Expand Up @@ -61,9 +61,9 @@ export default async function Contact(props: PageProps) {
function Favorite(props: { contact: ContactRecord }) {
const favorite = props.contact.favorite;

// TODO
return (
<form method="post">
<form action={actoinFavorite}>
<input type="hidden" name="id" value={props.contact.id} />
<button
aria-label={favorite ? "Remove from favorites" : "Add to favorites"}
name="favorite"
Expand Down