Skip to content

Commit

Permalink
Hardcoded shuttle runs (#193)
Browse files Browse the repository at this point in the history
* helpers/array.ts uniq
* known shuttles list
  • Loading branch information
skyqrose authored Sep 13, 2019
1 parent 2f9e060 commit 0fb74f0
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 47 deletions.
9 changes: 8 additions & 1 deletion assets/css/_route_picker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@
}

.m-route-picker__route-list-button {
@include button-light;
margin-bottom: 0.25rem;
width: 100%;
word-break: break-word;

&--selected {
@include button-primary;
}

&--unselected {
@include button-light;
}

&--disabled {
@include button-disabled;
}
}
29 changes: 24 additions & 5 deletions assets/css/_ui_kit.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ $color-secondary-dark: #4db6ac;
$color-secondary-medium: #b8dbd9;
$color-secondary-light: #d4e7e7;

$color-button-simple: $color-component-light;
$color-button-primary: $color-primary;
$color-button-secondary: $color-secondary-dark;
$color-button-light: $color-bg-base;
$color-button-disabled: #e6e8ec;
$color-button-focus: $color-secondary-medium;

$color-font-dark: $color-component-dark;
$color-font-grey: $color-component-medium;
$color-font-link: $color-secondary-dark;
Expand Down Expand Up @@ -113,23 +120,23 @@ $color-line-gapped-light: #75e8ff;

&:focus,
&:hover {
background-color: $color-secondary-medium;
background-color: $color-button-focus;
}
}

@mixin button-simple {
@include button-base;
background-color: $color-component-light;
background-color: $color-button-simple;
}

@mixin button-light {
@include button-base;
background-color: $color-bg-base;
background-color: $color-button-light;
}

@mixin button-primary {
@include button-base;
background-color: $color-primary;
background-color: $color-button-primary;
color: $color-font-light;

&:focus,
Expand All @@ -140,7 +147,19 @@ $color-line-gapped-light: #75e8ff;

@mixin button-secondary {
@include button-base;
background-color: $color-secondary-dark;
background-color: $color-button-secondary;
}

@mixin button-disabled {
@include button-base;
background-color: $color-button-disabled;
color: $color-font-grey;
cursor: default;

&:focus,
&:hover {
background-color: $color-button-disabled;
}
}

@mixin button-icon($size) {
Expand Down
2 changes: 1 addition & 1 deletion assets/src/components/routePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const RouteListButton = ({
const [, dispatch] = useContext(StateDispatchContext)
const selectedClass = isSelected
? "m-route-picker__route-list-button--selected"
: ""
: "m-route-picker__route-list-button--unselected"
const clickHandler = isSelected
? () => dispatch(deselectRoute(route.id))
: () => dispatch(selectRoute(route.id))
Expand Down
99 changes: 81 additions & 18 deletions assets/src/components/shuttlePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,115 @@
import React, { ReactElement, useContext } from "react"
import { ShuttleVehiclesContext } from "../contexts/shuttleVehiclesContext"
import { StateDispatchContext } from "../contexts/stateDispatchContext"
import { Vehicle } from "../realtime"
import { uniq } from "../helpers/array"
import { RunId, Vehicle } from "../realtime"
import { deselectShuttleRun, selectShuttleRun } from "../state"

interface KnownShuttle {
name: string
runId: RunId
}

const KNOWN_SHUTTLES: KnownShuttle[] = [
{
name: "Special",
runId: "999-0555",
},
{
name: "Blue",
runId: "999-0501",
},
{
name: "Green",
runId: "999-0502",
},
{
name: "Orange",
runId: "999-0503",
},
{
name: "Red",
runId: "999-0504",
},
{
name: "Commuter Rail",
runId: "999-0505",
},
]

const KNOWN_RUN_IDS: RunId[] = KNOWN_SHUTTLES.map(
knownShuttle => knownShuttle.runId
)

const ShuttlePicker = ({}): ReactElement<HTMLDivElement> => {
const shuttles = useContext(ShuttleVehiclesContext)
const shuttles: Vehicle[] = useContext(ShuttleVehiclesContext)
const activeRunIds: RunId[] = uniq(shuttles
.map(v => v.runId)
.filter(runId => runId !== null) as RunId[])

return (
<div className="m-route-picker">
<div className="m-route-picker__label">Run #</div>
<ul className="m-route-picker__route-list">
{renderRunIdButtons(shuttles)}
{KNOWN_SHUTTLES.map(knownShuttle => (
<RunIdButton
key={knownShuttle.runId}
name={`${knownShuttle.name} ${formatRunId(knownShuttle.runId)}`}
runId={knownShuttle.runId}
isActive={activeRunIds.includes(knownShuttle.runId)}
/>
))}
{activeRunIds.map(runId =>
KNOWN_RUN_IDS.includes(runId) ? null : (
<RunIdButton
key={runId}
name={formatRunId(runId)}
runId={runId}
isActive={true}
/>
)
)}
</ul>
</div>
)
}

const renderRunIdButtons = (
shuttles: Vehicle[]
): Array<ReactElement<HTMLLIElement>> =>
Array.from(new Set(shuttles.map(v => v.runId).sort())).map(runId => (
<RunIdButton key={runId!} runId={runId!} />
))

const RunIdButton = ({
name,
runId,
isActive,
}: {
runId: string
name: string
runId: RunId
isActive: boolean
}): ReactElement<HTMLLIElement> => {
const [state, dispatch] = useContext(StateDispatchContext)
const isSelected = state.selectedShuttleRunIds.includes(runId)
const selectedClass = isSelected
? "m-route-picker__route-list-button--selected"
: ""
const selectedClass = isActive
? isSelected
? "m-route-picker__route-list-button--selected"
: "m-route-picker__route-list-button--unselected"
: "m-route-picker__route-list-button--disabled"

const onClick = isSelected
? () => dispatch(deselectShuttleRun(runId))
: () => dispatch(selectShuttleRun(runId))
const onClick = isActive
? isSelected
? () => dispatch(deselectShuttleRun(runId))
: () => dispatch(selectShuttleRun(runId))
: // tslint:disable-next-line: no-empty
() => {}
return (
<li>
<button
className={`m-route-picker__route-list-button ${selectedClass}`}
onClick={onClick}
disabled={!isActive}
>
{runId}
{name}
</button>
</li>
)
}

export const formatRunId = (runId: RunId): string => runId.replace(/-0*/, " ")

export default ShuttlePicker
2 changes: 2 additions & 0 deletions assets/src/helpers/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export function partition<T>(items: T[], testFn: (value: T) => boolean): T[][] {
[[] as T[], [] as T[]]
)
}

export const uniq = <T>(array: T[]): T[] => Array.from(new Set(array)).sort()
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ exports[`LadderPage renders with routes 1`] = `
</li>
<li>
<button
className="m-route-picker__route-list-button "
className="m-route-picker__route-list-button m-route-picker__route-list-button--unselected"
onClick={[Function]}
>
28
Expand Down Expand Up @@ -497,15 +497,15 @@ exports[`LadderPage renders with timepoints 1`] = `
>
<li>
<button
className="m-route-picker__route-list-button "
className="m-route-picker__route-list-button m-route-picker__route-list-button--unselected"
onClick={[Function]}
>
1
</button>
</li>
<li>
<button
className="m-route-picker__route-list-button "
className="m-route-picker__route-list-button m-route-picker__route-list-button--unselected"
onClick={[Function]}
>
28
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,23 @@ exports[`RoutePicker renders a list of routes 1`] = `
</li>
<li>
<button
className="m-route-picker__route-list-button "
className="m-route-picker__route-list-button m-route-picker__route-list-button--unselected"
onClick={[Function]}
>
71
</button>
</li>
<li>
<button
className="m-route-picker__route-list-button "
className="m-route-picker__route-list-button m-route-picker__route-list-button--unselected"
onClick={[Function]}
>
73
</button>
</li>
<li>
<button
className="m-route-picker__route-list-button "
className="m-route-picker__route-list-button m-route-picker__route-list-button--unselected"
onClick={[Function]}
>
111
Expand Down
Loading

0 comments on commit 0fb74f0

Please sign in to comment.