👤 add a teacher or student straight from their roster
Browse files at this commita43c7c39fe71d5f0ca0d3b4d650d480adae17cdbParent: 52bbe31- feat
- 👤 add a teacher or student straight from their roster
3 changed files +56 -22
- src/components/users/create-user-panel.tsx +13 -9
- src/pages/students-roster-page.tsx +21 -6
- src/pages/teachers-roster-page.tsx +22 -7
src/components/users/create-user-panel.tsxmodified+13 -9
| @@ -24,12 +24,14 @@ export function CreateUserPanel(props: { | |||
| 24 | open: boolean; | 24 | open: boolean; |
| 25 | onOpenChange: (open: boolean) => void; | 25 | onOpenChange: (open: boolean) => void; |
| 26 | onCreated: (user: User) => void; | 26 | onCreated: (user: User) => void; |
| 27 | + /** Fix the new account to one role when opened from a role-specific roster. */ | ||
| 28 | + fixedRole?: Role; | ||
| 27 | }) { | 29 | }) { |
| 28 | const t = useT(); | 30 | const t = useT(); |
| 29 | const [limits] = createResource(() => getLimits()); | 31 | const [limits] = createResource(() => getLimits()); |
| 30 | const [username, setUsername] = createSignal(""); | 32 | const [username, setUsername] = createSignal(""); |
| 31 | const [password, setPassword] = createSignal(""); | 33 | const [password, setPassword] = createSignal(""); |
| 32 | - const [role, setRole] = createSignal<Role>("student"); | 34 | + const [role, setRole] = createSignal<Role>(props.fixedRole ?? "student"); |
| 33 | const [error, setError] = createSignal(""); | 35 | const [error, setError] = createSignal(""); |
| 34 | const [pending, setPending] = createSignal(false); | 36 | const [pending, setPending] = createSignal(false); |
| 35 | 37 | ||
| @@ -38,7 +40,7 @@ export function CreateUserPanel(props: { | |||
| 38 | const reset = () => { | 40 | const reset = () => { |
| 39 | setUsername(""); | 41 | setUsername(""); |
| 40 | setPassword(""); | 42 | setPassword(""); |
| 41 | - setRole("student"); | 43 | + setRole(props.fixedRole ?? "student"); |
| 42 | setError(""); | 44 | setError(""); |
| 43 | }; | 45 | }; |
| 44 | 46 | ||
| @@ -61,7 +63,7 @@ export function CreateUserPanel(props: { | |||
| 61 | if (msg) { setError(msg); return; } | 63 | if (msg) { setError(msg); return; } |
| 62 | setPending(true); | 64 | setPending(true); |
| 63 | try { | 65 | try { |
| 64 | - const created = await postUser({ username: username().trim(), password: password(), role: role() }); | 66 | + const created = await postUser({ username: username().trim(), password: password(), role: props.fixedRole ?? role() }); |
| 65 | reset(); | 67 | reset(); |
| 66 | props.onCreated(created); | 68 | props.onCreated(created); |
| 67 | } catch (err) { | 69 | } catch (err) { |
| @@ -110,12 +112,14 @@ export function CreateUserPanel(props: { | |||
| 110 | /> | 112 | /> |
| 111 | <p class="text-xs text-muted-foreground">{t("admin.createUserPasswordHint")}</p> | 113 | <p class="text-xs text-muted-foreground">{t("admin.createUserPasswordHint")}</p> |
| 112 | </div> | 114 | </div> |
| 113 | - <div class="space-y-1.5"> | 115 | + <Show when={!props.fixedRole}> |
| 114 | - <Label for="create-user-role">{t("admin.role")}</Label> | 116 | + <div class="space-y-1.5"> |
| 115 | - <Select id="create-user-role" class="h-10" value={role()} onChange={(e) => setRole(e.currentTarget.value as Role)}> | 117 | + <Label for="create-user-role">{t("admin.role")}</Label> |
| 116 | - <For each={ROLES}>{(r) => <option value={r}>{t(`role.${r}` as MessageKey)}</option>}</For> | 118 | + <Select id="create-user-role" class="h-10" value={role()} onChange={(e) => setRole(e.currentTarget.value as Role)}> |
| 117 | - </Select> | 119 | + <For each={ROLES}>{(r) => <option value={r}>{t(`role.${r}` as MessageKey)}</option>}</For> |
| 118 | - </div> | 120 | + </Select> |
| 121 | + </div> | ||
| 122 | + </Show> | ||
| 119 | <div class="flex flex-wrap items-center justify-end gap-2"> | 123 | <div class="flex flex-wrap items-center justify-end gap-2"> |
| 120 | <Button type="button" variant="outline" class="h-10 rounded-lg" onClick={close}> | 124 | <Button type="button" variant="outline" class="h-10 rounded-lg" onClick={close}> |
| 121 | {t("common.cancel")} | 125 | {t("common.cancel")} |
src/pages/students-roster-page.tsxmodified+21 -6
| @@ -2,11 +2,13 @@ import { useNavigate } from "@tanstack/solid-router"; | |||
| 2 | import type { ColumnDef } from "@tanstack/solid-table"; | 2 | import type { ColumnDef } from "@tanstack/solid-table"; |
| 3 | import { For, Show, Suspense, createMemo, createSignal } from "solid-js"; | 3 | import { For, Show, Suspense, createMemo, createSignal } from "solid-js"; |
| 4 | import { createResource } from "@/lib/create-resource"; | 4 | import { createResource } from "@/lib/create-resource"; |
| 5 | +import { matchesSearch } from "@/lib/search-text"; | ||
| 5 | import { getClassMembers, getClasses } from "@/api/classes"; | 6 | import { getClassMembers, getClasses } from "@/api/classes"; |
| 6 | import { getAcademicYears } from "@/api/academic-years"; | 7 | import { getAcademicYears } from "@/api/academic-years"; |
| 7 | import { getUserSearch } from "@/api/users"; | 8 | import { getUserSearch } from "@/api/users"; |
| 8 | import { formatApiError, type ClassGroup, type PersonRef } from "@/api/client"; | 9 | import { formatApiError, type ClassGroup, type PersonRef } from "@/api/client"; |
| 9 | import { RouteGuard } from "@/components/layout/route-guard"; | 10 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 11 | +import { CreateUserPanel } from "@/components/users/create-user-panel"; | ||
| 10 | import { RosterPersonCell } from "@/components/users/roster-person-cell"; | 12 | import { RosterPersonCell } from "@/components/users/roster-person-cell"; |
| 11 | import { Button } from "@/components/ui/button"; | 13 | import { Button } from "@/components/ui/button"; |
| 12 | import { ComingSoonBadge, ComingSoonValue } from "@/components/ui/coming-soon"; | 14 | import { ComingSoonBadge, ComingSoonValue } from "@/components/ui/coming-soon"; |
| @@ -17,6 +19,7 @@ import { Select } from "@/components/ui/select"; | |||
| 17 | import { TableRowActions } from "@/components/ui/table-row-actions"; | 19 | import { TableRowActions } from "@/components/ui/table-row-actions"; |
| 18 | import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; | 20 | import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 19 | import { useT } from "@/stores/preferences-context"; | 21 | import { useT } from "@/stores/preferences-context"; |
| 22 | +import { useAuth } from "@/stores/auth-context"; | ||
| 20 | 23 | ||
| 21 | const ROSTER_PAGE_SIZE = 10; | 24 | const ROSTER_PAGE_SIZE = 10; |
| 22 | 25 | ||
| @@ -35,9 +38,11 @@ export default function StudentsRosterPage() { | |||
| 35 | // columns keep the design's slot and say "yakında" instead of a number. | 38 | // columns keep the design's slot and say "yakında" instead of a number. |
| 36 | function StudentsRosterContent() { | 39 | function StudentsRosterContent() { |
| 37 | const t = useT(); | 40 | const t = useT(); |
| 41 | + const auth = useAuth(); | ||
| 38 | const navigate = useNavigate(); | 42 | const navigate = useNavigate(); |
| 39 | const [classFilter, setClassFilter] = createSignal(""); | 43 | const [classFilter, setClassFilter] = createSignal(""); |
| 40 | const [yearFilter, setYearFilter] = createSignal(""); | 44 | const [yearFilter, setYearFilter] = createSignal(""); |
| 45 | + const [creating, setCreating] = createSignal(false); | ||
| 41 | 46 | ||
| 42 | const [data, { refetch }] = createResource(async () => { | 47 | const [data, { refetch }] = createResource(async () => { |
| 43 | const [students, classes, years] = await Promise.all([ | 48 | const [students, classes, years] = await Promise.all([ |
| @@ -131,11 +136,12 @@ function StudentsRosterContent() { | |||
| 131 | {t("roster.import")} | 136 | {t("roster.import")} |
| 132 | <ComingSoonBadge class="ml-1.5" /> | 137 | <ComingSoonBadge class="ml-1.5" /> |
| 133 | </Button> | 138 | </Button> |
| 134 | - <Button size="sm" class="min-w-[7.5rem] rounded-lg" disabled title={t("comingSoon.title")}> | 139 | + <Show when={auth.user()?.role === "admin"}> |
| 135 | - <IconPlus class="h-4 w-4" /> | 140 | + <Button size="sm" class="min-w-[7.5rem] rounded-lg" onClick={() => setCreating(true)}> |
| 136 | - {t("roster.addStudent")} | 141 | + <IconPlus class="h-4 w-4" /> |
| 137 | - <ComingSoonBadge class="ml-1.5" /> | 142 | + {t("roster.addStudent")} |
| 138 | - </Button></>} | 143 | + </Button> |
| 144 | + </Show></>} | ||
| 139 | columns={columns()} | 145 | columns={columns()} |
| 140 | data={rows()} | 146 | data={rows()} |
| 141 | tableClass="min-w-[940px]" | 147 | tableClass="min-w-[940px]" |
| @@ -143,7 +149,7 @@ function StudentsRosterContent() { | |||
| 143 | filterPlaceholder={t("roster.searchStudents")} | 149 | filterPlaceholder={t("roster.searchStudents")} |
| 144 | filterHint={t("search.hint.students")} | 150 | filterHint={t("search.hint.students")} |
| 145 | searchPredicate={(row, query) => | 151 | searchPredicate={(row, query) => |
| 146 | - [row.person.username, row.person.display_name ?? ""].join(" ").toLocaleLowerCase().includes(query.toLocaleLowerCase()) | 152 | + matchesSearch(query, row.person.username, row.person.display_name) |
| 147 | } | 153 | } |
| 148 | filters={ | 154 | filters={ |
| 149 | <> | 155 | <> |
| @@ -166,6 +172,15 @@ function StudentsRosterContent() { | |||
| 166 | </Show> | 172 | </Show> |
| 167 | </Suspense> | 173 | </Suspense> |
| 168 | </section> | 174 | </section> |
| 175 | + <CreateUserPanel | ||
| 176 | + open={creating()} | ||
| 177 | + onOpenChange={setCreating} | ||
| 178 | + fixedRole="student" | ||
| 179 | + onCreated={() => { | ||
| 180 | + setCreating(false); | ||
| 181 | + void refetch(); | ||
| 182 | + }} | ||
| 183 | + /> | ||
| 169 | </div> | 184 | </div> |
| 170 | ); | 185 | ); |
| 171 | } | 186 | } |
src/pages/teachers-roster-page.tsxmodified+22 -7
| @@ -1,11 +1,13 @@ | |||
| 1 | import { useNavigate } from "@tanstack/solid-router"; | 1 | import { useNavigate } from "@tanstack/solid-router"; |
| 2 | import type { ColumnDef } from "@tanstack/solid-table"; | 2 | import type { ColumnDef } from "@tanstack/solid-table"; |
| 3 | -import { For, Show, Suspense, createMemo } from "solid-js"; | 3 | +import { For, Show, Suspense, createMemo, createSignal } from "solid-js"; |
| 4 | import { createResource } from "@/lib/create-resource"; | 4 | import { createResource } from "@/lib/create-resource"; |
| 5 | +import { matchesSearch } from "@/lib/search-text"; | ||
| 5 | import { getClasses } from "@/api/classes"; | 6 | import { getClasses } from "@/api/classes"; |
| 6 | import { getUserSearch } from "@/api/users"; | 7 | import { getUserSearch } from "@/api/users"; |
| 7 | import { formatApiError, type PersonRef } from "@/api/client"; | 8 | import { formatApiError, type PersonRef } from "@/api/client"; |
| 8 | import { RouteGuard } from "@/components/layout/route-guard"; | 9 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 10 | +import { CreateUserPanel } from "@/components/users/create-user-panel"; | ||
| 9 | import { RosterPersonCell } from "@/components/users/roster-person-cell"; | 11 | import { RosterPersonCell } from "@/components/users/roster-person-cell"; |
| 10 | import { Button } from "@/components/ui/button"; | 12 | import { Button } from "@/components/ui/button"; |
| 11 | import { ComingSoonBadge, ComingSoonValue } from "@/components/ui/coming-soon"; | 13 | import { ComingSoonBadge, ComingSoonValue } from "@/components/ui/coming-soon"; |
| @@ -15,6 +17,7 @@ import { IconEye, IconPlus, IconUploadCloud } from "@/components/ui/icons"; | |||
| 15 | import { TableRowActions } from "@/components/ui/table-row-actions"; | 17 | import { TableRowActions } from "@/components/ui/table-row-actions"; |
| 16 | import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; | 18 | import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 17 | import { useT } from "@/stores/preferences-context"; | 19 | import { useT } from "@/stores/preferences-context"; |
| 20 | +import { useAuth } from "@/stores/auth-context"; | ||
| 18 | 21 | ||
| 19 | const ROSTER_PAGE_SIZE = 10; | 22 | const ROSTER_PAGE_SIZE = 10; |
| 20 | 23 | ||
| @@ -33,7 +36,9 @@ export default function TeachersRosterPage() { | |||
| 33 | // status have no field anywhere, so they stay "yakında". | 36 | // status have no field anywhere, so they stay "yakında". |
| 34 | function TeachersRosterContent() { | 37 | function TeachersRosterContent() { |
| 35 | const t = useT(); | 38 | const t = useT(); |
| 39 | + const auth = useAuth(); | ||
| 36 | const navigate = useNavigate(); | 40 | const navigate = useNavigate(); |
| 41 | + const [creating, setCreating] = createSignal(false); | ||
| 37 | 42 | ||
| 38 | const [data, { refetch }] = createResource(async () => { | 43 | const [data, { refetch }] = createResource(async () => { |
| 39 | const [teachers, classes] = await Promise.all([getUserSearch("", undefined, "teacher"), getClasses()]); | 44 | const [teachers, classes] = await Promise.all([getUserSearch("", undefined, "teacher"), getClasses()]); |
| @@ -104,11 +109,12 @@ function TeachersRosterContent() { | |||
| 104 | {t("roster.import")} | 109 | {t("roster.import")} |
| 105 | <ComingSoonBadge class="ml-1.5" /> | 110 | <ComingSoonBadge class="ml-1.5" /> |
| 106 | </Button> | 111 | </Button> |
| 107 | - <Button size="sm" class="min-w-[7.5rem] rounded-lg" disabled title={t("comingSoon.title")}> | 112 | + <Show when={auth.user()?.role === "admin"}> |
| 108 | - <IconPlus class="h-4 w-4" /> | 113 | + <Button size="sm" class="min-w-[7.5rem] rounded-lg" onClick={() => setCreating(true)}> |
| 109 | - {t("roster.addTeacher")} | 114 | + <IconPlus class="h-4 w-4" /> |
| 110 | - <ComingSoonBadge class="ml-1.5" /> | 115 | + {t("roster.addTeacher")} |
| 111 | - </Button></>} | 116 | + </Button> |
| 117 | + </Show></>} | ||
| 112 | columns={columns()} | 118 | columns={columns()} |
| 113 | data={rows()} | 119 | data={rows()} |
| 114 | tableClass="min-w-[940px]" | 120 | tableClass="min-w-[940px]" |
| @@ -116,7 +122,7 @@ function TeachersRosterContent() { | |||
| 116 | filterPlaceholder={t("roster.searchTeachers")} | 122 | filterPlaceholder={t("roster.searchTeachers")} |
| 117 | filterHint={t("search.hint.teachers")} | 123 | filterHint={t("search.hint.teachers")} |
| 118 | searchPredicate={(row, query) => | 124 | searchPredicate={(row, query) => |
| 119 | - [row.person.username, row.person.display_name ?? ""].join(" ").toLocaleLowerCase().includes(query.toLocaleLowerCase()) | 125 | + matchesSearch(query, row.person.username, row.person.display_name) |
| 120 | } | 126 | } |
| 121 | enablePagination | 127 | enablePagination |
| 122 | pageSize={ROSTER_PAGE_SIZE} | 128 | pageSize={ROSTER_PAGE_SIZE} |
| @@ -127,6 +133,15 @@ function TeachersRosterContent() { | |||
| 127 | </Show> | 133 | </Show> |
| 128 | </Suspense> | 134 | </Suspense> |
| 129 | </section> | 135 | </section> |
| 136 | + <CreateUserPanel | ||
| 137 | + open={creating()} | ||
| 138 | + onOpenChange={setCreating} | ||
| 139 | + fixedRole="teacher" | ||
| 140 | + onCreated={() => { | ||
| 141 | + setCreating(false); | ||
| 142 | + void refetch(); | ||
| 143 | + }} | ||
| 144 | + /> | ||
| 130 | </div> | 145 | </div> |
| 131 | ); | 146 | ); |
| 132 | } | 147 | } |