hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { Show } from "solid-js"; |
| 2 | import { createResource } from "@/lib/create-resource"; |
| 3 | import { getLimits } from "@/api/limits"; |
| 4 | import { getSettings } from "@/api/settings"; |
| 5 | import { getUserProfile, patchUserProfile } from "@/api/users"; |
| 6 | import type { User } from "@/api/client"; |
| 7 | import { ProfileForm } from "@/components/users/profile-form"; |
| 8 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 9 | import { SidePanel } from "@/components/ui/side-panel"; |
| 10 | import { useT } from "@/stores/preferences-context"; |
| 11 | |
| 12 | /** |
| 13 | * The admin's edit form for one account (name, contact, branş, student |
| 14 | * number, display name/bio). One component for the admin user detail header |
| 15 | * and the users list's row menu, so both send the same PATCH. It reads the |
| 16 | * profile (display name, bio, branş live only there), the branş vocabulary and |
| 17 | * the field limits only while open, and mounts the form once they are in: |
| 18 | * ProfileForm reads its initial values once. The caller closes it and |
| 19 | * refetches in `onSaved`. |
| 20 | */ |
| 21 | export function AdminUserEditPanel(props: { |
| 22 | user: User | null; |
| 23 | open: boolean; |
| 24 | onOpenChange: (open: boolean) => void; |
| 25 | onSaved: () => void | Promise<void>; |
| 26 | }) { |
| 27 | const t = useT(); |
| 28 | const openId = () => (props.open && props.user ? props.user.id : null); |
| 29 | const [profile] = createResource(openId, (userId) => getUserProfile(userId).catch(() => null)); |
| 30 | const [settings] = createResource(() => (props.open ? true : null), () => getSettings().catch(() => null)); |
| 31 | const [limits] = createResource(() => (props.open ? true : null), () => getLimits().catch(() => null)); |
| 32 | |
| 33 | // `.latest` suspends until a first value, which would blank the page's own |
| 34 | // Suspense boundary; the form waits for all three reads instead. |
| 35 | const settled = (res: { state: string }) => res.state === "ready" || res.state === "errored"; |
| 36 | const ready = () => settled(profile) && settled(settings) && settled(limits); |
| 37 | |
| 38 | return ( |
| 39 | <SidePanel guardUnsaved open={props.open && !!props.user} onOpenChange={props.onOpenChange} title={t("profile.edit")} description={props.user?.username}> |
| 40 | <Show when={props.user} keyed> |
| 41 | {(user) => ( |
| 42 | <Show when={ready()} fallback={<PageSpinner />}> |
| 43 | <ProfileForm |
| 44 | user={user} |
| 45 | profile={profile.latest ? { display_name: profile.latest.display_name, bio: profile.latest.bio, branch: profile.latest.branch } : undefined} |
| 46 | branches={settings.latest?.branches} |
| 47 | editStudentNumber |
| 48 | maxStudentNumberLen={limits.latest?.user.max_student_number_len} |
| 49 | maxAddressLen={limits.latest?.user.max_address_len} |
| 50 | onSave={(body) => patchUserProfile(user.id, body)} |
| 51 | onSaved={() => void props.onSaved()} |
| 52 | /> |
| 53 | </Show> |
| 54 | )} |
| 55 | </Show> |
| 56 | </SidePanel> |
| 57 | ); |
| 58 | } |