Files
gerbeur/src/components/profile/ChangePasswordModal.tsx
khannurien 563daa720c
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 50s
Build and Deploy Static Documentation Site / build-and-push (push) Successful in 22s
v3: src/ is documented like api/ and shared/, and the reference covers the whole repository — the frontend had no module docs, JSDoc on some components and hooks and not on others, props typed inline in half the components and in private interfaces in the rest, // comments on declarations, and no return types on 147 exported functions, so deno doc --lint over src/ found 637 errors. The rule api/ follows is applied to every non-test module of src/: a /** … @module */ block, JSDoc on every export and every field of an exported interface, /** */ for declarations and // for notes, verbs for functions ("Renders…" for a component), parameters named in the prose, {@link} only for symbols in scope, and a page's JSDoc names its route and who may see it (App.tsx's module doc lists the route table). For deno doc to show them, every exported function states its return type: components React.JSX.Element (or | null, or a portal), memo and forwardRef components their exotic types, contexts React.Context<…>, and a hook returning several things a documented Use<Name>Result interface (useAuth, useRequiredAuth, useFading, useEmojiTrigger, useMentionAutocomplete, useDumpCardLink, usePlayRichContent; FeedCacheResult, YouTubePlaylistControls and UseApiFormReturn are renamed to match). Every component that takes props takes an exported, documented <Name>Props interface — the 64 inline prop types are lifted into one, DumpFeed gets DumpFeedProps extending MainFeedProps. React types go through the global React namespace: the four default imports of react are gone, since deno doc can't follow them. Two duplicates go too: useMentionAutocomplete's UserResult was shared/'s UserSuggestion, and JournalCard re-exported JournalShape for no one; i18n.ts no longer re-exports Lingui's i18n, which its three users now import from @lingui/core. deno task doc and doc:lint now take src/ (but its tests and the generated locales), the index page gains a Frontend section linking every module by layer and domain, mod.test.ts walks src/ as well, and the docs workflow runs on changes to src/. deno doc doesn't document npm packages, so it takes a type imported from one — react-hook-form's FieldValues in the form fields, frimousse's SkinTone — for a private one; doc:lint now goes through doc-lint.ts, which fails on every diagnostic but those, 25 of them. The README states the frontend's part of the rule. No behaviour changes; 205 backend and 628 frontend tests pass, and tsc, deno check and deno lint are clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 07:31:22 +00:00

132 lines
4.2 KiB
TypeScript

/**
* The password change modal.
*
* @module
*/
import { useState } from "react";
import { t } from "@lingui/core/macro";
import { Trans } from "@lingui/react/macro";
import { API_URL } from "../../config/api.ts";
import { VALIDATION } from "../../../shared/validation.ts";
import { useAuth } from "../../hooks/auth/useAuth.ts";
import { Modal } from "../shared/Modal.tsx";
import { expectOk } from "../../utils/shared/api.ts";
import { FormActions } from "../form/FormActions.tsx";
import { FormError } from "../form/FormError.tsx";
import { FormProvider } from "react-hook-form";
import { SubmitButton } from "../form/SubmitButton.tsx";
import { TextField } from "../form/TextField.tsx";
import { useApiForm } from "../../hooks/form/useApiForm.ts";
/** The props of {@link ChangePasswordModal}. */
export interface ChangePasswordModalProps {
/** Called to close it. */
onClose: () => void;
}
/** The form's fields. */
interface Values {
currentPassword: string;
newPassword: string;
confirmPassword: string;
}
/**
* Renders the form that changes the user's password, given the current one. The
* change signs out every other session, and this one keeps the fresh tokens it
* is handed.
*/
export function ChangePasswordModal(
{ onClose }: ChangePasswordModalProps,
): React.JSX.Element {
const { authFetch, updateSession } = useAuth();
const [done, setDone] = useState(false);
const form = useApiForm<Values>({
mode: "onChange",
defaultValues: {
currentPassword: "",
newPassword: "",
confirmPassword: "",
},
});
const onSubmit = form.submit(async ({ currentPassword, newPassword }) => {
// The change signs out every session opened with the old password, this
// one included, so keep the fresh tokens it hands back.
const tokens = await expectOk<{ token: string; mediaToken: string }>(
await authFetch(`${API_URL}/api/users/me/change-password`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ currentPassword, newPassword }),
}),
);
updateSession(tokens);
setDone(true);
});
return (
<Modal title={t`Change password`} onClose={onClose}>
{done
? (
<div className="form">
<p className="form-success">
<Trans>Password changed successfully.</Trans>
</p>
<FormActions>
<button type="button" className="btn-primary" onClick={onClose}>
<Trans>Close</Trans>
</button>
</FormActions>
</div>
)
: (
<FormProvider {...form}>
<form className="form" onSubmit={onSubmit}>
<TextField<Values>
name="currentPassword"
type="password"
label={t`Current password`}
autoComplete="current-password"
autoFocus
rules={{ required: true }}
/>
<TextField<Values>
name="newPassword"
type="password"
label={t`New password`}
autoComplete="new-password"
maxLength={VALIDATION.PASSWORD_MAX}
rules={{
required: true,
minLength: {
value: VALIDATION.PASSWORD_MIN,
message: t`At least ${VALIDATION.PASSWORD_MIN} characters`,
},
}}
/>
<TextField<Values>
name="confirmPassword"
type="password"
label={t`Confirm new password`}
autoComplete="new-password"
rules={{
required: true,
validate: (value, values) =>
value === values.newPassword ||
t`Passwords do not match`,
}}
/>
<FormError title={t`Could not change password`} />
<FormActions onCancel={onClose}>
<SubmitButton pendingLabel={<Trans>Saving…</Trans>}>
<Trans>Change password</Trans>
</SubmitButton>
</FormActions>
</form>
</FormProvider>
)}
</Modal>
);
}