Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/components/RoleSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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('/');
Expand All @@ -44,9 +52,10 @@ export function RoleSwitcher() {
{active === r && <Check />}
</button>
{r === 'student' && (
<select className="input sm" aria-label="Student to view as" value={viewAs?.role === 'student' ? viewAs.studentId : ''} onChange={(e) => choose('student', { studentId: e.target.value })}>
<select className="input sm" aria-label="Student to view as" value={viewAs?.role === 'student' ? viewAs.studentId : ''} onChange={(e) => chooseStudent(e.target.value)}>
<option value="" disabled>Choose student…</option>
{db.students.map((s) => <option key={s.id} value={s.id}>{s.firstName} {s.lastName} · {s.id}</option>)}
{mine ? <option value={mine.id}>Me · {fullName(mine)} · {mine.id}</option> : <option value="__mine">Me · create my student record</option>}
{db.students.filter((s) => s.id !== realUser?.studentId).map((s) => <option key={s.id} value={s.id}>{fullName(s)} · {s.id}</option>)}
</select>
)}
{r === 'faculty' && (
Expand Down
14 changes: 14 additions & 0 deletions src/data/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/screens/CourseSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Navigate to="/search" replace />;

Expand Down
Loading