diff --git a/src/api/client/types.ts b/src/api/client/types.ts index 7411ff0..fa006ad 100644 --- a/src/api/client/types.ts +++ b/src/api/client/types.ts @@ -1,6 +1,7 @@ export type { Page, PageParams } from "./page"; export type Role = "student" | "parent" | "teacher" | "manager" | "admin"; +export type Gender = "female" | "male" | "other" | "undisclosed"; export type UserTheme = "light" | "dark"; export type UserLanguage = "tr" | "en"; export type CourseKind = "course" | "study" | "club" | string; @@ -21,6 +22,11 @@ export type User = { email: string | null; phone: string | null; birth_date: string | null; + gender: Gender | null; + // Trimmed; at most limits.user.max_address_len characters when set. + address: string | null; + emergency_contact_name: string | null; + emergency_contact_phone: string | null; display_name?: string | null; bio?: string | null; theme: UserTheme | null; @@ -39,6 +45,12 @@ export type ProfileUpdate = { email?: string | null; phone?: string | null; birth_date?: string | null; + // Omitted = keep, "" = clear, a value = set. `gender` must be one of the + // limits.user.genders values; anything else is a 400. + gender?: Gender | null; + address?: string | null; + emergency_contact_name?: string | null; + emergency_contact_phone?: string | null; display_name?: string | null; bio?: string | null; // The teacher's branş, by name from settings.branches. @@ -847,6 +859,10 @@ export type Limits = { max_email_len: number; min_phone_digits: number; max_phone_digits: number; + // Personal-data caps the profile form mirrors: address length and the + // accepted gender vocabulary. + max_address_len: number; + genders: Gender[]; palette_color_len: number; palette_color_pattern: string; roles: Role[]; diff --git a/src/components/users/admin-user-edit-panel.tsx b/src/components/users/admin-user-edit-panel.tsx index 4847107..5717042 100644 --- a/src/components/users/admin-user-edit-panel.tsx +++ b/src/components/users/admin-user-edit-panel.tsx @@ -46,6 +46,7 @@ export function AdminUserEditPanel(props: { branches={settings.latest?.branches} editStudentNumber maxStudentNumberLen={limits.latest?.user.max_student_number_len} + maxAddressLen={limits.latest?.user.max_address_len} onSave={(body) => patchUserProfile(user.id, body)} onSaved={() => void props.onSaved()} /> diff --git a/src/components/users/profile-form.test.tsx b/src/components/users/profile-form.test.tsx new file mode 100644 index 0000000..8eb0877 --- /dev/null +++ b/src/components/users/profile-form.test.tsx @@ -0,0 +1,114 @@ +import { fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; +import type { ProfileUpdate, User } from "@/api/client"; +import { ProfileForm } from "@/components/users/profile-form"; +import { PreferencesProvider } from "@/stores/preferences-context"; + +const baseUser: User = { + id: "u1", + username: "ada", + role: "student", + name: "Ada", + surname: "Yılmaz", + email: null, + phone: null, + birth_date: null, + gender: null, + address: null, + emergency_contact_name: null, + emergency_contact_phone: null, + theme: null, + language: null, + palette_color: null, +}; + +function renderForm(user: User, onSave: (body: ProfileUpdate) => Promise) { + return render(() => ( + + {}} onSave={onSave} maxAddressLen={500} /> + + )); +} + +function setField(label: string, value: string) { + fireEvent.input(screen.getByLabelText(label), { target: { value } }); +} + +describe("ProfileForm personal data fields", () => { + it("sends gender, address and emergency contact on save", async () => { + const onSave = vi.fn(async () => baseUser); + renderForm(baseUser, onSave); + + fireEvent.change(screen.getByLabelText("Gender"), { target: { value: "female" } }); + setField("Address", " Çamlık Mah. 4. Sokak No 12, Kadıköy "); + setField("Emergency contact name", "Veli Yılmaz"); + setField("Emergency contact phone", "+90 532 000 00 00"); + + fireEvent.click(screen.getByRole("button", { name: "Save" })); + + await waitFor(() => + expect(onSave).toHaveBeenCalledWith({ + gender: "female", + address: "Çamlık Mah. 4. Sokak No 12, Kadıköy", + emergency_contact_name: "Veli Yılmaz", + emergency_contact_phone: "+90 532 000 00 00", + }), + ); + }); + + it("clears set fields: gender with null, text fields with empty string", async () => { + const saved: User = { + ...baseUser, + gender: "other", + address: "Eski adres", + emergency_contact_name: "Veli Yılmaz", + emergency_contact_phone: "+90 532 000 00 00", + }; + const onSave = vi.fn(async () => saved); + renderForm(saved, onSave); + + fireEvent.change(screen.getByLabelText("Gender"), { target: { value: "" } }); + setField("Address", ""); + setField("Emergency contact name", ""); + setField("Emergency contact phone", ""); + + fireEvent.click(screen.getByRole("button", { name: "Save" })); + + await waitFor(() => + expect(onSave).toHaveBeenCalledWith({ + gender: null, + address: "", + emergency_contact_name: "", + emergency_contact_phone: "", + }), + ); + }); + + it("skips the PATCH entirely when nothing changed", async () => { + const onSave = vi.fn(async () => baseUser); + renderForm(baseUser, onSave); + + fireEvent.click(screen.getByRole("button", { name: "Save" })); + + expect(onSave).not.toHaveBeenCalled(); + }); + + it("refuses an emergency phone that fails the phone pattern", async () => { + const onSave = vi.fn(async () => baseUser); + renderForm(baseUser, onSave); + + setField("Emergency contact phone", "not-a-phone"); + fireEvent.click(screen.getByRole("button", { name: "Save" })); + + expect(await screen.findByText(/valid phone number/i)).toBeTruthy(); + expect(onSave).not.toHaveBeenCalled(); + }); + + it("renders gender through the localized label, never the raw enum", () => { + renderForm({ ...baseUser, gender: "undisclosed" }, async () => baseUser); + const select = screen.getByLabelText("Gender") as HTMLSelectElement; + expect(select.value).toBe("undisclosed"); + const labels = Array.from(select.options).map((o) => o.textContent); + expect(labels).toContain("Prefer not to say"); + expect(labels).not.toContain("undisclosed"); + }); +}); diff --git a/src/components/users/profile-form.tsx b/src/components/users/profile-form.tsx index 749373e..7558af6 100644 --- a/src/components/users/profile-form.tsx +++ b/src/components/users/profile-form.tsx @@ -1,6 +1,6 @@ import { For, Show, createSignal } from "solid-js"; import { patchMe } from "@/api/users"; -import type { ProfileUpdate, User } from "@/api/client"; +import type { Gender, ProfileUpdate, User } from "@/api/client"; import { formatApiError } from "@/api/client"; import { Alert } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; @@ -10,12 +10,15 @@ import { Label } from "@/components/ui/label"; import { Select } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; import { createFlash } from "@/lib/flash"; +import { GENDER_LABEL_KEYS } from "@/lib/gender"; import { hasMinRole } from "@/lib/roles"; import { useT } from "@/stores/preferences-context"; const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; const PHONE_RE = /^\+?[\d\s().-]{7,20}$/; const DATE_RE = /^\d{4}-\d{2}-\d{2}$/; +// The closed gender vocabulary the backend accepts (limits.user.genders). +const GENDERS: Gender[] = ["female", "male", "other", "undisclosed"]; function isFutureDate(iso: string) { const d = new Date(`${iso}T00:00:00Z`); @@ -54,6 +57,8 @@ export function ProfileForm(props: { branches?: string[]; maxDisplayNameLen?: number; maxBioLen?: number; + /** Cap for the address field, from limits.user.max_address_len. */ + maxAddressLen?: number; /** * Offer the school-issued student number. It is the office's field, not the * account holder's, so only the admin surface passes this; it stays hidden @@ -71,6 +76,10 @@ export function ProfileForm(props: { const [email, setEmail] = createSignal(props.user.email ?? ""); const [phone, setPhone] = createSignal(props.user.phone ?? ""); const [birthDate, setBirthDate] = createSignal(dateInputFromIso(props.user.birth_date)); + const [gender, setGender] = createSignal(props.user.gender ?? ""); + const [address, setAddress] = createSignal(props.user.address ?? ""); + const [emergencyName, setEmergencyName] = createSignal(props.user.emergency_contact_name ?? ""); + const [emergencyPhone, setEmergencyPhone] = createSignal(props.user.emergency_contact_phone ?? ""); const [displayName, setDisplayName] = createSignal(props.profile?.display_name ?? ""); const [bio, setBio] = createSignal(props.profile?.bio ?? ""); const [branch, setBranch] = createSignal(props.profile?.branch ?? ""); @@ -92,8 +101,10 @@ export function ProfileForm(props: { const e = email().trim(); const p = phone().trim(); const b = isoFromDateInput(birthDate()); + const ep = emergencyPhone().trim(); if (e && !EMAIL_RE.test(e)) return t("profile.emailInvalid"); if (p && !PHONE_RE.test(p)) return t("profile.phoneInvalid"); + if (ep && !PHONE_RE.test(ep)) return t("profile.phoneInvalid"); if (b && (!DATE_RE.test(b) || isFutureDate(b))) return t("profile.dateInvalid"); return null; }; @@ -110,6 +121,16 @@ export function ProfileForm(props: { if (e !== (props.user.email ?? "")) b.email = e; if (p !== (props.user.phone ?? "")) b.phone = p; if (d !== (props.user.birth_date ?? "")) b.birth_date = d; + // Same omitted=keep / ""=clear / value=set merge the backend applies. An + // empty gender select sends an explicit null (clear); a pick sends the enum. + const g = gender(); + if (g !== (props.user.gender ?? "")) b.gender = g === "" ? null : (g as Gender); + const a = address().trim(); + if (a !== (props.user.address ?? "")) b.address = a; + const em = emergencyName().trim(); + if (em !== (props.user.emergency_contact_name ?? "")) b.emergency_contact_name = em; + const ep = emergencyPhone().trim(); + if (ep !== (props.user.emergency_contact_phone ?? "")) b.emergency_contact_phone = ep; // Blank clears it server-side, which is how the office un-numbers a student. if (showStudentNumber() && studentNumber().trim() !== (props.user.student_number ?? "")) { b.student_number = studentNumber().trim(); @@ -222,6 +243,36 @@ export function ProfileForm(props: { +
+ + +
+
+ +