Compare commits
14 Commits
90c0dd6812
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| f60847f9aa | |||
| 57c7440e65 | |||
| 94346dd242 | |||
| a54d7b57ee | |||
| b73c9311f5 | |||
| 434b7c8dc8 | |||
| 800c231580 | |||
| aa81948f29 | |||
| a4536d20b4 | |||
| 75337be90c | |||
| 298d25a579 | |||
| 276bea81b0 | |||
| 595520296c | |||
| 4330157491 |
@@ -9,10 +9,12 @@
|
|||||||
"lint": "next lint"
|
"lint": "next lint"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"lowdb": "^7.0.1",
|
||||||
"match-sorter": "^8.0.0",
|
"match-sorter": "^8.0.0",
|
||||||
"next": "15.2.4",
|
"next": "15.2.4",
|
||||||
"react": "^19.0.0",
|
"react": "^19.0.0",
|
||||||
"react-dom": "^19.0.0",
|
"react-dom": "^19.0.0",
|
||||||
|
"server-only": "^0.0.1",
|
||||||
"sort-by": "^1.2.0",
|
"sort-by": "^1.2.0",
|
||||||
"tiny-invariant": "^1.3.3"
|
"tiny-invariant": "^1.3.3"
|
||||||
},
|
},
|
||||||
|
|||||||
25
pnpm-lock.yaml
generated
25
pnpm-lock.yaml
generated
@@ -8,6 +8,9 @@ importers:
|
|||||||
|
|
||||||
.:
|
.:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
lowdb:
|
||||||
|
specifier: ^7.0.1
|
||||||
|
version: 7.0.1
|
||||||
match-sorter:
|
match-sorter:
|
||||||
specifier: ^8.0.0
|
specifier: ^8.0.0
|
||||||
version: 8.0.0
|
version: 8.0.0
|
||||||
@@ -20,6 +23,9 @@ importers:
|
|||||||
react-dom:
|
react-dom:
|
||||||
specifier: ^19.0.0
|
specifier: ^19.0.0
|
||||||
version: 19.1.0(react@19.1.0)
|
version: 19.1.0(react@19.1.0)
|
||||||
|
server-only:
|
||||||
|
specifier: ^0.0.1
|
||||||
|
version: 0.0.1
|
||||||
sort-by:
|
sort-by:
|
||||||
specifier: ^1.2.0
|
specifier: ^1.2.0
|
||||||
version: 1.2.0
|
version: 1.2.0
|
||||||
@@ -1118,6 +1124,10 @@ packages:
|
|||||||
resolution: {integrity: sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==}
|
resolution: {integrity: sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
|
|
||||||
|
lowdb@7.0.1:
|
||||||
|
resolution: {integrity: sha512-neJAj8GwF0e8EpycYIDFqEPcx9Qz4GUho20jWFR7YiFeXzF1YMLdxB36PypcTSPMA+4+LvgyMacYhlr18Zlymw==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
|
||||||
match-sorter@8.0.0:
|
match-sorter@8.0.0:
|
||||||
resolution: {integrity: sha512-bGJ6Zb+OhzXe+ptP5d80OLVx7AkqfRbtGEh30vNSfjNwllu+hHI+tcbMIT/fbkx/FKN1PmKuDb65+Oofg+XUxw==}
|
resolution: {integrity: sha512-bGJ6Zb+OhzXe+ptP5d80OLVx7AkqfRbtGEh30vNSfjNwllu+hHI+tcbMIT/fbkx/FKN1PmKuDb65+Oofg+XUxw==}
|
||||||
|
|
||||||
@@ -1348,6 +1358,9 @@ packages:
|
|||||||
engines: {node: '>=10'}
|
engines: {node: '>=10'}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
|
|
||||||
|
server-only@0.0.1:
|
||||||
|
resolution: {integrity: sha512-qepMx2JxAa5jjfzxG79yPPq+8BuFToHd1hm7kI+Z4zAq1ftQiP7HcxMhDDItrbtwVeLg/cY2JnKnrcFkmiswNA==}
|
||||||
|
|
||||||
set-function-length@1.2.2:
|
set-function-length@1.2.2:
|
||||||
resolution: {integrity: sha512-pgRc4hJ4/sNjWCSS9AmnS40x3bNMDTknHgL5UaMBTMyJnU90EgWh1Rz+MC9eFu4BuN/UwZjKQuY/1v3rM7HMfg==}
|
resolution: {integrity: sha512-pgRc4hJ4/sNjWCSS9AmnS40x3bNMDTknHgL5UaMBTMyJnU90EgWh1Rz+MC9eFu4BuN/UwZjKQuY/1v3rM7HMfg==}
|
||||||
engines: {node: '>= 0.4'}
|
engines: {node: '>= 0.4'}
|
||||||
@@ -1401,6 +1414,10 @@ packages:
|
|||||||
stable-hash@0.0.5:
|
stable-hash@0.0.5:
|
||||||
resolution: {integrity: sha512-+L3ccpzibovGXFK+Ap/f8LOS0ahMrHTf3xu7mMLSpEGU0EO9ucaysSylKo9eRDFNhWve/y275iPmIZ4z39a9iA==}
|
resolution: {integrity: sha512-+L3ccpzibovGXFK+Ap/f8LOS0ahMrHTf3xu7mMLSpEGU0EO9ucaysSylKo9eRDFNhWve/y275iPmIZ4z39a9iA==}
|
||||||
|
|
||||||
|
steno@4.0.2:
|
||||||
|
resolution: {integrity: sha512-yhPIQXjrlt1xv7dyPQg2P17URmXbuM5pdGkpiMB3RenprfiBlvK415Lctfe0eshk90oA7/tNq7WEiMK8RSP39A==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
|
||||||
streamsearch@1.1.0:
|
streamsearch@1.1.0:
|
||||||
resolution: {integrity: sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==}
|
resolution: {integrity: sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==}
|
||||||
engines: {node: '>=10.0.0'}
|
engines: {node: '>=10.0.0'}
|
||||||
@@ -2758,6 +2775,10 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
js-tokens: 4.0.0
|
js-tokens: 4.0.0
|
||||||
|
|
||||||
|
lowdb@7.0.1:
|
||||||
|
dependencies:
|
||||||
|
steno: 4.0.2
|
||||||
|
|
||||||
match-sorter@8.0.0:
|
match-sorter@8.0.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@babel/runtime': 7.27.0
|
'@babel/runtime': 7.27.0
|
||||||
@@ -2996,6 +3017,8 @@ snapshots:
|
|||||||
|
|
||||||
semver@7.7.1: {}
|
semver@7.7.1: {}
|
||||||
|
|
||||||
|
server-only@0.0.1: {}
|
||||||
|
|
||||||
set-function-length@1.2.2:
|
set-function-length@1.2.2:
|
||||||
dependencies:
|
dependencies:
|
||||||
define-data-property: 1.1.4
|
define-data-property: 1.1.4
|
||||||
@@ -3092,6 +3115,8 @@ snapshots:
|
|||||||
|
|
||||||
stable-hash@0.0.5: {}
|
stable-hash@0.0.5: {}
|
||||||
|
|
||||||
|
steno@4.0.2: {}
|
||||||
|
|
||||||
streamsearch@1.1.0: {}
|
streamsearch@1.1.0: {}
|
||||||
|
|
||||||
string.prototype.includes@2.0.1:
|
string.prototype.includes@2.0.1:
|
||||||
|
|||||||
@@ -1,57 +1,60 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import Form from "next/form";
|
|
||||||
import { ContactRecord } from "@/app/data";
|
import { ContactRecord } from "@/app/data";
|
||||||
|
import Form from "next/form";
|
||||||
|
import { use } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
|
||||||
export default function Contact() {
|
export default function Contact({
|
||||||
const contact = {
|
contact
|
||||||
first: "Your",
|
}: {
|
||||||
last: "Name",
|
contact: Promise<ContactRecord>
|
||||||
avatar: "https://placecats.com/200/200",
|
}) {
|
||||||
twitter: "your_handle",
|
const c = use(contact);
|
||||||
notes: "Some notes",
|
const router = useRouter();
|
||||||
favorite: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div id="contact">
|
<div id="contact">
|
||||||
<div>
|
<div>
|
||||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||||
<img
|
<img
|
||||||
alt={`${contact.first} ${contact.last} avatar`}
|
alt={`${c.first} ${c.last} avatar`}
|
||||||
key={contact.avatar}
|
key={c.avatar}
|
||||||
src={contact.avatar}
|
src={c.avatar}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<h1>
|
<h1>
|
||||||
{contact.first || contact.last ? (
|
{c.first || c.last ? (
|
||||||
<>
|
<>
|
||||||
{contact.first} {contact.last}
|
{c.first} {c.last}
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<i>No Name</i>
|
<i>No Name</i>
|
||||||
)}
|
)}
|
||||||
<Favorite contact={contact}/>
|
<Favorite contact={c}/>
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
{contact.twitter ? (
|
{c.twitter ? (
|
||||||
<p>
|
<p>
|
||||||
<a
|
<a
|
||||||
href={`https://twitter.com/${contact.twitter}`}
|
href={`https://twitter.com/${c.twitter}`}
|
||||||
>
|
>
|
||||||
{contact.twitter}
|
{c.twitter}
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
{contact.notes ? <p>{contact.notes}</p> : null}
|
{c.notes ? <p>{c.notes}</p> : null}
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<Form action="edit">
|
<button
|
||||||
<button type="submit">Edit</button>
|
type="button"
|
||||||
</Form>
|
onClick={() => router.push(`/contacts/${c.id}/edit`)}
|
||||||
|
>
|
||||||
|
Edit
|
||||||
|
</button>
|
||||||
|
|
||||||
<Form
|
<Form
|
||||||
action="destroy"
|
action="destroy"
|
||||||
87
src/app/(sidebar)/contacts/[contactId]/edit/page.tsx
Normal file
87
src/app/(sidebar)/contacts/[contactId]/edit/page.tsx
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
import Form from "next/form";
|
||||||
|
import { fetchContact } from "@/app/(sidebar)/loaders";
|
||||||
|
import { notFound, redirect } from "next/navigation";
|
||||||
|
import { updateContact } from "@/app/data";
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
|
||||||
|
|
||||||
|
async function editContact(contactId: string, formData: FormData) {
|
||||||
|
"use server";
|
||||||
|
const updates = Object.fromEntries(formData);
|
||||||
|
await updateContact(contactId, updates);
|
||||||
|
revalidatePath('/(sidebar)', 'layout'); // THE NESTED BEHAVIOR IS UNSTABLE AND CAN CHANGE IN FUTURE.
|
||||||
|
redirect(`/contacts/${contactId}`); // This makes the server component data be reloaded.
|
||||||
|
/*
|
||||||
|
* The server component architecture is a HTMX-like structure where the component rendered is STATE-LESS
|
||||||
|
* and that means that they don't automagically update.
|
||||||
|
* How do you say a server component to "re-render"? YOU REFRESH THE PAGE! THIS IS AS INTENDED!!!
|
||||||
|
* Do you want a component that re-render interactively from a "signal" client-side?
|
||||||
|
* That's a classical Client Component you dummy!! Go fetch clientside library and use that instead of Server Comps.
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function EditContactPage({
|
||||||
|
params
|
||||||
|
}: {
|
||||||
|
params: Promise<{ contactId: string }>
|
||||||
|
}) {
|
||||||
|
const { contactId } = await params;
|
||||||
|
const contact = await fetchContact(contactId).then((c) => {
|
||||||
|
if (c === null) notFound();
|
||||||
|
return c;
|
||||||
|
});
|
||||||
|
const editThisContact = editContact.bind(null, contactId);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Form key={contact.id} id="contact-form" action={editThisContact}>
|
||||||
|
<p>
|
||||||
|
<span>Name</span>
|
||||||
|
<input
|
||||||
|
aria-label="First name"
|
||||||
|
defaultValue={contact.first}
|
||||||
|
name="first"
|
||||||
|
placeholder="First"
|
||||||
|
type="text"
|
||||||
|
/>
|
||||||
|
<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>
|
||||||
|
);
|
||||||
|
}
|
||||||
5
src/app/(sidebar)/contacts/[contactId]/loading.tsx
Normal file
5
src/app/(sidebar)/contacts/[contactId]/loading.tsx
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export default function ContactLoading() {
|
||||||
|
return (
|
||||||
|
<div>Loading Contact...</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
8
src/app/(sidebar)/contacts/[contactId]/not-found.tsx
Normal file
8
src/app/(sidebar)/contacts/[contactId]/not-found.tsx
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
export default function NotFound() {
|
||||||
|
return (
|
||||||
|
<main id="error-page">
|
||||||
|
<h1>404</h1>
|
||||||
|
<p>The requested contact could not be found.</p>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
22
src/app/(sidebar)/contacts/[contactId]/page.tsx
Normal file
22
src/app/(sidebar)/contacts/[contactId]/page.tsx
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { notFound } from "next/navigation";
|
||||||
|
import Contact from "@/app/(sidebar)/contacts/[contactId]/Contact";
|
||||||
|
import { fetchContact } from "@/app/(sidebar)/loaders";
|
||||||
|
import React, { Suspense } from "react";
|
||||||
|
|
||||||
|
export default async function ContactPage({
|
||||||
|
params
|
||||||
|
}: {
|
||||||
|
params: Promise<{ contactId: string }>
|
||||||
|
}) {
|
||||||
|
const { contactId } = await params;
|
||||||
|
const contact = fetchContact(contactId).then((c) => {
|
||||||
|
if (c === null) notFound();
|
||||||
|
return c;
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Suspense fallback={<div>Loading contact...</div>}>
|
||||||
|
<Contact contact={contact}/>
|
||||||
|
</Suspense>
|
||||||
|
);
|
||||||
|
}
|
||||||
56
src/app/(sidebar)/layout.tsx
Normal file
56
src/app/(sidebar)/layout.tsx
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import React, { Suspense } from "react";
|
||||||
|
import Link from "next/link";
|
||||||
|
import Form from "next/form";
|
||||||
|
import ContactList from "./sidebar-contacts";
|
||||||
|
import { createEmptyContact } from "@/app/data";
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
import { fetchContacts } from "@/app/(sidebar)/loaders";
|
||||||
|
|
||||||
|
async function newContact() {
|
||||||
|
"use server";
|
||||||
|
await createEmptyContact();
|
||||||
|
revalidatePath('/(sidebar)', 'layout');
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function SidebarRootLayout({
|
||||||
|
children,
|
||||||
|
}: Readonly<{
|
||||||
|
children: React.ReactNode;
|
||||||
|
}>) {
|
||||||
|
const contacts = fetchContacts();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div id="sidebar">
|
||||||
|
<h1>
|
||||||
|
<Link href="/about">React Router Contacts</Link>
|
||||||
|
</h1>
|
||||||
|
<div>
|
||||||
|
<Form id="search-form" role="search" action="." formMethod="get">
|
||||||
|
<input
|
||||||
|
aria-label="Search contacts"
|
||||||
|
id="q"
|
||||||
|
name="q"
|
||||||
|
placeholder="Search"
|
||||||
|
type="search"/>
|
||||||
|
<div
|
||||||
|
aria-hidden
|
||||||
|
hidden={true}
|
||||||
|
id="search-spinner"/>
|
||||||
|
</Form>
|
||||||
|
<Form action={newContact}>
|
||||||
|
<button type="submit">New</button>
|
||||||
|
</Form>
|
||||||
|
</div>
|
||||||
|
<nav>
|
||||||
|
<Suspense fallback={<div>Loading...</div>}>
|
||||||
|
<ContactList contacts={contacts}/>
|
||||||
|
</Suspense>
|
||||||
|
</nav>
|
||||||
|
</div>
|
||||||
|
<div id={'detail'}>
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
13
src/app/(sidebar)/loaders.ts
Normal file
13
src/app/(sidebar)/loaders.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { getContact, getContacts } from "@/app/data";
|
||||||
|
|
||||||
|
// This files is a non-needed interface file. I can directly call getContact/s into Server Components.
|
||||||
|
|
||||||
|
export const fetchContacts = async () => {
|
||||||
|
console.log("fetch contacts");
|
||||||
|
return await getContacts();
|
||||||
|
};
|
||||||
|
|
||||||
|
export const fetchContact = async (contactId: string) => {
|
||||||
|
console.log(`fetch contact ${contactId}`);
|
||||||
|
return await getContact(contactId);
|
||||||
|
};
|
||||||
15
src/app/(sidebar)/page.tsx
Normal file
15
src/app/(sidebar)/page.tsx
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
export default function Home() {
|
||||||
|
return (
|
||||||
|
<p id="index-page">
|
||||||
|
This is a demo for React Router.
|
||||||
|
<br/>
|
||||||
|
Check out{" "}
|
||||||
|
<a href="https://reactrouter.com">
|
||||||
|
the docs at reactrouter.com
|
||||||
|
</a>
|
||||||
|
.
|
||||||
|
</p>
|
||||||
|
);
|
||||||
|
}
|
||||||
27
src/app/(sidebar)/sidebar-contacts.tsx
Normal file
27
src/app/(sidebar)/sidebar-contacts.tsx
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { ContactRecord } from "@/app/data";
|
||||||
|
import React, { use } from "react";
|
||||||
|
import Link from "next/link";
|
||||||
|
|
||||||
|
export default function ContactList({ contacts }: { contacts: Promise<ContactRecord[]> }) {
|
||||||
|
const allContacts = use(contacts);
|
||||||
|
return (
|
||||||
|
<ul>
|
||||||
|
{allContacts.map((c) => (
|
||||||
|
<li key={c.id}>
|
||||||
|
<Link href={`/contacts/${c.id}`}>
|
||||||
|
{c.first || c.last ? (
|
||||||
|
<>
|
||||||
|
{c.first} {c.last}
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<i>No Name</i>
|
||||||
|
)}
|
||||||
|
{c.favorite ? (
|
||||||
|
<span>★</span>
|
||||||
|
) : null}
|
||||||
|
</Link>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
);
|
||||||
|
}
|
||||||
45
src/app/about/page.tsx
Normal file
45
src/app/about/page.tsx
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import Link from "next/link";
|
||||||
|
|
||||||
|
export default function About() {
|
||||||
|
return (
|
||||||
|
<div id="about">
|
||||||
|
<Link href="/">← Go to demo</Link>
|
||||||
|
<h1>About React Router Contacts</h1>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<p>
|
||||||
|
This is a demo application showing off some of the
|
||||||
|
powerful features of React Router, including
|
||||||
|
dynamic routing, nested routes, loaders, actions,
|
||||||
|
and more.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<h2>Features</h2>
|
||||||
|
<p>
|
||||||
|
Explore the demo to see how React Router handles:
|
||||||
|
</p>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
Data loading and mutations with loaders and
|
||||||
|
actions
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Nested routing with parent/child relationships
|
||||||
|
</li>
|
||||||
|
<li>URL-based routing with dynamic segments</li>
|
||||||
|
<li>Pending and optimistic UI</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<h2>Learn More</h2>
|
||||||
|
<p>
|
||||||
|
Check out the official documentation at{" "}
|
||||||
|
<a href="https://reactrouter.com">
|
||||||
|
reactrouter.com
|
||||||
|
</a>{" "}
|
||||||
|
to learn more about building great web
|
||||||
|
applications with React Router.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
539
src/app/data.ts
539
src/app/data.ts
@@ -2,11 +2,16 @@
|
|||||||
// 🛑 Nothing in here has anything to do with React Router, it's just a fake database
|
// 🛑 Nothing in here has anything to do with React Router, it's just a fake database
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// noinspection UnnecessaryLocalVariableJS,JSUnusedGlobalSymbols
|
// noinspection UnnecessaryLocalVariableJS,JSUnusedGlobalSymbols
|
||||||
|
import "server-only";
|
||||||
|
|
||||||
import { matchSorter } from "match-sorter";
|
import { matchSorter } from "match-sorter";
|
||||||
// @ts-expect-error - no types, but it's a tiny function
|
// @ts-expect-error - no types, but it's a tiny function
|
||||||
import sortBy from "sort-by";
|
import sortBy from "sort-by";
|
||||||
import invariant from "tiny-invariant";
|
import invariant from "tiny-invariant";
|
||||||
|
import { Low } from "lowdb";
|
||||||
|
import { JSONFile } from "lowdb/node";
|
||||||
|
import { join } from "path";
|
||||||
|
import { mkdir } from "fs/promises";
|
||||||
|
|
||||||
type ContactMutation = {
|
type ContactMutation = {
|
||||||
id?: string;
|
id?: string;
|
||||||
@@ -23,42 +28,97 @@ export type ContactRecord = ContactMutation & {
|
|||||||
createdAt: string;
|
createdAt: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
// Define the database schema
|
||||||
// This is just a fake DB table. In a real app you'd be talking to a real db or
|
type Schema = {
|
||||||
// fetching from an existing API.
|
records: Record<string, ContactRecord>;
|
||||||
const fakeContacts = {
|
};
|
||||||
records: {} as Record<string, ContactRecord>,
|
|
||||||
|
|
||||||
|
// Set up lowdb
|
||||||
|
const initDb = async (): Promise<Low<Schema>> => {
|
||||||
|
// Ensure the db directory exists
|
||||||
|
const dbDir = join(process.cwd(), '.db');
|
||||||
|
await mkdir(dbDir, { recursive: true });
|
||||||
|
|
||||||
|
const file = join(dbDir, 'contacts.json');
|
||||||
|
const adapter = new JSONFile<Schema>(file);
|
||||||
|
const defaultData: Schema = { records: {} };
|
||||||
|
const db = new Low<Schema>(adapter, defaultData);
|
||||||
|
|
||||||
|
// Load existing data
|
||||||
|
await db.read();
|
||||||
|
|
||||||
|
// Initialize if needed
|
||||||
|
if (!db.data) {
|
||||||
|
db.data = defaultData;
|
||||||
|
}
|
||||||
|
|
||||||
|
return db;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create a singleton instance of the database
|
||||||
|
let dbPromise: Promise<Low<Schema>> | null = null;
|
||||||
|
|
||||||
|
const getDb = () => {
|
||||||
|
if (!dbPromise) {
|
||||||
|
dbPromise = initDb();
|
||||||
|
}
|
||||||
|
return dbPromise;
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// This is a DB wrapper using lowdb for persistence
|
||||||
|
const fakeContacts = {
|
||||||
async getAll(): Promise<ContactRecord[]> {
|
async getAll(): Promise<ContactRecord[]> {
|
||||||
return Object.keys(fakeContacts.records)
|
const db = await getDb();
|
||||||
.map((key) => fakeContacts.records[key])
|
return Object.keys(db.data.records)
|
||||||
|
.map((key) => db.data.records[key])
|
||||||
.sort(sortBy("-createdAt", "last"));
|
.sort(sortBy("-createdAt", "last"));
|
||||||
},
|
},
|
||||||
|
|
||||||
async get(id: string): Promise<ContactRecord | null> {
|
async get(id: string): Promise<ContactRecord | null> {
|
||||||
return fakeContacts.records[id] || null;
|
const db = await getDb();
|
||||||
|
return db.data.records[id] || null;
|
||||||
},
|
},
|
||||||
|
|
||||||
async create(values: ContactMutation): Promise<ContactRecord> {
|
async create(values: ContactMutation): Promise<ContactRecord> {
|
||||||
|
const db = await getDb();
|
||||||
const id = values.id || Math.random().toString(36).substring(2, 9);
|
const id = values.id || Math.random().toString(36).substring(2, 9);
|
||||||
const createdAt = new Date().toISOString();
|
const createdAt = new Date().toISOString();
|
||||||
const newContact = { id, createdAt, ...values };
|
const newContact = { id, createdAt, ...values };
|
||||||
fakeContacts.records[id] = newContact;
|
db.data.records[id] = newContact;
|
||||||
|
await db.write();
|
||||||
return newContact;
|
return newContact;
|
||||||
},
|
},
|
||||||
|
|
||||||
async set(id: string, values: ContactMutation): Promise<ContactRecord> {
|
async set(id: string, values: ContactMutation): Promise<ContactRecord> {
|
||||||
const contact = await fakeContacts.get(id);
|
const db = await getDb();
|
||||||
|
const contact = await this.get(id);
|
||||||
invariant(contact, `No contact found for ${id}`);
|
invariant(contact, `No contact found for ${id}`);
|
||||||
const updatedContact = { ...contact, ...values };
|
const updatedContact = { ...contact, ...values };
|
||||||
fakeContacts.records[id] = updatedContact;
|
db.data.records[id] = updatedContact;
|
||||||
|
await db.write();
|
||||||
return updatedContact;
|
return updatedContact;
|
||||||
},
|
},
|
||||||
|
|
||||||
destroy(id: string): null {
|
async destroy(id: string): Promise<null> {
|
||||||
delete fakeContacts.records[id];
|
const db = await getDb();
|
||||||
|
delete db.data.records[id];
|
||||||
|
await db.write();
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// New reset function to restore the initial data
|
||||||
|
async reset(): Promise<void> {
|
||||||
|
const db = await getDb();
|
||||||
|
db.data.records = {};
|
||||||
|
await db.write();
|
||||||
|
|
||||||
|
// Restore initial data
|
||||||
|
const initialContacts = getInitialContacts();
|
||||||
|
for (const contact of initialContacts) {
|
||||||
|
await this.create(contact);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -93,228 +153,243 @@ export async function updateContact(id: string, updates: ContactMutation) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteContact(id: string) {
|
export async function deleteContact(id: string) {
|
||||||
fakeContacts.destroy(id);
|
await fakeContacts.destroy(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
[
|
// New function to reset the database to initial state
|
||||||
{
|
export async function resetDatabase() {
|
||||||
avatar:
|
console.log("RESET DATABASE");
|
||||||
"https://sessionize.com/image/124e-400o400o2-wHVdAuNaxi8KJrgtN3ZKci.jpg",
|
await fakeContacts.reset();
|
||||||
first: "Shruti",
|
}
|
||||||
last: "Kapoor",
|
|
||||||
twitter: "@shrutikapoor08",
|
// Helper function to get initial contacts data
|
||||||
},
|
function getInitialContacts() {
|
||||||
{
|
return [
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/1940-400o400o2-Enh9dnYmrLYhJSTTPSw3MH.jpg",
|
avatar:
|
||||||
first: "Glenn",
|
"https://sessionize.com/image/124e-400o400o2-wHVdAuNaxi8KJrgtN3ZKci.jpg",
|
||||||
last: "Reyes",
|
first: "Shruti",
|
||||||
twitter: "@glnnrys",
|
last: "Kapoor",
|
||||||
},
|
twitter: "@shrutikapoor08",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/9273-400o400o2-3tyrUE3HjsCHJLU5aUJCja.jpg",
|
avatar:
|
||||||
first: "Ryan",
|
"https://sessionize.com/image/1940-400o400o2-Enh9dnYmrLYhJSTTPSw3MH.jpg",
|
||||||
last: "Florence",
|
first: "Glenn",
|
||||||
},
|
last: "Reyes",
|
||||||
{
|
twitter: "@glnnrys",
|
||||||
avatar:
|
},
|
||||||
"https://sessionize.com/image/d14d-400o400o2-pyB229HyFPCnUcZhHf3kWS.png",
|
{
|
||||||
first: "Oscar",
|
avatar:
|
||||||
last: "Newman",
|
"https://sessionize.com/image/9273-400o400o2-3tyrUE3HjsCHJLU5aUJCja.jpg",
|
||||||
twitter: "@__oscarnewman",
|
first: "Ryan",
|
||||||
},
|
last: "Florence",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/fd45-400o400o2-fw91uCdGU9hFP334dnyVCr.jpg",
|
avatar:
|
||||||
first: "Michael",
|
"https://sessionize.com/image/d14d-400o400o2-pyB229HyFPCnUcZhHf3kWS.png",
|
||||||
last: "Jackson",
|
first: "Oscar",
|
||||||
},
|
last: "Newman",
|
||||||
{
|
twitter: "@__oscarnewman",
|
||||||
avatar:
|
},
|
||||||
"https://sessionize.com/image/b07e-400o400o2-KgNRF3S9sD5ZR4UsG7hG4g.jpg",
|
{
|
||||||
first: "Christopher",
|
avatar:
|
||||||
last: "Chedeau",
|
"https://sessionize.com/image/fd45-400o400o2-fw91uCdGU9hFP334dnyVCr.jpg",
|
||||||
twitter: "@Vjeux",
|
first: "Michael",
|
||||||
},
|
last: "Jackson",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/262f-400o400o2-UBPQueK3fayaCmsyUc1Ljf.jpg",
|
avatar:
|
||||||
first: "Cameron",
|
"https://sessionize.com/image/b07e-400o400o2-KgNRF3S9sD5ZR4UsG7hG4g.jpg",
|
||||||
last: "Matheson",
|
first: "Christopher",
|
||||||
twitter: "@cmatheson",
|
last: "Chedeau",
|
||||||
},
|
twitter: "@Vjeux",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/820b-400o400o2-Ja1KDrBAu5NzYTPLSC3GW8.jpg",
|
avatar:
|
||||||
first: "Brooks",
|
"https://sessionize.com/image/262f-400o400o2-UBPQueK3fayaCmsyUc1Ljf.jpg",
|
||||||
last: "Lybrand",
|
first: "Cameron",
|
||||||
twitter: "@BrooksLybrand",
|
last: "Matheson",
|
||||||
},
|
twitter: "@cmatheson",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/df38-400o400o2-JwbChVUj6V7DwZMc9vJEHc.jpg",
|
avatar:
|
||||||
first: "Alex",
|
"https://sessionize.com/image/820b-400o400o2-Ja1KDrBAu5NzYTPLSC3GW8.jpg",
|
||||||
last: "Anderson",
|
first: "Brooks",
|
||||||
twitter: "@ralex1993",
|
last: "Lybrand",
|
||||||
},
|
twitter: "@BrooksLybrand",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/5578-400o400o2-BMT43t5kd2U1XstaNnM6Ax.jpg",
|
avatar:
|
||||||
first: "Kent C.",
|
"https://sessionize.com/image/df38-400o400o2-JwbChVUj6V7DwZMc9vJEHc.jpg",
|
||||||
last: "Dodds",
|
first: "Alex",
|
||||||
twitter: "@kentcdodds",
|
last: "Anderson",
|
||||||
},
|
twitter: "@ralex1993",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/c9d5-400o400o2-Sri5qnQmscaJXVB8m3VBgf.jpg",
|
avatar:
|
||||||
first: "Nevi",
|
"https://sessionize.com/image/5578-400o400o2-BMT43t5kd2U1XstaNnM6Ax.jpg",
|
||||||
last: "Shah",
|
first: "Kent C.",
|
||||||
twitter: "@nevikashah",
|
last: "Dodds",
|
||||||
},
|
twitter: "@kentcdodds",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/2694-400o400o2-MYYTsnszbLKTzyqJV17w2q.png",
|
avatar:
|
||||||
first: "Andrew",
|
"https://sessionize.com/image/c9d5-400o400o2-Sri5qnQmscaJXVB8m3VBgf.jpg",
|
||||||
last: "Petersen",
|
first: "Nevi",
|
||||||
},
|
last: "Shah",
|
||||||
{
|
twitter: "@nevikashah",
|
||||||
avatar:
|
},
|
||||||
"https://sessionize.com/image/907a-400o400o2-9TM2CCmvrw6ttmJiTw4Lz8.jpg",
|
{
|
||||||
first: "Scott",
|
avatar:
|
||||||
last: "Smerchek",
|
"https://sessionize.com/image/2694-400o400o2-MYYTsnszbLKTzyqJV17w2q.png",
|
||||||
twitter: "@smerchek",
|
first: "Andrew",
|
||||||
},
|
last: "Petersen",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/08be-400o400o2-WtYGFFR1ZUJHL9tKyVBNPV.jpg",
|
avatar:
|
||||||
first: "Giovanni",
|
"https://sessionize.com/image/907a-400o400o2-9TM2CCmvrw6ttmJiTw4Lz8.jpg",
|
||||||
last: "Benussi",
|
first: "Scott",
|
||||||
twitter: "@giovannibenussi",
|
last: "Smerchek",
|
||||||
},
|
twitter: "@smerchek",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/f814-400o400o2-n2ua5nM9qwZA2hiGdr1T7N.jpg",
|
avatar:
|
||||||
first: "Igor",
|
"https://sessionize.com/image/08be-400o400o2-WtYGFFR1ZUJHL9tKyVBNPV.jpg",
|
||||||
last: "Minar",
|
first: "Giovanni",
|
||||||
twitter: "@IgorMinar",
|
last: "Benussi",
|
||||||
},
|
twitter: "@giovannibenussi",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/fb82-400o400o2-LbvwhTVMrYLDdN3z4iEFMp.jpeg",
|
avatar:
|
||||||
first: "Brandon",
|
"https://sessionize.com/image/f814-400o400o2-n2ua5nM9qwZA2hiGdr1T7N.jpg",
|
||||||
last: "Kish",
|
first: "Igor",
|
||||||
},
|
last: "Minar",
|
||||||
{
|
twitter: "@IgorMinar",
|
||||||
avatar:
|
},
|
||||||
"https://sessionize.com/image/fcda-400o400o2-XiYRtKK5Dvng5AeyC8PiUA.png",
|
{
|
||||||
first: "Arisa",
|
avatar:
|
||||||
last: "Fukuzaki",
|
"https://sessionize.com/image/fb82-400o400o2-LbvwhTVMrYLDdN3z4iEFMp.jpeg",
|
||||||
twitter: "@arisa_dev",
|
first: "Brandon",
|
||||||
},
|
last: "Kish",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/c8c3-400o400o2-PR5UsgApAVEADZRixV4H8e.jpeg",
|
avatar:
|
||||||
first: "Alexandra",
|
"https://sessionize.com/image/fcda-400o400o2-XiYRtKK5Dvng5AeyC8PiUA.png",
|
||||||
last: "Spalato",
|
first: "Arisa",
|
||||||
twitter: "@alexadark",
|
last: "Fukuzaki",
|
||||||
},
|
twitter: "@arisa_dev",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/7594-400o400o2-hWtdCjbdFdLgE2vEXBJtyo.jpg",
|
avatar:
|
||||||
first: "Cat",
|
"https://sessionize.com/image/c8c3-400o400o2-PR5UsgApAVEADZRixV4H8e.jpeg",
|
||||||
last: "Johnson",
|
first: "Alexandra",
|
||||||
},
|
last: "Spalato",
|
||||||
{
|
twitter: "@alexadark",
|
||||||
avatar:
|
},
|
||||||
"https://sessionize.com/image/5636-400o400o2-TWgi8vELMFoB3hB9uPw62d.jpg",
|
{
|
||||||
first: "Ashley",
|
avatar:
|
||||||
last: "Narcisse",
|
"https://sessionize.com/image/7594-400o400o2-hWtdCjbdFdLgE2vEXBJtyo.jpg",
|
||||||
twitter: "@_darkfadr",
|
first: "Cat",
|
||||||
},
|
last: "Johnson",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/6aeb-400o400o2-Q5tAiuzKGgzSje9ZsK3Yu5.JPG",
|
avatar:
|
||||||
first: "Edmund",
|
"https://sessionize.com/image/5636-400o400o2-TWgi8vELMFoB3hB9uPw62d.jpg",
|
||||||
last: "Hung",
|
first: "Ashley",
|
||||||
twitter: "@_edmundhung",
|
last: "Narcisse",
|
||||||
},
|
twitter: "@_darkfadr",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/30f1-400o400o2-wJBdJ6sFayjKmJycYKoHSe.jpg",
|
avatar:
|
||||||
first: "Clifford",
|
"https://sessionize.com/image/6aeb-400o400o2-Q5tAiuzKGgzSje9ZsK3Yu5.JPG",
|
||||||
last: "Fajardo",
|
first: "Edmund",
|
||||||
twitter: "@cliffordfajard0",
|
last: "Hung",
|
||||||
},
|
twitter: "@_edmundhung",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/6faa-400o400o2-amseBRDkdg7wSK5tjsFDiG.jpg",
|
avatar:
|
||||||
first: "Erick",
|
"https://sessionize.com/image/30f1-400o400o2-wJBdJ6sFayjKmJycYKoHSe.jpg",
|
||||||
last: "Tamayo",
|
first: "Clifford",
|
||||||
twitter: "@ericktamayo",
|
last: "Fajardo",
|
||||||
},
|
twitter: "@cliffordfajard0",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/feba-400o400o2-R4GE7eqegJNFf3cQ567obs.jpg",
|
avatar:
|
||||||
first: "Paul",
|
"https://sessionize.com/image/6faa-400o400o2-amseBRDkdg7wSK5tjsFDiG.jpg",
|
||||||
last: "Bratslavsky",
|
first: "Erick",
|
||||||
twitter: "@codingthirty",
|
last: "Tamayo",
|
||||||
},
|
twitter: "@ericktamayo",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/c315-400o400o2-spjM5A6VVfVNnQsuwvX3DY.jpg",
|
avatar:
|
||||||
first: "Pedro",
|
"https://sessionize.com/image/feba-400o400o2-R4GE7eqegJNFf3cQ567obs.jpg",
|
||||||
last: "Cattori",
|
first: "Paul",
|
||||||
twitter: "@pcattori",
|
last: "Bratslavsky",
|
||||||
},
|
twitter: "@codingthirty",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/eec1-400o400o2-HkvWKLFqecmFxLwqR9KMRw.jpg",
|
avatar:
|
||||||
first: "Andre",
|
"https://sessionize.com/image/c315-400o400o2-spjM5A6VVfVNnQsuwvX3DY.jpg",
|
||||||
last: "Landgraf",
|
first: "Pedro",
|
||||||
twitter: "@AndreLandgraf94",
|
last: "Cattori",
|
||||||
},
|
twitter: "@pcattori",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/c73a-400o400o2-4MTaTq6ftC15hqwtqUJmTC.jpg",
|
avatar:
|
||||||
first: "Monica",
|
"https://sessionize.com/image/eec1-400o400o2-HkvWKLFqecmFxLwqR9KMRw.jpg",
|
||||||
last: "Powell",
|
first: "Andre",
|
||||||
twitter: "@indigitalcolor",
|
last: "Landgraf",
|
||||||
},
|
twitter: "@AndreLandgraf94",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/cef7-400o400o2-KBZUydbjfkfGACQmjbHEvX.jpeg",
|
avatar:
|
||||||
first: "Brian",
|
"https://sessionize.com/image/c73a-400o400o2-4MTaTq6ftC15hqwtqUJmTC.jpg",
|
||||||
last: "Lee",
|
first: "Monica",
|
||||||
twitter: "@brian_dlee",
|
last: "Powell",
|
||||||
},
|
twitter: "@indigitalcolor",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/f83b-400o400o2-Pyw3chmeHMxGsNoj3nQmWU.jpg",
|
avatar:
|
||||||
first: "Sean",
|
"https://sessionize.com/image/cef7-400o400o2-KBZUydbjfkfGACQmjbHEvX.jpeg",
|
||||||
last: "McQuaid",
|
first: "Brian",
|
||||||
twitter: "@SeanMcQuaidCode",
|
last: "Lee",
|
||||||
},
|
twitter: "@brian_dlee",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/a9fc-400o400o2-JHBnWZRoxp7QX74Hdac7AZ.jpg",
|
avatar:
|
||||||
first: "Shane",
|
"https://sessionize.com/image/f83b-400o400o2-Pyw3chmeHMxGsNoj3nQmWU.jpg",
|
||||||
last: "Walker",
|
first: "Sean",
|
||||||
twitter: "@swalker326",
|
last: "McQuaid",
|
||||||
},
|
twitter: "@SeanMcQuaidCode",
|
||||||
{
|
},
|
||||||
avatar:
|
{
|
||||||
"https://sessionize.com/image/6644-400o400o2-aHnGHb5Pdu3D32MbfrnQbj.jpg",
|
avatar:
|
||||||
first: "Jon",
|
"https://sessionize.com/image/a9fc-400o400o2-JHBnWZRoxp7QX74Hdac7AZ.jpg",
|
||||||
last: "Jensen",
|
first: "Shane",
|
||||||
twitter: "@jenseng",
|
last: "Walker",
|
||||||
},
|
twitter: "@swalker326",
|
||||||
].forEach((contact) => {
|
},
|
||||||
fakeContacts.create({
|
{
|
||||||
|
avatar:
|
||||||
|
"https://sessionize.com/image/6644-400o400o2-aHnGHb5Pdu3D32MbfrnQbj.jpg",
|
||||||
|
first: "Jon",
|
||||||
|
last: "Jensen",
|
||||||
|
twitter: "@jenseng",
|
||||||
|
},
|
||||||
|
].map(contact => ({
|
||||||
...contact,
|
...contact,
|
||||||
id: `${contact.first
|
id: `${contact.first
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.split(" ")
|
.split(" ")
|
||||||
.join("_")}-${contact.last.toLocaleLowerCase()}`,
|
.join("_")}-${contact.last.toLowerCase()}`,
|
||||||
});
|
}));
|
||||||
});
|
}
|
||||||
|
|
||||||
|
// Initialize with seed data on first load
|
||||||
|
(async () => {
|
||||||
|
const db = await getDb();
|
||||||
|
if (Object.keys(db.data.records).length === 0) {
|
||||||
|
await resetDatabase();
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
import type { Metadata } from "next";
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
import type { Metadata } from "next";
|
||||||
|
|
||||||
import "./globals.css";
|
import "./globals.css";
|
||||||
import Form from "next/form";
|
|
||||||
import Link from "next/link";
|
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
title: "Next.js Address Book",
|
title: "Next.js Address Book",
|
||||||
@@ -18,41 +16,7 @@ export default function RootLayout({
|
|||||||
return (
|
return (
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<body>
|
<body>
|
||||||
<div id="sidebar">
|
{children}
|
||||||
<h1>React Router Contacts</h1>
|
|
||||||
<div>
|
|
||||||
<Form id="search-form" role="search" action="." formMethod="get">
|
|
||||||
<input
|
|
||||||
aria-label="Search contacts"
|
|
||||||
id="q"
|
|
||||||
name="q"
|
|
||||||
placeholder="Search"
|
|
||||||
type="search"
|
|
||||||
/>
|
|
||||||
<div
|
|
||||||
aria-hidden
|
|
||||||
hidden={true}
|
|
||||||
id="search-spinner"
|
|
||||||
/>
|
|
||||||
</Form>
|
|
||||||
<Form action="." formMethod="post">
|
|
||||||
<button type="submit">New</button>
|
|
||||||
</Form>
|
|
||||||
</div>
|
|
||||||
<nav>
|
|
||||||
<ul>
|
|
||||||
<li>
|
|
||||||
<Link href={`/contacts/1`}>Your Name</Link>
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<Link href={`/contacts/2`}>Your Friend</Link>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
<div id={'detail'}>
|
|
||||||
{children}
|
|
||||||
</div>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
import Form from "next/form";
|
|
||||||
import React from "react";
|
|
||||||
|
|
||||||
export default function Home() {
|
|
||||||
return (
|
|
||||||
<div></div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user