Skip to content

Commit

Permalink
feat: Move detour text creation to state machine; allow edits
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahpurcell committed Oct 17, 2024
1 parent b0d3ffa commit 259cb9f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 58 deletions.
57 changes: 8 additions & 49 deletions assets/src/components/detours/diversionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, {
ComponentPropsWithoutRef,
PropsWithChildren,
useContext,
useEffect,
useState,
} from "react"
import { DrawDetourPanel } from "./detourPanels/drawDetourPanel"
Expand Down Expand Up @@ -90,7 +89,6 @@ export const DiversionPage = ({
detourShape,
directions,
routingError,
nearestIntersection,

unfinishedRouteSegments,

Expand All @@ -113,16 +111,7 @@ export const DiversionPage = ({
: { input: useDetourProps.originalRoute }
)

const [textArea, setTextArea] = useState("")

const nearestIntersectionDirection = [
{ instruction: "From " + nearestIntersection },
]
const extendedDirections = directions
? nearestIntersectionDirection.concat(directions)
: undefined

const { route, routePattern, routePatterns } = snapshot.context
const { route, routePattern, routePatterns, editedDetourText } = snapshot.context
const routePatternsById = Object.fromEntries(
routePatterns?.map((rp) => [rp.id, rp]) ?? []
)
Expand All @@ -137,36 +126,6 @@ export const DiversionPage = ({
? displayFieldsFromRouteAndPattern(route, routePattern)
: {}

useEffect(() => {
if (snapshot.matches({ "Detour Drawing": "Share Detour" })) {
setTextArea(
[
`Detour ${routeName} ${routeDirection}`,
routeOrigin,
,
"Connection Points:",
connectionPoints?.start?.name ?? "N/A",
connectionPoints?.end?.name ?? "N/A",
,
`Missed Stops (${missedStops?.length}):`,
...(missedStops?.map(({ name }) => name) ?? ["no stops"]),
,
"Turn-by-Turn Directions:",
...(extendedDirections?.map((v) => v.instruction) ?? []),
].join("\n")
)
}
}, [
snapshot,
routeName,
routeDirection,
routeOrigin,
extendedDirections,
missedStops,
connectionPoints?.start?.name,
connectionPoints?.end?.name,
])

const routes = useContext(RoutesContext)
const epochNowInSeconds = useCurrentTimeSeconds()

Expand Down Expand Up @@ -236,7 +195,7 @@ export const DiversionPage = ({
) {
return (
<DrawDetourPanel
directions={extendedDirections}
directions={directions}
missedStops={missedStops}
routeName={routeName ?? "??"}
routeDescription={routeDescription ?? "??"}
Expand All @@ -254,8 +213,8 @@ export const DiversionPage = ({
return (
<DetourFinishedPanel
onNavigateBack={editDetour}
detourText={textArea}
onChangeDetourText={setTextArea}
detourText={editedDetourText || ""}
onChangeDetourText={(detourText: string) => send({type: "detour.share.edit-directions", detourText})}
onActivateDetour={
inTestGroup(TestGroups.DetoursList)
? () => {
Expand Down Expand Up @@ -339,8 +298,8 @@ export const DiversionPage = ({
} else if (snapshot.matches({ "Detour Drawing": "Active" })) {
return (
<ActiveDetourPanel
detourText="Hello World"
directions={extendedDirections}
detourText={editedDetourText || ""}
directions={directions}
connectionPoints={[
connectionPoints?.start?.name ?? "N/A",
connectionPoints?.end?.name ?? "N/A",
Expand Down Expand Up @@ -380,8 +339,8 @@ export const DiversionPage = ({
} else if (snapshot.matches({ "Detour Drawing": "Past" })) {
return (
<PastDetourPanel
detourText="Hello World"
directions={extendedDirections}
detourText={editedDetourText || ""}
directions={directions}
connectionPoints={[
connectionPoints?.start?.name ?? "N/A",
connectionPoints?.end?.name ?? "N/A",
Expand Down
49 changes: 41 additions & 8 deletions assets/src/models/createDetourMachine.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { setup, assign, fromPromise, ActorLogicFrom, InputFrom } from "xstate"
import { RoutePatternId, ShapePoint } from "../schedule"
import { Route, RouteId, RoutePattern } from "../schedule"
import { Ok, Result } from "../util/result"
import { isOk, Ok, Result } from "../util/result"
import {
FetchDetourDirectionsError,
fetchDetourDirections,
Expand Down Expand Up @@ -30,6 +30,8 @@ export const createDetourMachine = setup({

finishedDetour: FinishedDetour | undefined | null

editedDetourText?: string

selectedDuration?: string
selectedReason?: string
},
Expand Down Expand Up @@ -66,6 +68,7 @@ export const createDetourMachine = setup({
| { type: "detour.edit.place-waypoint-on-route"; location: ShapePoint }
| { type: "detour.edit.place-waypoint"; location: ShapePoint }
| { type: "detour.edit.undo" }
| { type: "detour.share.edit-directions"; detourText: string }
| { type: "detour.share.copy-detour"; detourText: string }
| { type: "detour.share.open-activate-modal" }
| {
Expand Down Expand Up @@ -133,7 +136,7 @@ export const createDetourMachine = setup({
throw "Missing detour direction inputs"
}
if (points.length < 2) {
return Ok({ coordinates: [], directions: undefined })
return Ok({ coordinates: [], directions: [] })
}
return fetchDetourDirections(points)
}),
Expand Down Expand Up @@ -439,15 +442,15 @@ export const createDetourMachine = setup({
onDone: {
actions: assign({
finishedDetour: ({ event }) => event.output,
detourShape: ({ event }) =>
detourShape: ({ context, event }) =>
event.output?.detourShape &&
Ok({
...event.output.detourShape,
directions: event.output.detourShape.directions?.concat(
{
instruction: "Regular Route",
}
),
directions: [
{ instruction: "From " + context.nearestIntersection },
...event.output.detourShape.directions,
{ instruction: "Regular Route" }
]
}),
}),
},
Expand All @@ -472,6 +475,30 @@ export const createDetourMachine = setup({

onDone: {
target: "Share Detour",
actions: assign({
editedDetourText: ({context}) => {
const routeName = context.route?.name
const routeDirection = context.routePattern && context.route?.directionNames[context.routePattern.directionId]
const routeOrigin = context.routePattern?.name
const missedStops = context.finishedDetour?.missedStops

const detourShape = context.detourShape && isOk(context.detourShape) ? context.detourShape.ok : null

return [
`Detour ${routeName} ${routeDirection}`,
routeOrigin,
,
"Connection Points:",
context.finishedDetour?.connectionPoint?.start?.name ?? "N/A",
context.finishedDetour?.connectionPoint?.end?.name ?? "N/A",
,
`Missed Stops (${missedStops?.length}):`,
...(missedStops?.map(({ name }) => name) ?? ["no stops"]),
,
"Turn-by-Turn Directions:",
...(detourShape?.directions.map((v) => v.instruction) ?? []),
].join("\n")
}})
},
},
"Share Detour": {
Expand All @@ -490,6 +517,12 @@ export const createDetourMachine = setup({
"detour.share.open-activate-modal": {
target: "Activating",
},
"detour.share.edit-directions": {
target: "Reviewing",
actions: assign({
editedDetourText: ({ event }) => event.detourText,
}),
}
},
},
Activating: {
Expand Down
2 changes: 1 addition & 1 deletion assets/src/models/detour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const detourStateFromData = (

export interface DetourShape {
coordinates: ShapePoint[]
directions?: DetourDirection[]
directions: DetourDirection[]
}

export type DetourDirection = {
Expand Down

0 comments on commit 259cb9f

Please sign in to comment.