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: add edit page
  • Loading branch information
hi-ogawa committed Apr 5, 2024
commit 822029d36d481e5f3a524a23047cc3c6937cb68b
9 changes: 9 additions & 0 deletions src/routes/_action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use server";

import { redirect } from "@hiogawa/react-server/server";
import { createEmptyContact } from "./_data";

export async function actionNewContact() {
const contact = await createEmptyContact();
throw redirect(`/contacts/${contact.id}/edit`);
}
2 changes: 1 addition & 1 deletion src/routes/_data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { tinyassert } from "@hiogawa/utils";

// verify this only lives on the server
// verify the data accessed only on the server
tinyassert(typeof document === "undefined");

type ContactMutation = {
Expand Down
58 changes: 58 additions & 0 deletions src/routes/contacts/[contactId]/edit/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { createError, type PageProps } from "@hiogawa/react-server/server";
import { getContact } from "../../../_data";

export default async function EditContact(props: PageProps) {
const contact = await getContact(props.params["contactId"]);
if (!contact) {
throw createError({ status: 404 });
}

return (
<form key={contact.id} id="contact-form" method="post">
<p>
<span>Name</span>
<input
defaultValue={contact.first}
aria-label="First name"
name="first"
type="text"
placeholder="First"
/>
<input
aria-label="Last name"
defaultValue={contact.last}
name="last"
placeholder="Last"
type="text"
/>
</p>
<label>
<span>Twitter</span>
<input
defaultValue={contact.twitter}
name="twitter"
placeholder="@jack"
type="text"
/>
</label>
<label>
<span>Avatar URL</span>
<input
aria-label="Avatar URL"
defaultValue={contact.avatar}
name="avatar"
placeholder="https://example.com/avatar.jpg"
type="text"
/>
</label>
<label>
<span>Notes</span>
<textarea defaultValue={contact.notes} name="notes" rows={6} />
</label>
<p>
<button type="submit">Save</button>
<button type="button">Cancel</button>
</p>
</form>
);
}
43 changes: 18 additions & 25 deletions src/routes/contacts/[contactId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
"use client";
import { createError, type PageProps } from "@hiogawa/react-server/server";
import { getContact, type ContactRecord } from "../../_data";

import type { ContactRecord } from "../../_data";

export default function Contact() {
const contact = {
first: "Your",
last: "Name",
avatar: "https://placekitten.com/g/200/200",
twitter: "your_handle",
notes: "Some notes",
favorite: true,
};
export default async function Contact(props: PageProps) {
const contact = await getContact(props.params["contactId"]);
if (!contact) {
throw createError({ status: 404 });
}

return (
<div id="contact">
Expand Down Expand Up @@ -52,14 +47,14 @@ export default function Contact() {
<form
action="destroy"
method="post"
onSubmit={(event) => {
const response = confirm(
"Please confirm you want to delete this record.",
);
if (!response) {
event.preventDefault();
}
}}
// onSubmit={(event) => {
// const response = confirm(
// "Please confirm you want to delete this record.",
// );
// if (!response) {
// event.preventDefault();
// }
// }}
>
<button type="submit">Delete</button>
</form>
Expand All @@ -69,10 +64,8 @@ export default function Contact() {
);
}

const Favorite: React.FC<{
contact: Pick<ContactRecord, "favorite">;
}> = ({ contact }) => {
const favorite = contact.favorite;
function Favorite(props: { contact: ContactRecord }) {
const favorite = props.contact.favorite;

return (
<form method="post">
Expand All @@ -85,4 +78,4 @@ const Favorite: React.FC<{
</button>
</form>
);
};
}
5 changes: 4 additions & 1 deletion src/routes/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Link } from "@hiogawa/react-server/client";
import { getContacts } from "./_data";
import { actionNewContact } from "./_action";

export default async function Layout(props: React.PropsWithChildren) {
const contacts = await getContacts();
Expand All @@ -25,7 +26,9 @@ export default async function Layout(props: React.PropsWithChildren) {
/>
<div aria-hidden hidden={true} id="search-spinner" />
</form>
<form method="post">
{/* TODO: better error message when forgot to add `action`? */}
{/* TODO: layout doesn't invalidate when adding a new contact */}
<form action={actionNewContact}>
<button type="submit">New</button>
</form>
</div>
Expand Down