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

99 lines
2.1 KiB
JavaScript

import { useEffect, useRef } from "react";
import Router from "next/router";
import { useUser } from "../lib/hooks";
function ProfileEdit() {
const [user, { mutate }] = useUser();
const nameRef = useRef();
useEffect(() => {
if (!user) return;
nameRef.current.value = user.name;
}, [user]);
async function handleEditProfile(e) {
e.preventDefault();
const body = {
name: nameRef.current.value,
};
const res = await fetch(`/api/user`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
const updatedUser = await res.json();
mutate(updatedUser);
}
async function handleDeleteProfile() {
const res = await fetch(`/api/user`, {
method: "DELETE",
});
if (res.status === 204) {
mutate({ user: null });
Router.replace("/");
}
}
return (
<>
<div className="form-container">
<form onSubmit={handleEditProfile}>
<label>
<span>Name</span>
<input type="text" ref={nameRef} required />
</label>
<div className="submit">
<button type="submit">Update profile</button>
<a role="button" className="delete" onClick={handleDeleteProfile}>
Delete profile
</a>
</div>
</form>
</div>
<style jsx>{`
.delete {
color: #f44336;
cursor: pointer;
}
.delete:hover {
color: #b71c1c;
}
`}</style>
</>
);
}
export default function ProfilePage() {
const [user, { loading }] = useUser();
useEffect(() => {
// redirect user to login if not authenticated
if (!loading && !user) Router.replace("/login");
}, [user, loading]);
return (
<>
<h1>Profile</h1>
{user && (
<>
<p>Your session:</p>
<pre>{JSON.stringify(user, null, 2)}</pre>
<ProfileEdit />
</>
)}
<style jsx>{`
pre {
white-space: pre-wrap;
word-wrap: break-word;
}
`}</style>
</>
);
}