v3: code quality pass
This commit is contained in:
@@ -3,7 +3,12 @@ import type { SubmitEvent } from "react";
|
||||
import { Link, useNavigate, useSearchParams } from "react-router";
|
||||
|
||||
import { API_URL, VALIDATION } from "../config/api.ts";
|
||||
import { deserializeAuthResponse } from "../model.ts";
|
||||
import {
|
||||
deserializeAuthResponse,
|
||||
parseAPIResponse,
|
||||
type RawAuthResponse,
|
||||
type RegisterRequest,
|
||||
} from "../model.ts";
|
||||
import { useAuth } from "../hooks/useAuth.ts";
|
||||
import { PageShell } from "../components/PageShell.tsx";
|
||||
import { ErrorCard } from "../components/ErrorCard.tsx";
|
||||
@@ -47,26 +52,25 @@ export function UserRegister() {
|
||||
setFormState({ status: "submitting" });
|
||||
|
||||
const formData = new FormData(e.currentTarget);
|
||||
const username = formData.get("username");
|
||||
const password = formData.get("password");
|
||||
const username = formData.get("username") as string;
|
||||
const password = formData.get("password") as string;
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/api/users/register`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ username, password, inviteToken: token }),
|
||||
body: JSON.stringify(
|
||||
{ username, password, inviteToken: token } satisfies RegisterRequest,
|
||||
),
|
||||
});
|
||||
|
||||
const apiResponse = await res.json();
|
||||
const apiResponse = parseAPIResponse<RawAuthResponse>(await res.json());
|
||||
|
||||
if (apiResponse.success) {
|
||||
login(deserializeAuthResponse(apiResponse.data));
|
||||
navigate("/");
|
||||
} else {
|
||||
setFormState({
|
||||
status: "error",
|
||||
error: apiResponse.error?.message ?? "Registration failed.",
|
||||
});
|
||||
setFormState({ status: "error", error: apiResponse.error.message });
|
||||
}
|
||||
} catch (err) {
|
||||
setFormState({ status: "error", error: friendlyFetchError(err) });
|
||||
|
||||
Reference in New Issue
Block a user