Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(journey): do not count the suggested weather as an unsaved change

Maurice76b7b305b26e
diff --git a/client/src/components/Journey/useJourneyEntryForm.test.ts b/client/src/components/Journey/useJourneyEntryForm.test.ts
index 71740f2bf..6e31c753c 100644
--- a/client/src/components/Journey/useJourneyEntryForm.test.ts
+++ b/client/src/components/Journey/useJourneyEntryForm.test.ts
@@ -1,7 +1,7 @@
// FE-JRN-ENTRYFORM-001 to FE-JRN-ENTRYFORM-024: the journey entry form logic behind both
// the desktop editor dialog and the phone entry sheet.
import { act, renderHook, waitFor } from '@testing-library/react';
-import type React from 'react';
+import React from 'react';
import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import { journeyApi, mapsApi, weatherApi } from '../../api/client';
@@ -341,6 +341,51 @@ describe('useJourneyEntryForm', () => {
expect(get).toHaveBeenCalledTimes(1);
});
+ it('FE-JRN-ENTRYFORM-016b: the weather it fills in is a suggestion, so an untouched entry closes without asking', async () => {
+ vi.spyOn(weatherApi, 'get').mockResolvedValue({ main: 'Rain', description: 'light rain' } as never);
+ const { result } = setup({ entry: entry({ weather: '' }), autoFillWeather: true });
+ await waitFor(() => expect(result.current.weather).toBe('rainy'));
+ // 5.0.0-pre.1 feedback: Cancel asked about unsaved changes nobody made.
+ expect(result.current.isDirty).toBe(false);
+
+ act(() => result.current.setWeather('sunny'));
+ expect(result.current.isDirty).toBe(true);
+ act(() => result.current.setWeather(''));
+ expect(result.current.isDirty).toBe(false);
+ act(() => result.current.setWeather('rainy'));
+ expect(result.current.isDirty).toBe(false);
+ });
+
+ it('FE-JRN-ENTRYFORM-016d: an icon picked while the forecast is out, then cleared, stays cleared', async () => {
+ let answer: (value: unknown) => void = () => {};
+ const get = vi
+ .spyOn(weatherApi, 'get')
+ .mockImplementation(() => new Promise((resolve) => (answer = resolve)) as never);
+ const { result } = setup({ entry: entry({ weather: '' }), autoFillWeather: true });
+ act(() => result.current.setWeather('sunny'));
+ await act(async () => answer({ main: 'Rain', description: 'light rain' }));
+ act(() => result.current.setWeather(''));
+ expect(result.current.weather).toBe('');
+ expect(get).toHaveBeenCalledTimes(1);
+ });
+
+ it("FE-JRN-ENTRYFORM-016c: StrictMode's double mount does not swallow the weather", async () => {
+ vi.spyOn(weatherApi, 'get').mockResolvedValue({ main: 'Rain', description: 'light rain' } as never);
+ const props: JourneyEntryFormOptions = {
+ entry: entry({ weather: '' }),
+ journeyId: 3,
+ trips: [],
+ galleryPhotos: [],
+ onSave: vi.fn().mockResolvedValue(4),
+ onUploadPhotos: vi.fn().mockResolvedValue({ ok: [], failed: [] }),
+ onDone: vi.fn(),
+ autoFillWeather: true,
+ };
+ // The first mount's answer is dropped by its cleanup; the second mount has to ask again.
+ const { result } = renderHook(() => useJourneyEntryForm(props), { wrapper: React.StrictMode });
+ await waitFor(() => expect(result.current.weather).toBe('rainy'));
+ });
+
it('FE-JRN-ENTRYFORM-017: quick capture locates the traveller and names the place and the weather', async () => {
locate.mockResolvedValue({ lat: 5, lng: 6 });
vi.spyOn(mapsApi, 'reverse').mockResolvedValue({ name: '', address: 'Main St' } as never);
diff --git a/client/src/components/Journey/useJourneyEntryForm.ts b/client/src/components/Journey/useJourneyEntryForm.ts
index b641890f6..1b93993cc 100644
--- a/client/src/components/Journey/useJourneyEntryForm.ts
+++ b/client/src/components/Journey/useJourneyEntryForm.ts
@@ -106,6 +106,9 @@ export function useJourneyEntryForm({
const locationSearchSeqRef = useRef(0);
const [mood, setMood] = useState(entry.mood || '');
const [weather, setWeather] = useState(entry.weather || '');
+ // The icon the form filled in by itself (see the weather effect below). It is a
+ // suggestion, not an edit, so leaving the dialog must not ask about it.
+ const [suggestedWeather, setSuggestedWeather] = useState('');
const [statsExcluded, setStatsExcluded] = useState(entry.stats_excluded ?? false);
// The trip this day belongs to, when the journey does not follow it yet (#2265).
const tripSuggestion = useJourneyTripSuggestion(
@@ -155,7 +158,7 @@ export function useJourneyEntryForm({
((locationLat ?? null) !== (entry.location_lat ?? null) ||
(locationLng ?? null) !== (entry.location_lng ?? null))) ||
mood !== (entry.mood || '') ||
- weather !== (entry.weather || '') ||
+ (weather !== (entry.weather || '') && weather !== suggestedWeather) ||
statsExcluded !== (entry.stats_excluded ?? false) ||
isDraft !== (entry.is_draft ?? false) ||
joinRows(pros) !== (entry.pros_cons?.pros ?? []).join('\n') ||
@@ -234,9 +237,17 @@ export function useJourneyEntryForm({
* day from the ERA5 archive, so writing up last Tuesday gets last Tuesday's weather.
*
* Only ever fills an empty field, and each place and day is tried once, so a cleared
- * icon stays cleared and a chosen one is never overwritten.
+ * icon stays cleared and a chosen one is never overwritten. The tried place and day
+ * are forgotten on unmount only, so StrictMode's mount, cleanup, mount in development
+ * asks again, while picking or clearing an icon does not.
*/
const weatherTriedRef = useRef<string | null>(null);
+ useEffect(
+ () => () => {
+ weatherTriedRef.current = null;
+ },
+ []
+ );
useEffect(() => {
if (!autoFillWeather) return;
if (typeof locationLat !== 'number' || typeof locationLng !== 'number') return;
@@ -253,9 +264,10 @@ export function useJourneyEntryForm({
// does not stop it reaching here in production.
if (!active || !result || result.error || typeof result.main !== 'string') return;
const category = journeyWeatherCategory(result.main, result.description ?? '');
- // Re-checked rather than trusted from the closure: the request is a
- // round trip and the traveller may have picked an icon while it was out.
- setWeather((current) => current || category);
+ // No need to re-check the field: picking an icon while the request was out
+ // re-runs this effect, whose cleanup has already set `active` to false.
+ setSuggestedWeather(category);
+ setWeather(category);
})
.catch(() => {
/* no weather is a fine outcome for a journal entry */