mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 06:37:33 +00:00
feat(roadtrip): edit a stop's place from its phone sheet, title centred on the disc
The stop sheet had no way into the place, so a fixed time could not be set from the road. A pencil in the header now hands over to the place editor the plan tab uses, with this visit in context, so Start (the time the chain anchors on) can be set or cleared there. It shows with place_edit, once the place row resolves from the whole trip store and the visit carries a server id. No second writer for the time is added. Opening the editor on a booked night or a service stop the day lists hide used to miss the visit, prefill Start from the pool place and write that over the pin on a plain save, dropping the note too. MPlaceEditSheet now resolves the visit from the stored assignments. The header row is centred, so a one line name no longer hangs from the top of the 34px disc.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { useEffect, useMemo, useRef, useState, type ReactNode, type Ref } from 'react'
|
||||
import {
|
||||
AlertTriangle, Clock, Footprints, Fuel, Hourglass, MapPin, Moon, Navigation, X,
|
||||
AlertTriangle, Clock, Footprints, Fuel, Hourglass, MapPin, Moon, Navigation, Pencil, X,
|
||||
} from 'lucide-react'
|
||||
import MSheet from '../../../components/MSheet'
|
||||
import MIconBtn from '../../../components/MIconBtn'
|
||||
@@ -9,6 +9,7 @@ import MToggle from '../../../components/MToggle'
|
||||
import type { MTripSheetsProps } from '../MTripShell'
|
||||
import { useTranslation } from '../../../../i18n'
|
||||
import { useSettingsStore } from '../../../../store/settingsStore'
|
||||
import { useTripStore } from '../../../../store/tripStore'
|
||||
import ChargingInfo from '../../../../components/Roadtrip/ChargingInfo'
|
||||
import { formatDurationShort } from '../../../../components/Roadtrip/roadtripModel'
|
||||
import { roadtripRows, stageOf, type StopRow } from '../../../../components/Roadtrip/roadtripRowModel'
|
||||
@@ -34,6 +35,13 @@ import type { RoadtripDay, ScheduleWarning } from '@trek/shared/roadtrip'
|
||||
* confirmation, simply absent, because a dead control on a phone is a promise the screen
|
||||
* cannot keep. What is left are the three things a passenger does decide on the road:
|
||||
* navigate there, stay longer, stop for the night here.
|
||||
*
|
||||
* Editing the place is the one way out of that rule, and it is a hand-over rather than a
|
||||
* control of this sheet: the pencil opens the place editor the plan tab opens, with this
|
||||
* very visit in context, and this sheet closes behind it. The time somebody fixed for a
|
||||
* stop is that editor's start time (the chain anchors on the visit's own start time), so
|
||||
* a time control here would be a second writer for one number. A booked night can also
|
||||
* be held by its check-in, which belongs to the booking and not to the place.
|
||||
*/
|
||||
|
||||
interface RtStopSheetPayload {
|
||||
@@ -132,6 +140,7 @@ export default function MRtStopSheet({ planner, shell }: MTripSheetsProps) {
|
||||
const timeFormat = useSettingsStore(s => s.settings.time_format) || '24h'
|
||||
const unitSetting = useSettingsStore(s => s.settings.distance_unit)
|
||||
const unit: DistanceUnit = unitSetting === 'imperial' ? 'imperial' : 'metric'
|
||||
const tripPlaces = useTripStore(s => s.places)
|
||||
|
||||
const days = planner.roadtripRoutes.days
|
||||
const live = useMemo(
|
||||
@@ -155,7 +164,10 @@ export default function MRtStopSheet({ planner, shell }: MTripSheetsProps) {
|
||||
useEffect(() => { setPending(null) }, [payload.assignmentId])
|
||||
|
||||
const stop = located?.row.stop ?? null
|
||||
const place = planner.places.find(p => p.id === stop?.placeId) ?? null
|
||||
// From the whole trip store, not planner.places: with service stops hidden from the day
|
||||
// lists the phone filters them out of that list, while the stage still draws them, and
|
||||
// both the pencil and the directions have to reach the row the chain drew.
|
||||
const place = tripPlaces.find(p => p.id === stop?.placeId) ?? null
|
||||
|
||||
/**
|
||||
* Addressed by stop, not by selection.
|
||||
@@ -210,11 +222,22 @@ export default function MRtStopSheet({ planner, shell }: MTripSheetsProps) {
|
||||
}
|
||||
|
||||
const stayMinutes = stop.dwellMinutes
|
||||
const canEditStay = planner.can('place_edit', planner.trip)
|
||||
const canEditPlace = planner.can('place_edit', planner.trip)
|
||||
const editStay = () => {
|
||||
shell.openSheet('rtstay', { placeId: stop.placeId, minutes: stayMinutes })
|
||||
}
|
||||
|
||||
// A negative id is the optimistic row of a stop still being saved. The editor would
|
||||
// write the time against an id the server never issued, and handing it null instead is
|
||||
// no way round that: the planner then falls back to the place's only visit, which can be
|
||||
// that same row. So the pencil waits until the stop carries the id the server gave it.
|
||||
const canOpenEditor = canEditPlace && place != null && stop.assignmentId > 0
|
||||
const editPlace = () => {
|
||||
if (!place) return
|
||||
planner.openPlaceEditor(place, stop.assignmentId)
|
||||
shell.closeSheet()
|
||||
}
|
||||
|
||||
const toggleEndDay = async (next: boolean) => {
|
||||
if (!canEndDay || saving) return
|
||||
setPending({ from: endDayTruth, to: next })
|
||||
@@ -239,9 +262,12 @@ export default function MRtStopSheet({ planner, shell }: MTripSheetsProps) {
|
||||
|
||||
return (
|
||||
<MSheet open={open && !!live} onClose={shell.closeSheet} variant="bottom" material="opaque" ariaLabel={stop.name}>
|
||||
{/* ── Header: the disc the chain draws, the name, the way out ── */}
|
||||
{/* ── Header: the disc the chain draws, the name, the way to the editor and the way out ── */}
|
||||
<div className="flex-none px-[18px] pt-4">
|
||||
<div className="flex items-start gap-3">
|
||||
{/* Centred, not top-aligned: one line of the name is shorter than the 34px disc, so
|
||||
hung from the top it sat high beside it. A name that wraps to its two lines grows
|
||||
the row, and the disc and the buttons centre on both, as they do in the chain row. */}
|
||||
<div className="flex items-center gap-3">
|
||||
{row.service ? (
|
||||
kind && KindIcon ? (
|
||||
<span
|
||||
@@ -267,9 +293,16 @@ export default function MRtStopSheet({ planner, shell }: MTripSheetsProps) {
|
||||
<h2 className="min-w-0 flex-1 line-clamp-2 text-[1.0625rem] font-bold leading-tight text-m-ink">
|
||||
{stop.name}
|
||||
</h2>
|
||||
<MIconBtn variant="neutral" size={34} onClick={shell.closeSheet} ariaLabel={t('common.close')}>
|
||||
<X size={15} strokeWidth={2.2} />
|
||||
</MIconBtn>
|
||||
<div className="flex flex-none items-center gap-2">
|
||||
{canOpenEditor && (
|
||||
<MIconBtn variant="neutral" size={34} onClick={editPlace} ariaLabel={t('common.edit')}>
|
||||
<Pencil size={15} strokeWidth={2.2} />
|
||||
</MIconBtn>
|
||||
)}
|
||||
<MIconBtn variant="neutral" size={34} onClick={shell.closeSheet} ariaLabel={t('common.close')}>
|
||||
<X size={15} strokeWidth={2.2} />
|
||||
</MIconBtn>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -347,7 +380,7 @@ export default function MRtStopSheet({ planner, shell }: MTripSheetsProps) {
|
||||
icon={<Clock size={16} strokeWidth={2} />}
|
||||
label={t('roadtrip.stop.stayShort')}
|
||||
value={stayMinutes ? formatDurationShort(stayMinutes * 60) : t('roadtrip.stay.none')}
|
||||
onClick={canEditStay ? editStay : undefined}
|
||||
onClick={canEditPlace ? editStay : undefined}
|
||||
/>
|
||||
</div>
|
||||
{navOpen && (
|
||||
|
||||
@@ -14,7 +14,7 @@ import PlPlaceSearch, { type PlSearchPick } from './PlPlaceSearch'
|
||||
import PlCategoryPicker from './PlCategoryPicker'
|
||||
import PlTimeFields from './PlTimeFields'
|
||||
import PlFileAttach from './PlFileAttach'
|
||||
import type { Place } from '../../../../types'
|
||||
import type { Assignment, AssignmentsMap, Place } from '../../../../types'
|
||||
import type { TripPlanner } from '../MTripShell'
|
||||
import { useLocationBias } from '../../../../hooks/useLocationBias'
|
||||
|
||||
@@ -48,6 +48,21 @@ function findDuplicateName(
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* The visit the editor was opened on, looked up in the planner's STORED list.
|
||||
*
|
||||
* Not planner.assignments: the phone filters booked nights (always) and service stops
|
||||
* (while the day lists hide them) out of that one, and the road trip stop sheet opens
|
||||
* this editor on exactly those visits. Looked up there the visit was not found, Start
|
||||
* prefilled from the pool place, and a plain save wrote that over the time the stop was
|
||||
* pinned to; the day note was dropped the same way. Every visit the plan tab shows is in
|
||||
* the stored list as well, so its entry points resolve exactly as they did.
|
||||
*/
|
||||
function findVisit(stored: AssignmentsMap, assignmentId: number | null): Assignment | null {
|
||||
if (!assignmentId) return null
|
||||
return Object.values(stored).flat().find(a => a.id === assignmentId) ?? null
|
||||
}
|
||||
|
||||
/**
|
||||
* Add/edit place sheet — the mobile counterpart of PlaceFormModal, driven by
|
||||
* the planner's own editor flags (showPlaceForm / editingPlace / prefillCoords /
|
||||
@@ -58,7 +73,7 @@ function findDuplicateName(
|
||||
*/
|
||||
export default function MPlaceEditSheet({ planner, onOpenExpense }: MPlaceEditSheetProps) {
|
||||
const {
|
||||
t, toast, places, assignments, canUploadFiles,
|
||||
t, toast, places, assignments, storedAssignments, canUploadFiles,
|
||||
showPlaceForm, setShowPlaceForm,
|
||||
editingPlace, setEditingPlace,
|
||||
prefillCoords, setPrefillCoords,
|
||||
@@ -84,11 +99,23 @@ export default function MPlaceEditSheet({ planner, onOpenExpense }: MPlaceEditSh
|
||||
const [sheetPlace, setSheetPlace] = useState<Place | null>(null)
|
||||
const [sheetAssignmentId, setSheetAssignmentId] = useState<number | null>(null)
|
||||
|
||||
const dayAssignments = useMemo(
|
||||
() => (sheetPlace ? Object.values(assignments).flat() : []),
|
||||
[sheetPlace, assignments],
|
||||
// Live rather than the open-time snapshot, so the note's dirty check compares against
|
||||
// the note as it stands when Save is tapped.
|
||||
const ctxAssignment = useMemo(
|
||||
() => (sheetPlace ? findVisit(storedAssignments, sheetAssignmentId) : null),
|
||||
[sheetPlace, storedAssignments, sheetAssignmentId],
|
||||
)
|
||||
|
||||
// The overlap warning keeps to what the day lists show: on the plan tab a clash with a
|
||||
// pump or a booked night the list does not draw would name a stop nobody can see there.
|
||||
// The visit under edit joins when the lists hide it, because the warning reads the day
|
||||
// to compare against off that very row.
|
||||
const dayAssignments = useMemo(() => {
|
||||
if (!sheetPlace) return []
|
||||
const listed = Object.values(assignments).flat()
|
||||
return ctxAssignment && !listed.some(a => a.id === ctxAssignment.id) ? [...listed, ctxAssignment] : listed
|
||||
}, [sheetPlace, assignments, ctxAssignment])
|
||||
|
||||
// Prefill on open — same source order as the desktop form: editing place
|
||||
// (times off the in-context assignment), map/POI prefill coords, blank.
|
||||
useEffect(() => {
|
||||
@@ -96,9 +123,7 @@ export default function MPlaceEditSheet({ planner, onOpenExpense }: MPlaceEditSh
|
||||
setSheetPlace(editingPlace)
|
||||
setSheetAssignmentId(editingAssignmentId)
|
||||
if (editingPlace) {
|
||||
const assignment = editingAssignmentId
|
||||
? Object.values(assignments).flat().find(a => a.id === editingAssignmentId)
|
||||
: null
|
||||
const assignment = findVisit(storedAssignments, editingAssignmentId)
|
||||
const timeSource = assignment?.place ?? editingPlace
|
||||
setForm({
|
||||
name: editingPlace.name || '',
|
||||
@@ -143,7 +168,7 @@ export default function MPlaceEditSheet({ planner, onOpenExpense }: MPlaceEditSh
|
||||
setPendingFiles([])
|
||||
setDuplicateWarning(null)
|
||||
setDeleteArmed(false)
|
||||
// assignments is a fresh map each load — read at open time only.
|
||||
// storedAssignments is a fresh map each load, so it is read at open time only.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [showPlaceForm, editingPlace, prefillCoords, editingAssignmentId])
|
||||
|
||||
@@ -236,7 +261,6 @@ export default function MPlaceEditSheet({ planner, onOpenExpense }: MPlaceEditSh
|
||||
// #2163: the per-assignment note only travels when an assignment is in
|
||||
// context AND the value actually changed — same dirty-check as the
|
||||
// desktop form, so an untouched note never produces a PUT.
|
||||
const ctxAssignment = sheetAssignmentId ? dayAssignments.find(a => a.id === sheetAssignmentId) : null
|
||||
if (!ctxAssignment || (form.assignment_notes ?? '') === (ctxAssignment.notes ?? '')) {
|
||||
delete payload.assignment_notes
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { roadtripPreferencesRepo } from '../../repo/roadtripPreferencesRepo'
|
||||
// FE-TP-ROAD-001 to FE-TP-ROAD-096
|
||||
// FE-TP-ROAD-001 to FE-TP-ROAD-097
|
||||
import React from 'react'
|
||||
import { renderHook, act, waitFor } from '@testing-library/react'
|
||||
import { TranslationProvider } from '../../i18n/TranslationContext'
|
||||
@@ -2088,4 +2088,39 @@ describe('useTripPlanner road trip: the phone feed', () => {
|
||||
expect(result.current.showPlaceForm).toBe(true)
|
||||
expect(result.current.placeFormDayId).toBe(5)
|
||||
})
|
||||
|
||||
it('FE-TP-ROAD-097: a service stop opened for editing on the phone gets the place form with its own visit', async () => {
|
||||
// The stop sheet's pencil relies on this. The desk popup it would otherwise open has
|
||||
// no start time, and for a service stop the start time is the only way to pin or
|
||||
// unpin its arrival (a booked night can also be held by the booking's check-in).
|
||||
const fuelStop = buildPlace({ id: 102, name: 'Tankstelle', stop_type: 'fuel', lat: 60.39, lng: 5.32 })
|
||||
seedTrip({
|
||||
places: [fuelStop],
|
||||
days: [buildDay({ id: 5, day_number: 1 })],
|
||||
assignments: { '5': [buildAssignment({ id: 12, day_id: 5, order_index: 0, place: fuelStop })] },
|
||||
})
|
||||
// The STORED mode is on, set here rather than trusted to the suite's beforeEach: with
|
||||
// it off the form branch would be taken anyway and this test would prove nothing.
|
||||
sessionStorage.setItem('trip-roadtrip-42', '1')
|
||||
sessionStorage.setItem('trip-tab-42', 'roadtrip')
|
||||
|
||||
const phone = await renderPhone()
|
||||
act(() => { phone.result.current.openPlaceEditor(fuelStop, 12) })
|
||||
|
||||
expect(phone.result.current.roadtripMode).toBe(false)
|
||||
expect(phone.result.current.stopDraft).toBeNull()
|
||||
expect(phone.result.current.showPlaceForm).toBe(true)
|
||||
expect(phone.result.current.editingPlace?.id).toBe(102)
|
||||
expect(phone.result.current.editingAssignmentId).toBe(12)
|
||||
phone.unmount()
|
||||
|
||||
// The very same trip at desk width opens the stop popup, so what kept the phone on
|
||||
// the form is the width alone.
|
||||
Object.defineProperty(window, 'innerWidth', { configurable: true, writable: true, value: desktopWidth })
|
||||
const desk = await renderRoadtrip()
|
||||
act(() => { desk.result.current.openPlaceEditor(fuelStop, 12) })
|
||||
|
||||
expect(desk.result.current.stopDraft).toMatchObject({ dayId: 5, editing: { placeId: 102, stopType: 'fuel' } })
|
||||
expect(desk.result.current.showPlaceForm).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user