180 lines
5.9 KiB
TypeScript
180 lines
5.9 KiB
TypeScript
import { useState } from 'react';
|
|
import { useSuspenseQuery } from '@tanstack/react-query';
|
|
import { createFileRoute } from '@tanstack/react-router';
|
|
import {
|
|
BIO_MAX_LENGTH,
|
|
NAME_MAX_LENGTH,
|
|
USERNAME_MAX_LENGTH,
|
|
USERNAME_MIN_LENGTH,
|
|
updateProfileSchema,
|
|
} from '@repo/contracts';
|
|
import { Alert, AlertDescription } from '@repo/ui/components/alert';
|
|
import { Button } from '@repo/ui/components/button';
|
|
import {
|
|
Field,
|
|
FieldDescription,
|
|
FieldError,
|
|
FieldGroup,
|
|
FieldLabel,
|
|
} from '@repo/ui/components/field';
|
|
import { Input } from '@repo/ui/components/input';
|
|
import { Separator } from '@repo/ui/components/separator';
|
|
import { Spinner } from '@repo/ui/components/spinner';
|
|
import { Textarea } from '@repo/ui/components/textarea';
|
|
import { toast } from '@repo/ui/components/toast';
|
|
import { NotificationSettings } from '@/components/notification-settings';
|
|
import { ApiError } from '@/lib/api';
|
|
import { ownProfileQuery, useUpdateProfile } from '@/lib/profile';
|
|
|
|
export const Route = createFileRoute('/app/settings')({
|
|
component: ProfileSettings,
|
|
});
|
|
|
|
type FieldName = 'name' | 'username' | 'bio';
|
|
type FieldErrors = Partial<Record<FieldName, string>>;
|
|
|
|
function ProfileSettings() {
|
|
const { data: profile } = useSuspenseQuery(ownProfileQuery);
|
|
const updateProfile = useUpdateProfile();
|
|
const [fieldErrors, setFieldErrors] = useState<FieldErrors>({});
|
|
const [formError, setFormError] = useState<string | null>(null);
|
|
|
|
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
event.preventDefault();
|
|
const form = new FormData(event.currentTarget);
|
|
const parsed = updateProfileSchema.safeParse({
|
|
name: form.get('name'),
|
|
username: form.get('username'),
|
|
bio: form.get('bio'),
|
|
});
|
|
|
|
setFormError(null);
|
|
if (!parsed.success) {
|
|
const errors: FieldErrors = {};
|
|
for (const issue of parsed.error.issues) {
|
|
const field = issue.path[0] as FieldName;
|
|
errors[field] ??= issue.message;
|
|
}
|
|
setFieldErrors(errors);
|
|
return;
|
|
}
|
|
|
|
setFieldErrors({});
|
|
updateProfile.mutate(parsed.data, {
|
|
onSuccess: () => {
|
|
toast.add({ title: 'Profile saved', type: 'success' });
|
|
},
|
|
onError: (error) => {
|
|
// 409 is the API's answer for a username someone else has.
|
|
if (error instanceof ApiError && error.status === 409) {
|
|
setFieldErrors({ username: error.message });
|
|
} else if (error instanceof ApiError) {
|
|
setFormError(error.message);
|
|
} else {
|
|
setFormError('Could not save your profile. Please try again.');
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
return (
|
|
<div className="mx-auto flex w-full max-w-xl flex-col gap-6 p-6 md:py-10">
|
|
<header className="flex flex-col gap-1">
|
|
<h1 className="font-heading text-2xl font-semibold">Profile</h1>
|
|
<p className="text-sm text-muted-foreground">
|
|
How you appear to other people.
|
|
</p>
|
|
</header>
|
|
|
|
{profile.username === null && (
|
|
<Alert>
|
|
<AlertDescription>
|
|
Choose a username to finish setting up your account.
|
|
</AlertDescription>
|
|
</Alert>
|
|
)}
|
|
|
|
{/* Remounts with the saved values, e.g. a username the API lower-cased. */}
|
|
<form
|
|
key={`${profile.name}|${profile.username}|${profile.bio}`}
|
|
onSubmit={handleSubmit}
|
|
noValidate
|
|
>
|
|
<FieldGroup>
|
|
<Field data-invalid={fieldErrors.name ? true : undefined}>
|
|
<FieldLabel htmlFor="name">Name</FieldLabel>
|
|
<Input
|
|
id="name"
|
|
name="name"
|
|
autoComplete="name"
|
|
defaultValue={profile.name}
|
|
maxLength={NAME_MAX_LENGTH}
|
|
aria-invalid={fieldErrors.name ? true : undefined}
|
|
/>
|
|
<FieldError>{fieldErrors.name}</FieldError>
|
|
</Field>
|
|
|
|
<Field data-invalid={fieldErrors.username ? true : undefined}>
|
|
<FieldLabel htmlFor="username">Username</FieldLabel>
|
|
<Input
|
|
id="username"
|
|
name="username"
|
|
autoComplete="username"
|
|
autoCapitalize="none"
|
|
spellCheck={false}
|
|
defaultValue={profile.username ?? ''}
|
|
minLength={USERNAME_MIN_LENGTH}
|
|
maxLength={USERNAME_MAX_LENGTH}
|
|
aria-invalid={fieldErrors.username ? true : undefined}
|
|
/>
|
|
<FieldDescription>
|
|
How friends find you. Letters, numbers, underscores and dots.
|
|
</FieldDescription>
|
|
<FieldError>{fieldErrors.username}</FieldError>
|
|
</Field>
|
|
|
|
<Field data-invalid={fieldErrors.bio ? true : undefined}>
|
|
<FieldLabel htmlFor="bio">Bio</FieldLabel>
|
|
<Textarea
|
|
id="bio"
|
|
name="bio"
|
|
rows={3}
|
|
defaultValue={profile.bio ?? ''}
|
|
maxLength={BIO_MAX_LENGTH}
|
|
aria-invalid={fieldErrors.bio ? true : undefined}
|
|
/>
|
|
<FieldDescription>
|
|
Up to {BIO_MAX_LENGTH} characters.
|
|
</FieldDescription>
|
|
<FieldError>{fieldErrors.bio}</FieldError>
|
|
</Field>
|
|
|
|
<Field>
|
|
<FieldLabel htmlFor="email">Email</FieldLabel>
|
|
<Input id="email" type="email" value={profile.email} disabled />
|
|
<FieldDescription>
|
|
Used to log in. Only you can see it.
|
|
</FieldDescription>
|
|
</Field>
|
|
|
|
{formError && (
|
|
<Alert variant="destructive">
|
|
<AlertDescription>{formError}</AlertDescription>
|
|
</Alert>
|
|
)}
|
|
|
|
<Field orientation="horizontal">
|
|
<Button type="submit" disabled={updateProfile.isPending}>
|
|
{updateProfile.isPending && <Spinner />}
|
|
Save changes
|
|
</Button>
|
|
</Field>
|
|
</FieldGroup>
|
|
</form>
|
|
|
|
<Separator />
|
|
<NotificationSettings />
|
|
</div>
|
|
);
|
|
}
|