Skip to content

Commit e0359d7

Browse files
authored
Merge pull request #213 from CtrlZ-FRC4096/picklist-drag-move-remove
Scouting: drag and drop, Move to… and Remove on the picklists
2 parents aed3b80 + c4895d8 commit e0359d7

9 files changed

Lines changed: 732 additions & 117 deletions

File tree

‎src/modules/scouting/MyPicklist.tsx‎

Lines changed: 68 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { useSession } from '../../core/session'
33
import { toast } from '../../core/toast'
44
import { PageHead, Panel } from '../../core/ui'
55
import {
6-
MAX_PICKLIST_NOTE, addToMyPicklist, allWeightsZero, moveOnMyPicklist, setPicklistNote, startMyPicklist, useMyPicklist, useRankedPicklist,
6+
MAX_PICKLIST_NOTE, addToMyPicklist, allWeightsZero, moveOnMyPicklist, removeFromMyPicklist, setPicklistNote, startMyPicklist, useMyPicklist, useRankedPicklist,
77
type MyPicklistRow,
88
} from './data'
9+
import { ConfirmAction, MenuAction, MoveTo, RowMenu } from './RowMenu'
910
import { TeamLink } from './TeamPage'
11+
import { useDragOrder } from './useDragOrder'
1012

1113
/** Width and height of the buttons, in pixels. Big enough to tap. */
1214
const TAP_SIZE = 44
@@ -15,47 +17,64 @@ const TAP_SIZE = 44
1517
// team database (scouting_my_picklist), so it's on all your devices and the
1618
// team picklist can average it. It still shows with no Wi-Fi, but changes
1719
// need a connection. Changes go through data.ts: startMyPicklist(),
18-
// addToMyPicklist(), moveOnMyPicklist() and setPicklistNote().
20+
// addToMyPicklist(), moveOnMyPicklist(), setPicklistNote() and
21+
// removeFromMyPicklist(). Dragging is useDragOrder.ts, and the ⋯ menu with
22+
// Move to… and Remove is RowMenu.tsx.
1923
export default function MyPicklist() {
2024
useSession()
2125
const list = useMyPicklist()
22-
// True while a change is saving, so a second tap can't move from the old order.
23-
const [busy, setBusy] = useState(false)
2426
const [announcement, setAnnouncement] = useState('')
27+
// The team being taken off the list, while that saves.
28+
const [removing, setRemoving] = useState<string | null>(null)
29+
const { items: ordered, busy, list: attachList, rowProps, handleProps, moveTo } = useDragOrder({
30+
items: list,
31+
idOf: (row) => row.id,
32+
save: (id, to) => moveOnMyPicklist(list.find((row) => row.id === id)?.team_number ?? 0, to + 1),
33+
onDone: (id, result, place) => {
34+
if (!result.ok) toast(result.error ?? 'Something went wrong. Try again.')
35+
else setAnnouncement(`Moved team ${list.find((row) => row.id === id)?.team_number} to place ${place}.`)
36+
},
37+
removing,
38+
})
2539

26-
async function move(row: MyPicklistRow, rank: number) {
27-
setBusy(true)
28-
const result = await moveOnMyPicklist(row.team_number, rank)
29-
setBusy(false)
30-
if (!result.ok) toast(result.error)
31-
else setAnnouncement(`Moved team ${row.team_number} to place ${Math.min(list.length, Math.max(1, rank))}.`)
40+
async function remove(row: MyPicklistRow) {
41+
setRemoving(row.id)
42+
const result = await removeFromMyPicklist(row.team_number)
43+
setRemoving(null)
44+
toast(result.ok ? `Took team ${row.team_number} off your list.` : result.error)
3245
}
3346

3447
return (
3548
<div className="stack">
3649
<PageHead title="My picklist">
37-
Your own order for picking teams, with a note on any team. Everyone can see it, and the team picklist averages everyone's. It's saved to the team, so changing it needs a connection.
50+
Your own order for picking teams, with a note on any team. Drag a team by the handle on the right, or use its ⋯ menu to move it to a place, add a note or take it off your list. Everyone can see your list, and the team picklist averages everyone's. It's saved to the team, so changing it needs a connection.
3851
</PageHead>
3952
<StartOrAdd list={list} />
4053
<Panel title="My list" aside={<span className="note">{list.length} {list.length === 1 ? 'team' : 'teams'}</span>}>
4154
<p className="note" style={{ position: 'absolute', width: 1, height: 1, overflow: 'hidden', clipPath: 'inset(50%)' }} role="status">{announcement}</p>
4255
{list.length === 0 ? (
4356
<p className="panel-body muted" style={{ margin: 0 }}>No teams yet. Start it from the ranked list above.</p>
4457
) : (
45-
<ol style={{ listStyle: 'none', margin: 0, padding: 0 }}>
46-
{list.map((row, i) => <Line key={row.id} row={row} place={i + 1} last={list.length} busy={busy} move={move} />)}
58+
<ol ref={attachList} className="drag-list">
59+
{ordered.map((row, i) => (
60+
<li key={row.id} {...rowProps(row.id)}>
61+
<Line row={row} place={i + 1} last={list.length} busy={busy} moveTo={(place) => moveTo(row.id, place - 1)} remove={() => void remove(row)} />
62+
<button {...handleProps(row.id, `Move team ${row.team_number}. Drag, or use the arrow keys.`)}><span aria-hidden="true">⠿</span></button>
63+
</li>
64+
))}
4765
</ol>
4866
)}
4967
</Panel>
5068
</div>
5169
)
5270
}
5371

54-
function Line(props: { row: MyPicklistRow; place: number; last: number; busy: boolean; move: (row: MyPicklistRow, rank: number) => Promise<void> }) {
72+
// One team: its place, the team and its note, then the ⋯ menu.
73+
function Line(props: { row: MyPicklistRow; place: number; last: number; busy: boolean; moveTo: (place: number) => void; remove: () => void }) {
5574
const { row, place } = props
5675
const [editing, setEditing] = useState(false)
5776
const [note, setNote] = useState(row.note ?? '')
58-
const [moveTo, setMoveTo] = useState('')
77+
const name = `Team ${row.team_number}`
5978

6079
async function saveNote() {
6180
const result = await setPicklistNote(row.team_number, note)
@@ -64,43 +83,42 @@ function Line(props: { row: MyPicklistRow; place: number; last: number; busy: bo
6483
}
6584

6685
return (
67-
<li style={{ display: 'grid', gap: 8, padding: '10px 16px', borderBottom: '1px solid var(--line)' }}>
68-
<div style={{ display: 'flex', flexWrap: 'wrap', alignItems: 'center', gap: '8px 12px' }}>
69-
<strong className="tnum" style={{ fontSize: 18, minWidth: '2ch' }}>{`${place}.`}</strong>
70-
<strong style={{ fontSize: 18 }}><TeamLink team={row.team_number} /></strong>
71-
<span style={{ display: 'flex', flexWrap: 'wrap', alignItems: 'center', gap: 8, marginLeft: 'auto' }}>
72-
<button type="button" className="btn" style={{ minWidth: TAP_SIZE, minHeight: TAP_SIZE, fontSize: 20 }}
73-
aria-label={`Move team ${row.team_number} up`} disabled={props.busy || place === 1} onClick={() => void props.move(row, place - 1)}>
74-
<span aria-hidden="true">↑</span>
75-
</button>
76-
<button type="button" className="btn" style={{ minWidth: TAP_SIZE, minHeight: TAP_SIZE, fontSize: 20 }}
77-
aria-label={`Move team ${row.team_number} down`} disabled={props.busy || place === props.last} onClick={() => void props.move(row, place + 1)}>
78-
<span aria-hidden="true">↓</span>
79-
</button>
80-
{/* Move to any place, for long moves. */}
81-
<form style={{ display: 'flex', gap: 6 }} onSubmit={(e) => { e.preventDefault(); if (moveTo) void props.move(row, Number(moveTo)).then(() => setMoveTo('')) }}>
82-
<input className="input" style={{ width: '5ch', minHeight: TAP_SIZE, fontSize: 16 }} inputMode="numeric" aria-label={`Move team ${row.team_number} to place`} placeholder="#"
83-
value={moveTo} onChange={(e) => setMoveTo(e.target.value.replace(/\D/g, ''))} />
84-
<button type="submit" className="btn" style={{ minHeight: TAP_SIZE }} disabled={props.busy || !moveTo}>Move to…</button>
86+
<>
87+
<div className="drag-row-body">
88+
<div className="drag-row-top">
89+
<strong className="tnum" style={{ fontSize: 18, minWidth: '2ch' }}>{`${place}.`}</strong>
90+
<strong style={{ fontSize: 18 }}><TeamLink team={row.team_number} /></strong>
91+
</div>
92+
{editing ? (
93+
<form style={{ display: 'grid', gap: 6 }} onSubmit={(e) => { e.preventDefault(); void saveNote() }}>
94+
<textarea className="input" style={{ fontSize: 16 }} rows={2} maxLength={MAX_PICKLIST_NOTE} aria-label={`Note on team ${row.team_number}`} autoFocus
95+
value={note} onChange={(e) => setNote(e.target.value)} placeholder="Like: great defender, slow climb" />
96+
<div className="toolbar">
97+
<button type="submit" className="btn btn-primary" style={{ minHeight: TAP_SIZE }}>Save note</button>
98+
<button type="button" className="btn" style={{ minHeight: TAP_SIZE }} onClick={() => { setNote(row.note ?? ''); setEditing(false) }}>Cancel</button>
99+
<span className="note">{`${note.trim().length} of ${MAX_PICKLIST_NOTE}`}</span>
100+
</div>
85101
</form>
86-
</span>
102+
) : (
103+
<button type="button" className="link-btn" style={{ justifySelf: 'start', minHeight: TAP_SIZE, textAlign: 'left', overflowWrap: 'anywhere' }} onClick={() => setEditing(true)}>
104+
{row.note ?? 'Add a note'}
105+
</button>
106+
)}
87107
</div>
88-
{editing ? (
89-
<form style={{ display: 'grid', gap: 6 }} onSubmit={(e) => { e.preventDefault(); void saveNote() }}>
90-
<textarea className="input" style={{ fontSize: 16 }} rows={2} maxLength={MAX_PICKLIST_NOTE} aria-label={`Note on team ${row.team_number}`}
91-
value={note} onChange={(e) => setNote(e.target.value)} placeholder="Like: great defender, slow climb" />
92-
<div className="toolbar">
93-
<button type="submit" className="btn btn-primary" style={{ minHeight: TAP_SIZE }}>Save note</button>
94-
<button type="button" className="btn" style={{ minHeight: TAP_SIZE }} onClick={() => { setNote(row.note ?? ''); setEditing(false) }}>Cancel</button>
95-
<span className="note">{`${note.trim().length} of ${MAX_PICKLIST_NOTE}`}</span>
96-
</div>
97-
</form>
98-
) : (
99-
<button type="button" className="link-btn" style={{ justifySelf: 'start', minHeight: TAP_SIZE, textAlign: 'left', overflowWrap: 'anywhere' }} onClick={() => setEditing(true)}>
100-
{row.note ?? 'Add a note'}
101-
</button>
102-
)}
103-
</li>
108+
<RowMenu label={`More options for team ${row.team_number}`}>
109+
{(close) => (
110+
<>
111+
<MoveTo name={name} place={place} count={props.last} disabled={props.busy} onMove={(to) => { close(); props.moveTo(to) }} />
112+
<MenuAction disabled={props.busy || place === 1} onClick={() => { close(); props.moveTo(1) }}>Move to top</MenuAction>
113+
<MenuAction disabled={props.busy || place === props.last} onClick={() => { close(); props.moveTo(props.last) }}>Move to bottom</MenuAction>
114+
<MenuAction onClick={() => { close(); setEditing(true) }}>{row.note ? 'Change note' : 'Add a note'}</MenuAction>
115+
<ConfirmAction label="Remove from my list" confirm="Remove" disabled={props.busy}
116+
question={row.note ? `Take team ${row.team_number} off your list? Its note goes too.` : `Take team ${row.team_number} off your list?`}
117+
onConfirm={() => { close(); props.remove() }} />
118+
</>
119+
)}
120+
</RowMenu>
121+
</>
104122
)
105123
}
106124

‎src/modules/scouting/README.md‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,18 @@ The functions are in the "Shared picklist" part of `data.ts`:
6262
| `startPicklist(teams)` | Starts an empty list with these teams, best first. Refuses if the list already has teams |
6363
| `addMissingTeams(teams)` | Adds the teams that aren't on the list to the bottom |
6464
| `moveTeam(id, -1 or 1)` | Moves a team one place up or down. Nothing happens at the top or bottom |
65+
| `moveTeamTo(id, place)` | Moves a team to any place (1 is the top), for dragging and Move to…. Past either end means the top or bottom |
6566
| `setPickStatus(id, status)` | Marks a team Open, Picked (on another alliance) or Do not pick. It keeps its place |
67+
| `removeTeam(id)` | Takes a team off the list for everyone. The others keep their order, and only its row changes |
6668

67-
Each team has a `position`, smallest at the top, starting 1000 apart. A move changes only the moved team's position, to halfway between its new neighbours (`positionBetween()`), so two people moving different teams at once don't undo each other. If the halves get too close to tell apart, `moveTeam()` numbers the list 1000, 2000, … again. Only `position` and `status` can be changed, so not even a mentor can turn a row into a different team or event.
69+
Each team has a `position`, smallest at the top, starting 1000 apart. A move changes only the moved team's position, to halfway between its new neighbours (`positionBetween()`), so two people moving different teams at once don't undo each other. If the halves get too close to tell apart, `moveTeamTo()` numbers the list 1000, 2000, … again. Only `position` and `status` can be changed, so not even a mentor can turn a row into a different team or event.
6870

6971
The page is `SharedPicklist.tsx`. Everyone signed in sees the list: rank, team (tap it for the team page) and status. Picked and Do not pick teams stay in their place, greyed out, and **Hide picked** hides the Picked ones. Student leaders, mentors and admins also see:
7072

7173
- **Start from the ranked list** when the list is empty: it starts with the teams on the Picklist page, in the order the weights on their phone give (`useRankedPicklist()`).
7274
- **Add teams not on the list** when the ranked list has teams the picklist doesn't.
73-
- Big up and down buttons on each team, which wait while a move saves, and an Open / Picked / Do not pick choice. Screen readers hear "Moved team 9991 to place 2."
75+
- A drag handle (⠿) at the right of each team, and an Open / Picked / Do not pick choice beside it. See "Dragging and Move to…" below.
76+
- A ⋯ menu on each team with **Move to…** (type a place, then **Move**), **Move to top**, **Move to bottom** and **Remove from the picklist**. Remove asks first, in the menu, and suggests Do not pick for a team that should keep its place. A removed team counts as "not on the list", so **Add teams not on the list** puts it back at the bottom.
7477

7578
Hiding the buttons is only tidiness: the table's rules are what stop anyone else changing the list.
7679

@@ -308,8 +311,11 @@ Everyone signed in can make their own picklist for the event
308311
(`MyPicklist.tsx`, at `#/scouting/my-picklist`): **Start my list** fills it
309312
from the Picklist page's order with this phone's weights, **Add teams not
310313
on my list** adds teams that joined later, the up and down buttons move a
311-
team one place and **Move to…** moves it to any place. Each team can have a
312-
note of up to 500 characters.
314+
team by its handle (⠿), and the ⋯ menu has **Move to…**, **Move to top**,
315+
**Move to bottom**, **Add a note** and **Remove from my list** (it asks
316+
first; the note goes too, and **Add teams not on my list** would add the
317+
team back). Each team can have a note of up to 500 characters. See
318+
"Dragging and Move to…" below.
313319

314320
The team picklist (`TeamPicklist.tsx`, at `#/scouting/team-picklist`)
315321
averages everyone's lists: each team's average place, its best and worst,
@@ -334,7 +340,9 @@ and say so.
334340
| `startMyPicklist(teams)` | Starts an empty list in this order. Refuses if it's started |
335341
| `addToMyPicklist(teams)` | Adds the missing teams to the bottom |
336342
| `moveOnMyPicklist(team, place)` | Moves a team to a place; the teams between shift one place |
343+
| `checkPlace(text, count)` | Reads a place typed into Move to…: a whole number from 1 to `count`, or an error saying which places there are |
337344
| `setPicklistNote(team, note)` | Adds, changes or clears (with '') a note |
345+
| `removeFromMyPicklist(team)` | Takes a team and its note off your list. The teams below move up a place, so a team added later goes last |
338346
| `teamPicklist(rows)`, `useTeamPicklist()` | Everyone's lists averaged, as above |
339347

340348
Questions for the strategy team (from #151): should every list count the
@@ -382,3 +390,29 @@ makes it in the team database, with its permission rules and tests; see
382390
one step.
383391

384392
**Works with:** Robot status (can show entries waiting to sync).
393+
394+
## Dragging and Move to…
395+
396+
My picklist and the Final picklist use the same pieces:
397+
398+
- `useDragOrder.ts` does the dragging. It's based on the pit checklist's
399+
`usePitDrag.ts` in the status module (a module can't import another
400+
module's files, so it's a copy; a mentor could move one into `src/core/`).
401+
Drag a team by the ⠿ handle on the right with a finger or mouse: the
402+
other teams slide out of the way, and the page scrolls near the top or
403+
bottom edge. Let go outside the list to cancel. With a keyboard, focus the
404+
handle and press Up or Down to move one place, or Home or End for the top
405+
or bottom. Escape cancels a drag.
406+
- The new order shows as soon as you let go, and stays on screen while it
407+
saves. If saving fails, the list goes back and a message says why. Only
408+
one move saves at a time.
409+
- When the order changes, for any reason (another phone, too), teams slide
410+
to their new places. A team you moved lights up for a moment and scrolls
411+
into view if it went off screen. With "reduce motion" turned on in the
412+
phone's settings, nothing slides.
413+
- `RowMenu.tsx` is the ⋯ menu (it closes when you tap outside it or press
414+
Escape), its **Move to…** dropdown, which checks the place with
415+
`checkPlace()`, and `ConfirmAction`, the red Remove at the bottom that
416+
asks before it does anything. A team being removed fades while it saves,
417+
moves wait until it's done, and then the teams below slide up. The
418+
styles are `lists.css`.

0 commit comments

Comments
 (0)