Skip to content

Commit

Permalink
fix: Do not highlight and widen original route shape after the detour…
Browse files Browse the repository at this point in the history
… is finished (#2434)
  • Loading branch information
joshlarson authored Feb 29, 2024
1 parent 07f5d1c commit 059dfad
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 14 deletions.
40 changes: 26 additions & 14 deletions assets/src/components/detours/detourMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ interface DetourMapProps {
*/
waypoints: ShapePoint[]

/**
* User signal to show whether the original shape should be
* highlighted
*/
originalShapeClickable: boolean

/**
* Callback fired when the {@link originalShape} is clicked.
*/
Expand Down Expand Up @@ -79,6 +85,7 @@ export const DetourMap = ({
endPoint,
waypoints,

originalShapeClickable,
onClickOriginalShape,
onClickMap,

Expand Down Expand Up @@ -133,6 +140,7 @@ export const DetourMap = ({
? ["c-detour_map--original-route-shape__unstarted"]
: []
}
clickable={originalShapeClickable}
onClick={(e) => {
const { position } =
closestPosition(
Expand Down Expand Up @@ -211,13 +219,15 @@ interface OriginalRouteShapeProps extends PropsWithChildren {
key: string
positions: LatLngLiteral[]
classNames: string[]
clickable: boolean
onClick: (e: LeafletMouseEvent) => void
}

const OriginalRouteShape = ({
positions,
children,
classNames,
clickable,
onClick,
}: OriginalRouteShapeProps) => (
<>
Expand All @@ -226,19 +236,21 @@ const OriginalRouteShape = ({
positions={positions}
className="c-detour_map--original-route-shape-core"
/>
<Polyline
positions={positions}
weight={16}
className={joinClasses([
"c-detour_map--original-route-shape",
...classNames,
])}
bubblingMouseEvents={false}
eventHandlers={{
click: onClick,
}}
>
{children}
</Polyline>
{clickable && (
<Polyline
positions={positions}
weight={16}
className={joinClasses([
"c-detour_map--original-route-shape",
...classNames,
])}
bubblingMouseEvents={false}
eventHandlers={{
click: onClick,
}}
>
{children}
</Polyline>
)}
</>
)
2 changes: 2 additions & 0 deletions assets/src/components/detours/diversionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const DiversionPage = ({
addConnectionPoint,
addWaypoint,

canAddPoints,
startPoint,
endPoint,
waypoints,
Expand Down Expand Up @@ -92,6 +93,7 @@ export const DiversionPage = ({
startPoint={startPoint ?? undefined}
endPoint={endPoint ?? undefined}
waypoints={waypoints}
originalShapeClickable={canAddPoints}
onClickMap={addWaypoint}
onClickOriginalShape={addConnectionPoint}
undoDisabled={canUndo === false}
Expand Down
5 changes: 5 additions & 0 deletions assets/src/hooks/useDetour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const useDetour = (routePatternId: RoutePatternId) => {
}
}

const canAddPoints = endPoint === null
const canUndo = startPoint !== null && state === DetourState.Edit

const undo = () => {
Expand Down Expand Up @@ -137,6 +138,10 @@ export const useDetour = (routePatternId: RoutePatternId) => {
*/
addConnectionPoint,

/**
* Reports whether it's possible to add points to the detour.
*/
canAddPoints,
/**
* The starting connection point of the detour.
*/
Expand Down
27 changes: 27 additions & 0 deletions assets/tests/components/detours/detourMap.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const DetourMapWithDefaults = (
endPoint={undefined}
waypoints={[]}
undoDisabled={false}
originalShapeClickable={true}
onClickMap={() => {}}
onClickOriginalShape={() => {}}
onUndo={() => {}}
Expand Down Expand Up @@ -170,4 +171,30 @@ describe("DetourMap", () => {

expect(undoButton).toBeDisabled()
})

test("when `originalShapeClickable` is true, there should be two route shape elements", () => {
const { container } = render(
<DetourMapWithDefaults originalShapeClickable={true} />
)

expect(
container.querySelector(".c-detour_map--original-route-shape-core")
).toBeInTheDocument()
expect(
container.querySelector(".c-detour_map--original-route-shape")
).toBeInTheDocument()
})

test("when `originalShapeClickable` is false, there should be only be one (non-clickable) route shape element", () => {
const { container } = render(
<DetourMapWithDefaults originalShapeClickable={false} />
)

expect(
container.querySelector(".c-detour_map--original-route-shape-core")
).toBeInTheDocument()
expect(
container.querySelector(".c-detour_map--original-route-shape")
).not.toBeInTheDocument()
})
})
34 changes: 34 additions & 0 deletions assets/tests/hooks/useDetour.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,40 @@ describe("useDetour", () => {
expect(result.current.canUndo).toBe(true)
})

test("when `startPoint` is null, `canAddPoints` is `true`", async () => {
const { result } = renderHook(useDetourWithFakeRoutePattern)

expect(result.current.startPoint).toBeNull()
expect(result.current.canAddPoints).toBe(true)
})

test("when `startPoint` is set, `canAddPoints` is `true`", async () => {
const { result } = renderHook(useDetourWithFakeRoutePattern)

act(() => result.current.addConnectionPoint({ lat: 0, lon: 0 }))

expect(result.current.startPoint).not.toBeNull()
expect(result.current.canAddPoints).toBe(true)
})

test("when `endPoint` is set, `canAddPoints` is `false`", async () => {
const { result } = renderHook(useDetourWithFakeRoutePattern)

jest
.mocked(fetchDetourMissedStops)
.mockResolvedValue(stopFactory.buildList(3))

act(() => result.current.addConnectionPoint({ lat: 0, lon: 0 }))
act(() => result.current.addConnectionPoint({ lat: 0, lon: 0 }))

await waitFor(() => {
expect(result.current.missedStops).not.toBeUndefined()
})

expect(result.current.endPoint).not.toBeNull()
expect(result.current.canAddPoints).toBe(false)
})

test("when `endPoint` is set, `missedStops` is filled in", async () => {
const { result } = renderHook(useDetourWithFakeRoutePattern)

Expand Down

0 comments on commit 059dfad

Please sign in to comment.