1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-21 22:09:36 +02:00

feat: Enhance date handling in AdventureModal and related components for improved localization and all-day event support

This commit is contained in:
Sean Morley 2025-03-18 21:07:34 -04:00
parent 876c5e83b4
commit f554bb8777
3 changed files with 54 additions and 46 deletions

View file

@ -834,11 +834,16 @@
</p> </p>
{#if visit.end_date && visit.end_date !== visit.start_date} {#if visit.end_date && visit.end_date !== visit.start_date}
<p> <p>
{new Date(visit.end_date).toLocaleDateString(undefined, { {#if isAllDay(visit.end_date)}
timeZone: 'UTC' <!-- For all-day events, show just the date -->
})} {new Date(visit.end_date).toLocaleDateString(undefined, {
{#if !isAllDay(visit.end_date)} timeZone: 'UTC'
({new Date(visit.end_date).toLocaleTimeString()}) })}
{:else}
<!-- For timed events, show date and time -->
{new Date(visit.end_date).toLocaleDateString()} ({new Date(
visit.end_date
).toLocaleTimeString()})
{/if} {/if}
</p> </p>
{/if} {/if}

View file

@ -70,23 +70,23 @@ export function groupAdventuresByDate(
// Initialize all days in the range // Initialize all days in the range
for (let i = 0; i < numberOfDays; i++) { for (let i = 0; i < numberOfDays; i++) {
const currentDate = new Date(startDate); const currentDate = new Date(startDate);
currentDate.setUTCDate(startDate.getUTCDate() + i); currentDate.setDate(startDate.getDate() + i);
const dateString = currentDate.toISOString().split('T')[0]; const dateString = getLocalDateString(currentDate);
groupedAdventures[dateString] = []; groupedAdventures[dateString] = [];
} }
adventures.forEach((adventure) => { adventures.forEach((adventure) => {
adventure.visits.forEach((visit) => { adventure.visits.forEach((visit) => {
if (visit.start_date) { if (visit.start_date) {
const adventureDate = new Date(visit.start_date).toISOString().split('T')[0]; const adventureDate = getLocalDateString(new Date(visit.start_date));
if (visit.end_date) { if (visit.end_date) {
const endDate = new Date(visit.end_date).toISOString().split('T')[0]; const endDate = new Date(visit.end_date).toISOString().split('T')[0];
// Loop through all days and include adventure if it falls within the range // Loop through all days and include adventure if it falls within the range
for (let i = 0; i < numberOfDays; i++) { for (let i = 0; i < numberOfDays; i++) {
const currentDate = new Date(startDate); const currentDate = new Date(startDate);
currentDate.setUTCDate(startDate.getUTCDate() + i); currentDate.setDate(startDate.getDate() + i);
const dateString = currentDate.toISOString().split('T')[0]; const dateString = getLocalDateString(currentDate);
// Include the current day if it falls within the adventure date range // Include the current day if it falls within the adventure date range
if (dateString >= adventureDate && dateString <= endDate) { if (dateString >= adventureDate && dateString <= endDate) {
@ -116,22 +116,22 @@ export function groupTransportationsByDate(
// Initialize all days in the range // Initialize all days in the range
for (let i = 0; i < numberOfDays; i++) { for (let i = 0; i < numberOfDays; i++) {
const currentDate = new Date(startDate); const currentDate = new Date(startDate);
currentDate.setUTCDate(startDate.getUTCDate() + i); currentDate.setDate(startDate.getDate() + i);
const dateString = currentDate.toISOString().split('T')[0]; const dateString = getLocalDateString(currentDate);
groupedTransportations[dateString] = []; groupedTransportations[dateString] = [];
} }
transportations.forEach((transportation) => { transportations.forEach((transportation) => {
if (transportation.date) { if (transportation.date) {
const transportationDate = new Date(transportation.date).toISOString().split('T')[0]; const transportationDate = getLocalDateString(new Date(transportation.date));
if (transportation.end_date) { if (transportation.end_date) {
const endDate = new Date(transportation.end_date).toISOString().split('T')[0]; const endDate = new Date(transportation.end_date).toISOString().split('T')[0];
// Loop through all days and include transportation if it falls within the range // Loop through all days and include transportation if it falls within the range
for (let i = 0; i < numberOfDays; i++) { for (let i = 0; i < numberOfDays; i++) {
const currentDate = new Date(startDate); const currentDate = new Date(startDate);
currentDate.setUTCDate(startDate.getUTCDate() + i); currentDate.setDate(startDate.getDate() + i);
const dateString = currentDate.toISOString().split('T')[0]; const dateString = getLocalDateString(currentDate);
// Include the current day if it falls within the transportation date range // Include the current day if it falls within the transportation date range
if (dateString >= transportationDate && dateString <= endDate) { if (dateString >= transportationDate && dateString <= endDate) {
@ -150,6 +150,13 @@ export function groupTransportationsByDate(
return groupedTransportations; return groupedTransportations;
} }
function getLocalDateString(date: Date): string {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are 0-indexed
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
export function groupLodgingByDate( export function groupLodgingByDate(
transportations: Lodging[], transportations: Lodging[],
startDate: Date, startDate: Date,
@ -157,35 +164,32 @@ export function groupLodgingByDate(
): Record<string, Lodging[]> { ): Record<string, Lodging[]> {
const groupedTransportations: Record<string, Lodging[]> = {}; const groupedTransportations: Record<string, Lodging[]> = {};
// Initialize all days in the range // Initialize all days in the range using local dates
for (let i = 0; i < numberOfDays; i++) { for (let i = 0; i < numberOfDays; i++) {
const currentDate = new Date(startDate); const currentDate = new Date(startDate);
currentDate.setUTCDate(startDate.getUTCDate() + i); currentDate.setDate(startDate.getDate() + i);
const dateString = currentDate.toISOString().split('T')[0]; const dateString = getLocalDateString(currentDate);
groupedTransportations[dateString] = []; groupedTransportations[dateString] = [];
} }
transportations.forEach((transportation) => { transportations.forEach((transportation) => {
if (transportation.check_in) { if (transportation.check_in) {
const transportationDate = new Date(transportation.check_in).toISOString().split('T')[0]; // Use local date string conversion
const transportationDate = getLocalDateString(new Date(transportation.check_in));
if (transportation.check_out) { if (transportation.check_out) {
const endDate = new Date(transportation.check_out).toISOString().split('T')[0]; const endDate = getLocalDateString(new Date(transportation.check_out));
// Loop through all days and include transportation if it falls within the range // Loop through all days and include transportation if it falls within the transportation date range
for (let i = 0; i < numberOfDays; i++) { for (let i = 0; i < numberOfDays; i++) {
const currentDate = new Date(startDate); const currentDate = new Date(startDate);
currentDate.setUTCDate(startDate.getUTCDate() + i); currentDate.setDate(startDate.getDate() + i);
const dateString = currentDate.toISOString().split('T')[0]; const dateString = getLocalDateString(currentDate);
// Include the current day if it falls within the transportation date range
if (dateString >= transportationDate && dateString <= endDate) { if (dateString >= transportationDate && dateString <= endDate) {
if (groupedTransportations[dateString]) { groupedTransportations[dateString].push(transportation);
groupedTransportations[dateString].push(transportation);
}
} }
} }
} else if (groupedTransportations[transportationDate]) { } else if (groupedTransportations[transportationDate]) {
// If there's no end date, add transportation to the start date only
groupedTransportations[transportationDate].push(transportation); groupedTransportations[transportationDate].push(transportation);
} }
} }
@ -201,19 +205,18 @@ export function groupNotesByDate(
): Record<string, Note[]> { ): Record<string, Note[]> {
const groupedNotes: Record<string, Note[]> = {}; const groupedNotes: Record<string, Note[]> = {};
// Initialize all days in the range // Initialize all days in the range using local dates
for (let i = 0; i < numberOfDays; i++) { for (let i = 0; i < numberOfDays; i++) {
const currentDate = new Date(startDate); const currentDate = new Date(startDate);
currentDate.setUTCDate(startDate.getUTCDate() + i); currentDate.setDate(startDate.getDate() + i);
const dateString = currentDate.toISOString().split('T')[0]; const dateString = getLocalDateString(currentDate);
groupedNotes[dateString] = []; groupedNotes[dateString] = [];
} }
notes.forEach((note) => { notes.forEach((note) => {
if (note.date) { if (note.date) {
const noteDate = new Date(note.date).toISOString().split('T')[0]; // Use the date string as is since it's already in "YYYY-MM-DD" format.
const noteDate = note.date;
// Add note to the appropriate date group if it exists
if (groupedNotes[noteDate]) { if (groupedNotes[noteDate]) {
groupedNotes[noteDate].push(note); groupedNotes[noteDate].push(note);
} }
@ -230,19 +233,18 @@ export function groupChecklistsByDate(
): Record<string, Checklist[]> { ): Record<string, Checklist[]> {
const groupedChecklists: Record<string, Checklist[]> = {}; const groupedChecklists: Record<string, Checklist[]> = {};
// Initialize all days in the range // Initialize all days in the range using local dates
for (let i = 0; i < numberOfDays; i++) { for (let i = 0; i < numberOfDays; i++) {
const currentDate = new Date(startDate); const currentDate = new Date(startDate);
currentDate.setUTCDate(startDate.getUTCDate() + i); currentDate.setDate(startDate.getDate() + i);
const dateString = currentDate.toISOString().split('T')[0]; const dateString = getLocalDateString(currentDate);
groupedChecklists[dateString] = []; groupedChecklists[dateString] = [];
} }
checklists.forEach((checklist) => { checklists.forEach((checklist) => {
if (checklist.date) { if (checklist.date) {
const checklistDate = new Date(checklist.date).toISOString().split('T')[0]; // Use the date string as is since it's already in "YYYY-MM-DD" format.
const checklistDate = checklist.date;
// Add checklist to the appropriate date group if it exists
if (groupedChecklists[checklistDate]) { if (groupedChecklists[checklistDate]) {
groupedChecklists[checklistDate].push(checklist); groupedChecklists[checklistDate].push(checklist);
} }

View file

@ -935,24 +935,25 @@
{@const dateString = adjustedDate.toISOString().split('T')[0]} {@const dateString = adjustedDate.toISOString().split('T')[0]}
{@const dayAdventures = {@const dayAdventures =
groupAdventuresByDate(adventures, new Date(collection.start_date), numberOfDays)[ groupAdventuresByDate(adventures, new Date(collection.start_date), numberOfDays + 1)[
dateString dateString
] || []} ] || []}
{@const dayTransportations = {@const dayTransportations =
groupTransportationsByDate( groupTransportationsByDate(
transportations, transportations,
new Date(collection.start_date), new Date(collection.start_date),
numberOfDays numberOfDays + 1
)[dateString] || []} )[dateString] || []}
{@const dayLodging = {@const dayLodging =
groupLodgingByDate(lodging, new Date(collection.start_date), numberOfDays)[ groupLodgingByDate(lodging, new Date(collection.start_date), numberOfDays + 1)[
dateString dateString
] || []} ] || []}
{@const dayNotes = {@const dayNotes =
groupNotesByDate(notes, new Date(collection.start_date), numberOfDays)[dateString] || groupNotesByDate(notes, new Date(collection.start_date), numberOfDays + 1)[
[]} dateString
] || []}
{@const dayChecklists = {@const dayChecklists =
groupChecklistsByDate(checklists, new Date(collection.start_date), numberOfDays)[ groupChecklistsByDate(checklists, new Date(collection.start_date), numberOfDays + 1)[
dateString dateString
] || []} ] || []}