2023-08-07 10:52:44 +02:00
|
|
|
import type { ActionArgs } from "@remix-run/node";
|
|
|
|
|
import { json, redirect } from "@remix-run/node";
|
|
|
|
|
import { Form, useActionData } from "@remix-run/react";
|
|
|
|
|
import { useEffect, useRef } from "react";
|
|
|
|
|
|
2023-08-07 11:21:43 +02:00
|
|
|
import { createTranslation } from "~/models/translation.server";
|
2023-08-07 10:52:44 +02:00
|
|
|
import { requireUserId } from "~/session.server";
|
|
|
|
|
|
|
|
|
|
export const action = async ({ request }: ActionArgs) => {
|
|
|
|
|
const userId = await requireUserId(request);
|
|
|
|
|
|
|
|
|
|
const formData = await request.formData();
|
2023-08-07 11:21:43 +02:00
|
|
|
const lang = formData.get("lang");
|
|
|
|
|
const text = formData.get("text");
|
2023-08-07 10:52:44 +02:00
|
|
|
|
2023-08-07 11:21:43 +02:00
|
|
|
if (typeof lang !== "string" || lang.length === 0) {
|
2023-08-07 10:52:44 +02:00
|
|
|
return json(
|
2023-08-07 11:21:43 +02:00
|
|
|
{ errors: { text: null, lang: "Lang is required" } },
|
2023-08-07 10:52:44 +02:00
|
|
|
{ status: 400 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 11:21:43 +02:00
|
|
|
if (typeof text !== "string" || text.length === 0) {
|
2023-08-07 10:52:44 +02:00
|
|
|
return json(
|
2023-08-07 11:21:43 +02:00
|
|
|
{ errors: { text: "Text is required", lang: null } },
|
2023-08-07 10:52:44 +02:00
|
|
|
{ status: 400 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 11:21:43 +02:00
|
|
|
const result = "test";
|
2023-08-07 10:52:44 +02:00
|
|
|
|
2023-08-07 11:21:43 +02:00
|
|
|
const t = await createTranslation({ lang, text, result, userId });
|
|
|
|
|
|
|
|
|
|
return redirect(`/t/${t.id}`);
|
2023-08-07 10:52:44 +02:00
|
|
|
};
|
|
|
|
|
|
2023-08-07 11:21:43 +02:00
|
|
|
export default function NewTranslationPage() {
|
2023-08-07 10:52:44 +02:00
|
|
|
const actionData = useActionData<typeof action>();
|
2023-08-07 11:21:43 +02:00
|
|
|
const langRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
const textRef = useRef<HTMLTextAreaElement>(null);
|
2023-08-07 10:52:44 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-08-07 11:21:43 +02:00
|
|
|
if (actionData?.errors?.lang) {
|
|
|
|
|
langRef.current?.focus();
|
|
|
|
|
} else if (actionData?.errors?.text) {
|
|
|
|
|
textRef.current?.focus();
|
2023-08-07 10:52:44 +02:00
|
|
|
}
|
|
|
|
|
}, [actionData]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Form
|
|
|
|
|
method="post"
|
|
|
|
|
style={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
gap: 8,
|
|
|
|
|
width: "100%",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<div>
|
|
|
|
|
<label className="flex w-full flex-col gap-1">
|
2023-08-07 11:21:43 +02:00
|
|
|
<span>Translate to: </span>
|
2023-08-07 10:52:44 +02:00
|
|
|
<input
|
2023-08-07 11:21:43 +02:00
|
|
|
ref={langRef}
|
|
|
|
|
name="lang"
|
2023-08-07 10:52:44 +02:00
|
|
|
className="flex-1 rounded-md border-2 border-blue-500 px-3 text-lg leading-loose"
|
2023-08-07 11:21:43 +02:00
|
|
|
aria-invalid={actionData?.errors?.lang ? true : undefined}
|
2023-08-07 10:52:44 +02:00
|
|
|
aria-errormessage={
|
2023-08-07 11:21:43 +02:00
|
|
|
actionData?.errors?.lang ? "lang-error" : undefined
|
2023-08-07 10:52:44 +02:00
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
2023-08-07 11:21:43 +02:00
|
|
|
{actionData?.errors?.lang ? (
|
|
|
|
|
<div className="pt-1 text-red-700" id="lang-error">
|
|
|
|
|
{actionData.errors.lang}
|
2023-08-07 10:52:44 +02:00
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<label className="flex w-full flex-col gap-1">
|
2023-08-07 11:21:43 +02:00
|
|
|
<span>Text: </span>
|
2023-08-07 10:52:44 +02:00
|
|
|
<textarea
|
2023-08-07 11:21:43 +02:00
|
|
|
ref={textRef}
|
|
|
|
|
name="text"
|
2023-08-07 10:52:44 +02:00
|
|
|
rows={8}
|
|
|
|
|
className="w-full flex-1 rounded-md border-2 border-blue-500 px-3 py-2 text-lg leading-6"
|
2023-08-07 11:21:43 +02:00
|
|
|
aria-invalid={actionData?.errors?.text ? true : undefined}
|
2023-08-07 10:52:44 +02:00
|
|
|
aria-errormessage={
|
2023-08-07 11:21:43 +02:00
|
|
|
actionData?.errors?.text ? "text-error" : undefined
|
2023-08-07 10:52:44 +02:00
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
2023-08-07 11:21:43 +02:00
|
|
|
{actionData?.errors?.text ? (
|
|
|
|
|
<div className="pt-1 text-red-700" id="text-error">
|
|
|
|
|
{actionData.errors.text}
|
2023-08-07 10:52:44 +02:00
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="text-right">
|
|
|
|
|
<button
|
|
|
|
|
type="submit"
|
|
|
|
|
className="rounded bg-blue-500 px-4 py-2 text-white hover:bg-blue-600 focus:bg-blue-400"
|
|
|
|
|
>
|
2023-08-07 11:21:43 +02:00
|
|
|
Translate
|
2023-08-07 10:52:44 +02:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</Form>
|
|
|
|
|
);
|
|
|
|
|
}
|