-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.tsx
154 lines (144 loc) · 4.04 KB
/
root.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import type { LinksFunction, LoaderFunctionArgs } from "@remix-run/node";
import { json, redirect } from "@remix-run/node";
import {
Form,
Links,
LiveReload,
Meta,
NavLink,
Outlet,
Scripts,
ScrollRestoration,
useLoaderData,
useNavigation,
useSubmit,
} from "@remix-run/react";
import appStylesHref from "./app.css";
import { createEmptyContact, getContacts } from "./data";
import { useEffect, useState } from "react";
export const action = async () => {
const contact = await createEmptyContact();
return redirect(`/contacts/${contact.id}/edit`);
};
export const links: LinksFunction = () => [
{ rel: 'stylesheet', href: appStylesHref},
];
export const loader = async ({
request,
} : LoaderFunctionArgs) => {
const url = new URL(request.url);
const q = url.searchParams.get("q");
const contacts = await getContacts(q);
return json({ contacts, q });
};
export default function App() {
const { contacts, q } = useLoaderData<typeof loader>();
const navigation = useNavigation();
const submit = useSubmit();
const searching =
navigation.location &&
new URLSearchParams(navigation.location.search).has(
"q"
);
const [query, setQuery] = useState(q || "");
useEffect(() => {
setQuery(q || "");
}, [q])
// useEffect(() => {
// const searchField = document.getElementById("q");
// if (searchField instanceof HTMLInputElement) {
// searchField.value = q || "";
// }
// }, [q]);
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
</head>
<body>
<div id="sidebar">
<h1>Remix Contacts</h1>
<div>
<Form
id="search-form"
role="search"
onChange={(event) => {
const isFirstSearch = q === null;
submit(event.currentTarget,{
replace: !isFirstSearch,
});
}}
>
<input
id="q"
aria-label="Search contacts"
className={searching ? 'loading' : ""}
defaultValue={q || ""}
placeholder="Search"
type="search"
name="q"
onChange={(event) => {
setQuery(event.currentTarget.value)
}}
value={query}
/>
<div id="search-spinner" aria-hidden hidden={!searching} />
</Form>
<Form method="post">
<button type="submit">New</button>
</Form>
</div>
<nav>
{contacts.length ? (
<ul>
{contacts.map((contact) => (
<li key={contact.id}>
<NavLink
className={({ isActive, isPending }) =>
isActive
? "active"
: isPending
? "pending"
: ""
}
to={`contacts/${contact.id}`}
>
{contact.first || contact.last ? (
<>
{contact.first} {contact.last}
</>
) : (
<i>No Name</i>
)}{" "}
{contact.favorite ? (
<span>★</span>
) : null}
</NavLink>
</li>
))}
</ul>
) : (
<p>
<i>No contacts</i>
</p>
)}
</nav>
</div>
<div
className={
navigation.state === "loading" && !searching ? "loading" : ""
}
id="detail"
>
<Outlet />
</div>
<ScrollRestoration />
<Scripts />
<LiveReload />
</body>
</html>
);
}