Files
StevenandGitHub 4466ba436b chore(examples): use default prettier for examples/templates (#60530)
## Description
This PR ensures that the default prettier config is used for examples
and templates.

This config is compatible with `prettier@3` as well (upgrading prettier
is bigger change that can be a future PR).

## Changes
- Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will
be needed upgrading to prettier@3)
- Added `examples/.prettierrc.json` with default config (this will
change every example)
- Added `packages/create-next-app/templates/.prettierrc.json` with
default config (this will change every template)

## Related

- Fixes #54402
- Closes #54409
2024-01-11 16:01:44 -07:00

75 lines
2.0 KiB
JavaScript

import { useState, useEffect } from "react";
import Router from "next/router";
import Link from "next/link";
import { useUser } from "../lib/hooks";
export default function SignupPage() {
const [user, { mutate }] = useUser();
const [errorMsg, setErrorMsg] = useState("");
async function onSubmit(e) {
e.preventDefault();
const body = {
username: e.currentTarget.username.value,
password: e.currentTarget.password.value,
name: e.currentTarget.name.value,
};
if (body.password !== e.currentTarget.rpassword.value) {
setErrorMsg(`The passwords don't match`);
return;
}
const res = await fetch("/api/users", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (res.status === 201) {
const userObj = await res.json();
// set user to useSWR state
mutate(userObj);
} else {
setErrorMsg(await res.text());
}
}
useEffect(() => {
// redirect to home if user is authenticated
if (user) Router.push("/");
}, [user]);
return (
<>
<h1>Sign up to Example</h1>
{errorMsg && <p className="error">{errorMsg}</p>}
<div className="form-container">
<form onSubmit={onSubmit}>
<label>
<span>Username</span>
<input type="text" name="username" required />
</label>
<label>
<span>Password</span>
<input type="password" name="password" required />
</label>
<label>
<span>Repeat password</span>
<input type="password" name="rpassword" required />
</label>
<label>
<span>Name</span>
<input type="text" name="name" required />
</label>
<div className="submit">
<button type="submit">Sign up</button>
<Link href="/login">I already have an account</Link>
</div>
</form>
</div>
</>
);
}