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
2 changes: 1 addition & 1 deletion oh_queue/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ def create_group(form):
def load_group(group_id):
if not group_id:
return socket_error("Invalid group ID")
group = Group.query.filter_by(id=group_id, course=get_course()).one()
group = Group.query.filter_by(id=group_id, course=get_course()).one_or_none()
if group:
return group_json(group)

Expand Down
31 changes: 24 additions & 7 deletions src/components/app.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import React from "react";
import { BrowserRouter, Route, Routes, useNavigate } from "react-router-dom";
import {
BrowserRouter,
Route,
Routes,
useNavigate,
useParams,
} from "react-router-dom";
import { setApp } from "../app_instance";
import { isPartyRoot } from "../common";
import { ActivityLogLayout } from "./activity_log_layout";
Expand All @@ -19,6 +25,7 @@ import {
initialState,
isStaff,
loadTicket,
receiveGroup,
receiveTicket,
setGroup,
setTicket,
Expand Down Expand Up @@ -120,7 +127,7 @@ export class App extends React.Component {
this.state.currentUser = data.current_user;
}
if (Object.hasOwn(data, "groups")) {
this.state.groups = Array.from(data.groups).sort((x) => x.id);
this.state.groups = Array.from(data.groups).sort((a, b) => a.id - b.id);
}
if (Object.hasOwn(data, "presence")) {
this.state.presence = data.presence;
Expand Down Expand Up @@ -155,7 +162,7 @@ export class App extends React.Component {
loadGroup(id) {
if (isStaff(this.state)) {
this.socket.emit("load_group", id, (group) => {
setGroup(this.state, group);
receiveGroup(this.state, id, group);
this.refresh();
});
}
Expand Down Expand Up @@ -213,10 +220,11 @@ export class App extends React.Component {
}
if (callback) callback(response);
};
// A promise of whether the request succeeded (see Socket.emit).
if (request !== undefined) {
this.socket.emit(eventType, request, cb);
return this.socket.emit(eventType, request, cb);
} else {
this.socket.emit(eventType, cb);
return this.socket.emit(eventType, cb);
}
}

Expand Down Expand Up @@ -250,7 +258,8 @@ export class App extends React.Component {
<Route
path="/tickets/:id/*"
element={
<TicketLayout
<ByID
page={TicketLayout}
state={state}
socket={this.socket}
loadTicket={this.loadTicket}
Expand All @@ -260,7 +269,8 @@ export class App extends React.Component {
<Route
path="/groups/:id/*"
element={
<PartyGroupLayout
<ByID
page={PartyGroupLayout}
state={state}
socket={this.socket}
loadGroup={this.loadGroup}
Expand All @@ -279,6 +289,13 @@ export class App extends React.Component {
}
}

// A new page for each ticket or group, so nothing typed into or loaded for one
// (a description, an assignment) carries over to the next, as after Next Ticket.
function ByID({ page: Page, ...props }) {
const { id } = useParams();
return <Page key={id} {...props} />;
}

// Gives App the router's navigate function, for following the server's redirects.
function RememberNavigate({ onNavigate }) {
const navigate = useNavigate();
Expand Down
10 changes: 5 additions & 5 deletions src/components/config_linked_form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export function ConfigLinked({ configKey, configKeys = [], config, render }) {
? { key: configKey, value: e.target ? value : e }
: { keys: configKeys, values };

app.makeRequest(`update_config`, requestBody, (isSuccess) => {
if (isSuccess) {
minimumTime.then(() => setSaving(false));
}
});
// Whether or not it worked: a failed save shows the offline banner, and can be tried again.
app
.makeRequest(`update_config`, requestBody)
.then(() => minimumTime)
.then(() => setSaving(false));

return false;
};
Expand Down
114 changes: 38 additions & 76 deletions src/components/magic_word_display.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,48 @@ import React from "react";
import { app } from "../app_instance";
import { isStaff } from "../state";

export class MagicWordDisplay extends React.Component {
constructor(...args) {
super(...args);

this.state = {
loaded: false,
oldMagicWord: undefined,
magicWord: undefined,
refreshInterval: null,
};

this.loadState = this.loadState.bind(this);

this.loadState();
}

loadState() {
if (this.state.loaded) return;
var config = this.props.state.config;
if (!this.props.state.loaded || !config) {
setTimeout(this.loadState, 100);
// How often a timed magic word is fetched again, since it changes.
const REFRESH_MS = 10000;

// For staff, the current magic word when it's a timed one, which they can't
// otherwise know. (Staff set a text magic word themselves.)
export function MagicWordDisplay({ state }) {
const [magicWord, setMagicWord] = React.useState(undefined);
const timed =
state.loaded &&
isStaff(state) &&
state.config.queue_magic_word_mode === "timed_numeric";

React.useEffect(() => {
if (!timed) {
return;
}
if (isStaff(this.props.state) && config.queue_magic_word_mode !== "none") {
if (this.state.refreshInterval) {
clearInterval(this.state.refreshInterval);
this.state.refreshInterval = null;
}
this.setState({
refreshInterval: setInterval(() => {
let mode = this.props.state.config.queue_magic_word_mode;
if (
mode !== "timed_numeric" &&
(mode !== "none" || this.state.magicWord !== undefined)
)
return;
app.makeRequest("refresh_magic_word", (res) => {
let magicWord = res.magic_word || null;
this.setState({
oldMagicWord: this.state.magicWord,
magicWord: magicWord,
});
});
}, 10000),
let stopped = false;
const refresh = () =>
app.makeRequest("refresh_magic_word", (res) => {
if (!stopped && res) {
setMagicWord(res.magic_word || null);
}
});
}
this.setState({
loaded: true,
});
}
refresh();
const interval = setInterval(refresh, REFRESH_MS);
return () => {
stopped = true;
clearInterval(interval);
// so an old word isn't shown if the timed mode comes back
setMagicWord(undefined);
};
}, [timed]);

componentWillUnmount() {
if (this.state.refreshInterval) {
clearInterval(this.state.refreshInterval);
this.state.refreshInterval = null;
}
if (!timed || magicWord === undefined) {
return false;
}

render() {
if (!this.state.loaded) {
this.loadState();
return false;
}
if (
this.props.state.config.queue_magic_word_mode === "none" ||
this.state.magicWord === undefined
) {
return false;
}

let magicWordElem = <i>Loading...</i>;
if (this.state.magicWord) {
let magicWord = this.state.magicWord;
magicWordElem = <code>{magicWord}</code>;
}

return (
<div>
<h4>Magic word: {magicWordElem}</h4>
</div>
);
}
return (
<div>
<h4>
Magic word: {magicWord ? <code>{magicWord}</code> : <i>Loading...</i>}
</h4>
</div>
);
}
18 changes: 16 additions & 2 deletions src/components/party_group_layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getGroup,
getTicket,
groupIsMine,
isGroupMissing,
isStaff,
ticketQuestion,
ticketStatus,
Expand All @@ -34,6 +35,20 @@ export function PartyGroupLayout({ state, loadGroup, socket }) {
setDescription(savedDescription);
}

// Staff can open any group's page, so load it if it isn't one of the active
// groups the page already has: once, and again whenever the connection comes
// back, since a failed request leaves the page offline. A group that doesn't
// exist stays missing.
const staff = isStaff(state);
const hasGroup = group != null;
const missing = isGroupMissing(state, groupID);
const online = !state.offline;
React.useEffect(() => {
if (staff && online && !hasGroup && !missing) {
loadGroup(groupID);
}
}, [staff, online, hasGroup, missing, groupID, loadGroup]);

// Signed-out visitors get no groups; the home page asks them to sign in.
if (state.loaded && !state.currentUser) {
return <Navigate to="/" replace />;
Expand All @@ -42,10 +57,9 @@ export function PartyGroupLayout({ state, loadGroup, socket }) {
if (!group) {
// Only staff can load a group that isn't active; students get its page
// only for an active group, or their own.
if (state.loaded && !isStaff(state)) {
if (state.loaded && (!staff || missing)) {
return <NotFound />;
}
loadGroup(groupID);
return "loading...";
}
const ticket = getTicket(state, group.ticket_id);
Expand Down
10 changes: 9 additions & 1 deletion src/components/select_picker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ export class SelectPicker extends React.Component {
this.refreshSelect();
}
refreshSelect() {
$(this.selectRef.current).selectpicker("refresh");
const select = this.selectRef.current;
$(select).selectpicker("refresh");
// bootstrap-select chooses its title placeholder when it first sets up a
// select whose option React chose, so a controlled select's value is
// chosen again.
const { value } = this.props;
if (value != null) {
$(select).selectpicker("val", String(value));
}
}
componentDidUpdate() {
this.refreshSelect();
Expand Down
30 changes: 19 additions & 11 deletions src/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,25 @@ export class Socket {
this.handlers.delete(eventType);
}

// Sends requests one at a time, in order. Returns a promise of whether this
// one succeeded, settled once it's done (after its callback, if it has one),
// since a failed request doesn't call its callback.
emit(...args) {
const processQueue = () => {
if (this.jobs.length > 0) {
const args = this.jobs[0];
this.actuallyEmit(...args)
.finally(this.jobs.shift.bind(this.jobs))
.then(processQueue);
return new Promise((resolve) => {
const processQueue = () => {
if (this.jobs.length > 0) {
const [args, done] = this.jobs[0];
this.actuallyEmit(...args)
.then(done)
.finally(this.jobs.shift.bind(this.jobs))
.then(processQueue);
}
};
this.jobs.push([args, resolve]);
if (this.jobs.length === 1) {
processQueue();
}
};
this.jobs.push(args);
if (this.jobs.length === 1) {
processQueue();
}
});
}

actuallyEmit(event, params = {}, callback = null) {
Expand All @@ -112,10 +118,12 @@ export class Socket {
if (callback != null) {
callback(action);
}
return true;
})
.catch((e) => {
console.error(e);
this.trigger("disconnect");
return false;
});
}
}
26 changes: 22 additions & 4 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ export type State = {
ticketCounts: Record<string, number> | null;
/* Ticket IDs for any tickets we are currently loading. */
loadingTickets: Set<number>;
/* Group IDs the server has no group for. */
missingGroups: Set<number>;
/* Current ticket filter. */
filter: Filter;
/* Selected queue tab. */
Expand All @@ -126,6 +128,7 @@ export const initialState: State = {
tickets: new Map(),
ticketCounts: null,
loadingTickets: new Set(),
missingGroups: new Set(),
filter: {
location_id: null,
assignment_id: null,
Expand Down Expand Up @@ -174,9 +177,10 @@ export function ticketLocation(state: State, ticket: Ticket): TicketLocation {
}

export function ticketQuestion(state: State, ticket: Ticket | Group): string {
let question = ticket.question ?? "";
if (!isNaN(Number(question))) {
question = "Q" + parseInt(question);
// A question number, such as "3", is shown as "Q3"; anything else as it is.
const question = (ticket.question ?? "").trim();
if (/^\d+$/.test(question)) {
return "Q" + parseInt(question, 10);
}
return question;
}
Expand Down Expand Up @@ -404,7 +408,21 @@ export function setGroup(state: State, group: Group): void {
state.groups.splice(state.groups.indexOf(oldGroup), 1);
}
state.groups.push(group);
state.groups.sort((group) => group.id);
state.groups.sort((a, b) => a.id - b.id);
}

/* The server's answer to loading a group by ID: the group, or else nothing or
* an error (for an ID that isn't one), so there's no such group. */
export function receiveGroup(state: State, id: number, group: Group | null) {
if (group != null && group.id === id) {
setGroup(state, group);
} else {
state.missingGroups.add(id);
}
}

export function isGroupMissing(state: State, id: number): boolean {
return state.missingGroups.has(id);
}

export function groupIsActive(group: Group): boolean {
Expand Down
Loading
Loading