mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
fix(atlas): removing the last region keeps a country the user marked visited itself
Marking a region wrote its country with source 'manual', the same as ticking the country off by hand, and removing the last region then took the country away whatever its origin, Dawarich confirmations included. A country that only comes in through a region is now stored as 'region', marking it visited afterwards turns it into the user's own mark, and the cascade in unmarkRegion only runs for a country without such a mark. Readers still see 'manual' for those rows, so the country detail, the MCP resource and the plugin RPC answer as before. Rows written before this change stay 'manual' and keep their country. REST, MCP and plugin RPC share the service. The atlas popup on desktop and phone now asks the server whether the country is still marked before it drops it from the map instead of guessing. Refs #2597
This commit is contained in:
@@ -44,6 +44,8 @@ beforeEach(() => {
|
||||
http.post('/api/addons/atlas/country/:code/mark', () => HttpResponse.json({ success: true })),
|
||||
http.post('/api/addons/atlas/region/:code/mark', () => HttpResponse.json({ success: true })),
|
||||
http.delete('/api/addons/atlas/region/:code/mark', () => HttpResponse.json({ success: true })),
|
||||
// After a region goes the atlas asks whether the country is still marked (#2597).
|
||||
http.get('/api/addons/atlas/country/:code', () => HttpResponse.json({ manually_marked: false })),
|
||||
http.post('/api/addons/atlas/bucket-list', () =>
|
||||
HttpResponse.json({ item: buildBucketItem({ id: 99, name: 'Germany', country_code: 'DE' }) }),
|
||||
),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// FE-PAGE-ATLASACTIONS-001 to FE-PAGE-ATLASACTIONS-014: the country / region popup logic
|
||||
// FE-PAGE-ATLASACTIONS-001 to FE-PAGE-ATLASACTIONS-015: the country / region popup logic
|
||||
// behind both the desktop dialog and the phone sheet.
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
@@ -73,6 +73,9 @@ beforeEach(() => {
|
||||
deleted.push(new URL(request.url).pathname);
|
||||
return HttpResponse.json({ success: true });
|
||||
}),
|
||||
http.get('/api/addons/atlas/country/:code', () =>
|
||||
HttpResponse.json({ places: [], trips: [], manually_marked: false, marked_source: null, status: 'idea' })
|
||||
),
|
||||
http.post('/api/addons/atlas/bucket-list', async ({ request }) => {
|
||||
const body = (await request.json()) as Record<string, unknown>;
|
||||
posted.push({ url: '/api/addons/atlas/bucket-list', body });
|
||||
@@ -202,6 +205,32 @@ describe('useAtlasCountryActions', () => {
|
||||
expect(result.current.confirmAction).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-PAGE-ATLASACTIONS-015: a country the server still holds as marked stays when its last region goes (#2597)', async () => {
|
||||
const init = () => ({
|
||||
confirmAction: { type: 'unmark-region', code: 'ES', name: 'Galicia', regionCode: 'ES-GA' } as AtlasConfirmAction,
|
||||
data: buildAtlasData({
|
||||
countries: [{ code: 'ES', tripCount: 0, placeCount: 0, firstVisit: null, lastVisit: null }],
|
||||
}),
|
||||
visitedRegions: { ES: [{ code: 'ES-GA', name: 'Galicia', placeCount: 0 }] },
|
||||
});
|
||||
server.use(
|
||||
http.get('/api/addons/atlas/country/:code', () =>
|
||||
HttpResponse.json({ places: [], trips: [], manually_marked: true, marked_source: 'manual', status: 'visited' })
|
||||
)
|
||||
);
|
||||
const marked = setup(init());
|
||||
await act(() => marked.result.current.actions.unmarkRegion());
|
||||
expect(marked.result.current.visitedRegions).toEqual({});
|
||||
expect(marked.result.current.data?.countries.map((c) => c.code)).toEqual(['ES']);
|
||||
|
||||
// No answer: the country stays until the next load rather than vanishing on a guess.
|
||||
server.use(http.get('/api/addons/atlas/country/:code', () => HttpResponse.json({}, { status: 500 })));
|
||||
const unknown = setup(init());
|
||||
await act(() => unknown.result.current.actions.unmarkRegion());
|
||||
expect(unknown.result.current.data?.countries.map((c) => c.code)).toEqual(['ES']);
|
||||
expect(addToast).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-PAGE-ATLASACTIONS-010: adding to the bucket list posts the entry, prepends it and runs beforeClose', async () => {
|
||||
const beforeClose = vi.fn();
|
||||
const { result } = setup({
|
||||
|
||||
@@ -67,11 +67,12 @@ export function withRegionUnmarked(prev: VisitedRegionMap, countryCode: string,
|
||||
}
|
||||
|
||||
/**
|
||||
* The atlas once a region is unmarked: if no visible region of the country remains
|
||||
* (however a region was derived: the server hides it either way and cascades to the
|
||||
* country the same way), the country goes too, but only when it has no place or trip
|
||||
* of its own. A country with real places is never hidden server side (#1490), so
|
||||
* removing it here would only flash and reappear on the next load.
|
||||
* The atlas once a region is unmarked and the server no longer holds the country as
|
||||
* marked: if no visible region of the country remains (however a region was derived:
|
||||
* the server hides it either way and cascades to the country the same way), the
|
||||
* country goes too, but only when it has no place or trip of its own. A country with
|
||||
* real places is never hidden server side (#1490), so removing it here would only
|
||||
* flash and reappear on the next load.
|
||||
*/
|
||||
export function withCountryDroppedAfterRegionUnmark(
|
||||
prev: AtlasData | null,
|
||||
@@ -93,6 +94,21 @@ export function withCountryDroppedAfterRegionUnmark(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the server still holds the country as marked after one of its regions was
|
||||
* unmarked. A country the user marked itself survives its last region (#2597), so the
|
||||
* atlas asks instead of guessing; when the answer does not come, the country stays
|
||||
* until the next load settles it.
|
||||
*/
|
||||
async function isCountryStillMarked(code: string): Promise<boolean> {
|
||||
try {
|
||||
const r = await apiClient.get(`/addons/atlas/country/${code}`);
|
||||
return r.data?.manually_marked !== false;
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* What the country / region action popup does, behind both the desktop dialog and the
|
||||
* phone sheet (which render their own markup and their own bucket list date picker):
|
||||
@@ -155,7 +171,9 @@ export function useAtlasCountryActions({
|
||||
try {
|
||||
await apiClient.delete(`/addons/atlas/region/${regionCode}/mark`);
|
||||
setVisitedRegions((prev) => withRegionUnmarked(prev, countryCode, regionCode));
|
||||
setData((prev) => withCountryDroppedAfterRegionUnmark(prev, countryCode, regionCode, visitedRegions));
|
||||
if (!(await isCountryStillMarked(countryCode))) {
|
||||
setData((prev) => withCountryDroppedAfterRegionUnmark(prev, countryCode, regionCode, visitedRegions));
|
||||
}
|
||||
} catch (err) {
|
||||
toast.error(getApiErrorMessage(err, t('common.error')));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user