-
Notifications
You must be signed in to change notification settings - Fork 237
/
_index.tsx
51 lines (43 loc) · 1.26 KB
/
_index.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
import type { ActionArgs } from "@remix-run/node";
import { json, redirect } from "@remix-run/node";
import { Form, useActionData, useLoaderData } from "@remix-run/react";
import { redis } from "~/utils/redis.server";
export const loader = async () => {
const message = await redis.get("message");
return json({ message });
};
export const action = async ({ request }: ActionArgs) => {
const formData = await request.formData();
const message = formData.get("message");
if (!message || typeof message !== "string") {
return json("String only!", { status: 400 });
}
await redis.set("message", message);
return redirect("/");
};
export default function Index() {
const data = useLoaderData<typeof loader>();
const actionMessage = useActionData<typeof action>();
return (
<main>
<div>
<b>Stored Message:</b> {data.message}
</div>
<Form method="post">
<h2>Change the message</h2>
<label>
<div>Message</div>
<input name="message" defaultValue={data.message ?? ""} />
</label>
<div>
<button type="submit">Save</button>
</div>
</Form>
{actionMessage ? (
<p>
<b>{actionMessage}</b>
</p>
) : null}
</main>
);
}