diff --git a/src/components/RoleSwitcher.tsx b/src/components/RoleSwitcher.tsx index b4c0c06..579e45f 100644 --- a/src/components/RoleSwitcher.tsx +++ b/src/components/RoleSwitcher.tsx @@ -3,10 +3,11 @@ import { useNavigate } from 'react-router-dom'; import { useStore } from '../data/store'; import { canSwitchRoles, defaultView, describeView, ROLE_ORDER, ROLE_VIEWS } from '../lib/viewAs'; import { Check } from './icons'; +import { fullName } from '../lib/names'; /** Bottom-left switcher (FW-09): administrators view SignMeUp as each user type without separate accounts. */ export function RoleSwitcher() { - const { db, realUser, viewAs, setViewAs } = useStore(); + const { db, realUser, viewAs, setViewAs, createMyStudentRecord } = useStore(); const [open, setOpen] = useState(false); const nav = useNavigate(); @@ -21,7 +22,14 @@ export function RoleSwitcher() { const active = viewAs?.role ?? 'admin'; const advisors = db.users.filter((u) => u.role === 'advisor' && u.facultyId); + const mine = realUser?.studentId && db.students.find((s) => s.id === realUser.studentId); + function chooseStudent(value: string) { + if (value !== '__mine') return choose('student', { studentId: value }); + const id = createMyStudentRecord(); // first time: start a student record under the administrator's own ID + if (id) choose('student', { studentId: id }); + } function choose(role: typeof active, pick?: { studentId?: string; facultyId?: string }) { + if (role === 'student' && !pick && mine) pick = { studentId: mine.id }; // viewing as a student defaults to yourself when you have a record setViewAs(role === 'admin' ? null : { ...defaultView(db, role), ...pick }); setOpen(false); nav('/'); @@ -44,9 +52,10 @@ export function RoleSwitcher() { {active === r && } {r === 'student' && ( - chooseStudent(e.target.value)}> - {db.students.map((s) => )} + {mine ? : } + {db.students.filter((s) => s.id !== realUser?.studentId).map((s) => )} )} {r === 'faculty' && ( diff --git a/src/data/store.tsx b/src/data/store.tsx index 052d22f..0901d0c 100644 --- a/src/data/store.tsx +++ b/src/data/store.tsx @@ -57,6 +57,7 @@ interface StoreApi { realUser: User | null; // always the signed-in user viewAs: ViewAs | null; setViewAs(v: ViewAs | null): void; + createMyStudentRecord(): string | null; // administrators: a student record under their own ID, to view the site as themselves session: Session; mode: StoreMode; syncError: string | null; @@ -301,6 +302,19 @@ export function StoreProvider({ children }: { children: ReactNode }) { try { if (next) sessionStorage.setItem(VIEW_KEY, JSON.stringify(next)); else sessionStorage.removeItem(VIEW_KEY); } catch { /* not remembered across reloads */ } }, + createMyStudentRecord() { + if (!realUser || !canSwitchRoles(realUser)) return null; + const id = realUser.studentId ?? realUser.id; + if (realUser.studentId && db.students.some((s) => s.id === id)) return id; + if (db.students.some((s) => s.id === id)) return null; // that ID already belongs to someone else's record + commit((d) => stamp({ + ...d, + users: d.users.map((u) => (u.id === realUser.id ? { ...u, studentId: id } : u)), + students: d.students.some((s) => s.id === id) ? d.students : [...d.students, skeletonStudent(id, realUser.name)], + }, 'FRAMEWORK', `Created a student record for ${realUser.name} (${id})`, realUser.name)); + return id; + }, + signOut() { try { sessionStorage.removeItem(VIEW_KEY); } catch { /* nothing stored */ } void clerk.signOut({ redirectUrl: '/login' }); }, resetDemo() { diff --git a/src/screens/CourseSearch.tsx b/src/screens/CourseSearch.tsx index c679453..1d12330 100644 --- a/src/screens/CourseSearch.tsx +++ b/src/screens/CourseSearch.tsx @@ -21,7 +21,7 @@ const COLS = '84px 1fr 44px 130px 112px 92px'; // seats fits "3 seats left" and export default function CourseSearch() { const { db, user, register, requestWaiver } = useStore(); const toast = useToast(); - const student = db.students.find((s) => s.id === user?.studentId); + const student = user?.role === 'student' ? db.students.find((s) => s.id === user.studentId) : undefined; // staff accounts may also own a student record; it applies only when acting as a student const [q, setQ] = useState(''); const [dept, setDept] = useState(''); const [openOnly, setOpenOnly] = useState(true); diff --git a/src/screens/Dashboard.tsx b/src/screens/Dashboard.tsx index fd0a2bf..2363ad8 100644 --- a/src/screens/Dashboard.tsx +++ b/src/screens/Dashboard.tsx @@ -15,7 +15,7 @@ import { formatDate, formatTimeRange, shortName } from '../lib/format'; export default function Dashboard() { const { db, user, register, drop } = useStore(); const toast = useToast(); - const student = db.students.find((s) => s.id === user?.studentId); + const student = user?.role === 'student' ? db.students.find((s) => s.id === user.studentId) : undefined; // staff accounts may also own a student record; it applies only when acting as a student if (!user) return null; if (!student) return ;