mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 22:57:32 +00:00
fix(atlas): date a country's first and last trip from the trips that count
The Atlas tooltip took both dates from the start dates of every trip with a place in the country. One trip showed the month it began twice, the month it ended never appeared, and a trip only booked could become the last visit of a country already visited. Both now come from the trips that give the country its status, places and bookings alike: the start of the earliest and the end of the latest, with a trip still under way ending today. The optimistic mark on the client clears the dates the same way the server does. The second label rendered the raw key atlas.lastVisitLabel since #1908 removed it from the locales; the strings are back as they were. The month is built from the date's digits now, because a 'YYYY-MM-DD' string parses as UTC and showed the month before anywhere west of Greenwich. Closes #1535
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import {
|
||||
A2_TO_A3,
|
||||
bucketTooltipHeight,
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
bucketTooltipPlacement,
|
||||
bucketTooltipWidth,
|
||||
countryStatus,
|
||||
visitMonth,
|
||||
findBucketDuplicate,
|
||||
isBucketDuplicateError,
|
||||
isCountryVisible,
|
||||
@@ -71,6 +72,31 @@ describe('countryStatus', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('visitMonth (#1535)', () => {
|
||||
// West of Greenwich, where new Date('2024-06-01') is still the last evening of May.
|
||||
beforeEach(() => {
|
||||
vi.stubEnv('TZ', 'America/New_York');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it('reads the year and month as a local date, so the 1st stays in its own month', () => {
|
||||
const june = visitMonth('2024-06-01')!;
|
||||
expect([june.getFullYear(), june.getMonth(), june.getDate()]).toEqual([2024, 5, 1]);
|
||||
const january = visitMonth('2025-01-01')!;
|
||||
expect([january.getFullYear(), january.getMonth()]).toEqual([2025, 0]);
|
||||
});
|
||||
|
||||
it('gives nothing for a missing or malformed date', () => {
|
||||
expect(visitMonth(null)).toBeNull();
|
||||
expect(visitMonth(undefined)).toBeNull();
|
||||
expect(visitMonth('')).toBeNull();
|
||||
expect(visitMonth('June 2024')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('isCountryVisible', () => {
|
||||
it('always shows visited countries', () => {
|
||||
expect(isCountryVisible({ status: 'visited' }, false)).toBe(true);
|
||||
@@ -121,7 +147,7 @@ describe('withCountryMarkedVisited', () => {
|
||||
const prev = base({
|
||||
countries: [
|
||||
{ code: 'FR', tripCount: 2, placeCount: 5, status: 'visited' },
|
||||
{ code: 'JP', tripCount: 1, placeCount: 0, status: 'planned' },
|
||||
{ code: 'JP', tripCount: 1, placeCount: 0, firstVisit: '2099-05-01', lastVisit: '2099-05-08', status: 'planned' },
|
||||
],
|
||||
stats: { totalTrips: 3, totalPlaces: 10, totalCountries: 1, totalDays: 14, totalCountriesPlanned: 1 },
|
||||
continents: { Europe: 1 },
|
||||
@@ -131,7 +157,8 @@ describe('withCountryMarkedVisited', () => {
|
||||
const next = withCountryMarkedVisited(prev, 'JP');
|
||||
|
||||
expect(next.countries).toHaveLength(2);
|
||||
expect(next.countries.find((c) => c.code === 'JP')?.status).toBe('visited');
|
||||
// The planned trip's dates are not the dates of a visit, as the server agrees (#1535).
|
||||
expect(next.countries.find((c) => c.code === 'JP')).toMatchObject({ status: 'visited', firstVisit: null, lastVisit: null });
|
||||
expect(next.stats.totalCountries).toBe(2);
|
||||
expect(next.stats.totalCountriesPlanned).toBe(0);
|
||||
expect(next.continents).toEqual({ Europe: 1, Asia: 1 });
|
||||
|
||||
@@ -61,6 +61,15 @@ export function isCountryVisible(c: Pick<AtlasCountry, 'status'>, showPlanned: b
|
||||
return countryStatus(c) === 'visited' || showPlanned
|
||||
}
|
||||
|
||||
/**
|
||||
* The month a 'YYYY-MM-DD' visit date falls in, as a local date. `new Date('2025-06-01')`
|
||||
* reads the string as UTC midnight, which is still May anywhere west of Greenwich (#1535).
|
||||
*/
|
||||
export function visitMonth(date: string | null | undefined): Date | null {
|
||||
const match = date ? /^(\d{4})-(\d{2})/.exec(date) : null
|
||||
return match ? new Date(Number(match[1]), Number(match[2]) - 1, 1) : null
|
||||
}
|
||||
|
||||
/**
|
||||
* Fold a manual "I have been here" mark into the loaded data without refetching — the
|
||||
* map redraws from `data`, so a reload would flash the whole globe. A country that was
|
||||
@@ -75,7 +84,8 @@ export function withCountryMarkedVisited(prev: AtlasData, code: string): AtlasDa
|
||||
return {
|
||||
...prev,
|
||||
countries: existing
|
||||
? prev.countries.map(c => (c.code === code ? { ...c, status: 'visited' as const } : c))
|
||||
// A country that was not visited yet has no visited trip to take dates from (#1535).
|
||||
? prev.countries.map(c => (c.code === code ? { ...c, status: 'visited' as const, firstVisit: null, lastVisit: null } : c))
|
||||
: [...prev.countries, { code, placeCount: 0, tripCount: 0, firstVisit: null, lastVisit: null, status: 'visited' as const }],
|
||||
stats: {
|
||||
...prev.stats,
|
||||
|
||||
@@ -312,6 +312,7 @@ beforeEach(() => {
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
vi.unstubAllEnvs();
|
||||
delete window.__addToast;
|
||||
});
|
||||
|
||||
@@ -754,6 +755,24 @@ describe('useAtlas', () => {
|
||||
expect(atlas.confirmAction?.name).not.toBe('GERMANY_EN_ONLY');
|
||||
});
|
||||
|
||||
it('FE-HOOK-ATLAS-062: the country tooltip names both dates and shows their months (#1535)', async () => {
|
||||
// FR's dates fall on the 1st, which a UTC parse moves into the month before over here.
|
||||
vi.stubEnv('TZ', 'America/New_York');
|
||||
await mountAtlas({ geo: geoCountries });
|
||||
await waitFor(() => expect(lf.geoJson.length).toBeGreaterThan(0));
|
||||
|
||||
const countryLayer = lf.geoJson[0] as MockGeoJson;
|
||||
const fr = countryLayer.entries.find((e) => (e.feature.properties as Record<string, string>).ISO_A2 === 'FR');
|
||||
const html = fr!.layer.bindTooltip.mock.calls[0][0] as string;
|
||||
|
||||
expect(html).toContain('First trip');
|
||||
expect(html).toContain('Last trip');
|
||||
expect(html).not.toContain('atlas.lastVisitLabel');
|
||||
const month = (y: number, m: number) => new Date(y, m, 1).toLocaleDateString('en-US', { month: 'short', year: 'numeric' });
|
||||
expect(html).toContain(month(2023, 0));
|
||||
expect(html).toContain(month(2024, 5));
|
||||
});
|
||||
|
||||
it('FE-HOOK-ATLAS-026: plugin tint layers are drawn in their own pane and redrawn on theme change', async () => {
|
||||
await mountAtlas({
|
||||
geo: geoCountries,
|
||||
|
||||
@@ -9,7 +9,7 @@ import { isVectorStyle } from '../../utils/tileUrl'
|
||||
import apiClient, { mapsApi, pluginsApi, type PluginAtlasLayer } from '../../api/client'
|
||||
import L from 'leaflet'
|
||||
import type { GeoJsonFeatureCollection } from '../../types'
|
||||
import { A2_TO_A3, countryStatus, findBucketDuplicate, isBucketDuplicateError, isCountryVisible, normalizeRegionName, regionCacheEvictions, withCountryMarkedVisited, wishlistA3Codes, countryColor, REGION_CACHE_MAX, bucketTooltipWidth, bucketTooltipPlacement, bucketTooltipNeedsScroll, type AtlasData, type AtlasPlaceHit, type CountryDetail, type BucketItem } from './atlasModel'
|
||||
import { A2_TO_A3, countryStatus, visitMonth, findBucketDuplicate, isBucketDuplicateError, isCountryVisible, normalizeRegionName, regionCacheEvictions, withCountryMarkedVisited, wishlistA3Codes, countryColor, REGION_CACHE_MAX, bucketTooltipWidth, bucketTooltipPlacement, bucketTooltipNeedsScroll, type AtlasData, type AtlasPlaceHit, type CountryDetail, type BucketItem } from './atlasModel'
|
||||
import { continentForCountry, escapeHtml, type VisitStatus } from '@trek/shared'
|
||||
import { useGlassGlare } from '../../components/Atlas/useGlassGlare'
|
||||
import { dawarichApi } from '../../api/dawarich'
|
||||
@@ -560,7 +560,7 @@ export function useAtlas() {
|
||||
if (c) {
|
||||
country_layer_by_a2_ref.current[c.code] = layer
|
||||
const name = resolveName(c.code)
|
||||
const formatDate = (d) => { if (!d) return '—'; const dt = new Date(d); return dt.toLocaleDateString(getLocaleForLanguage(language), { month: 'short', year: 'numeric' }) }
|
||||
const formatDate = (d) => { const month = visitMonth(d); return month ? month.toLocaleDateString(getLocaleForLanguage(language), { month: 'short', year: 'numeric' }) : '—' }
|
||||
// "First trip / Last trip" is simply wrong for a country you haven't reached yet —
|
||||
// a planned one gets a single departure date instead.
|
||||
const planned = countryStatus(c) !== 'visited'
|
||||
|
||||
Reference in New Issue
Block a user