Skip to content

Commit 06fb538

Browse files
committed
fix(calendar): correctly space days when 1 or 2 results only
1 parent b20cbb6 commit 06fb538

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed

src/components/common/list/ListItemPanel.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ const onTagClick = (url?: string) => {
214214
<TagLink :tag="tag" @on-click="onTagClick" />
215215
</template>
216216
<template v-if="!hideTime && date">
217-
<NSkeleton v-if="loading" key="date-loader" text style="width: 6%" />
217+
<NSkeleton v-if="loading" key="date-loader" text style="width: 8%" />
218218
<NTag
219219
v-else
220220
key="date"

src/stores/data/calendar.store.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ export const useCalendarStore = defineStore(CalendarStoreConstants.Store, () =>
110110
Logger.debug('Fetching calendar', { mode, start_date, end_date, days: days.value });
111111

112112
const timeout = setTimeout(() => {
113-
if (mode === 'reload') calendar.value = getEmptyWeeks(startDate, true);
113+
if (mode === 'reload') calendar.value = getEmptyWeeks({ startDate, loading: true, days: days.value });
114114
else if (mode === 'start') calendar.value = [getLoadingPlaceholder(DateUtils.previous(1, endDate)), ...calendar.value];
115-
else if (mode === 'end') calendar.value = [...calendar.value, ...getEmptyWeeks(startDate, true)];
115+
else if (mode === 'end') calendar.value = [...calendar.value, ...getEmptyWeeks({ startDate, loading: true, days: days.value })];
116116
}, 100);
117117

118118
const query: TraktCalendarQuery = { start_date, days: days.value };
@@ -121,7 +121,7 @@ export const useCalendarStore = defineStore(CalendarStoreConstants.Store, () =>
121121
try {
122122
const newData: CalendarItem[] = await fetchCalendarData(query);
123123
delete calendarErrors[JSON.stringify(query)];
124-
const spacedData = spaceDate(newData, startDate, endDate);
124+
const spacedData = spaceDate(newData, { startDate, endDate, days: days.value });
125125

126126
if (mode === 'reload') {
127127
calendar.value = [...spacedData];

src/stores/data/releases.store.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ export const useReleasesStore = defineStore(ReleasesStoreConstants.Store, () =>
159159
});
160160

161161
const timeout = setTimeout(() => {
162-
if (mode === 'reload') releases.value = getEmptyWeeks(startDate, true);
162+
if (mode === 'reload') releases.value = getEmptyWeeks({ startDate, loading: true, days: days.value });
163163
else if (mode === 'start') releases.value = [getLoadingPlaceholder(DateUtils.previous(1, endDate)), ...releases.value];
164-
else if (mode === 'end') releases.value = [...releases.value, ...getEmptyWeeks(startDate, true)];
164+
else if (mode === 'end') releases.value = [...releases.value, ...getEmptyWeeks({ startDate, loading: true, days: days.value })];
165165
}, 100);
166166

167167
const query = {

src/utils/calendar.utils.ts

+59-38
Original file line numberDiff line numberDiff line change
@@ -29,61 +29,82 @@ export const getPlaceholder = (date: Date) => ({ ...CalendarPlaceholder, id: `em
2929
export const getLoadingPlaceholder = (date: Date) =>
3030
({ ...getPlaceholder(date), id: `loading-${date.getTime()}`, type: ListScrollItemType.loading }) as CalendarItem;
3131

32-
export const getEmptyWeeks = (fromDate: Date, loading?: boolean) => {
33-
return Array(14)
32+
export const getEmptyWeeks = ({ startDate, loading = false, days = 14 }: { startDate: Date; loading?: boolean; days?: number }) => {
33+
return Array(days)
3434
.fill(CalendarPlaceholder)
3535
.map((_, index) => {
36-
const date = DateUtils.next(index, fromDate);
36+
const date = DateUtils.next(index, startDate);
3737
return loading ? getLoadingPlaceholder(date) : getPlaceholder(date);
3838
});
3939
};
4040

41-
export const spaceDate = (data: CalendarItem[], startDate: Date, endDate: Date): CalendarItem[] => {
42-
const spacedData: CalendarItem[] = [];
43-
data?.forEach((item, index) => {
44-
if (index === 0) {
45-
// if the first item isn't the start date, add placeholders
46-
if (item.date.getTime() > startDate.getTime() && item.date.toLocaleDateString() !== startDate.toLocaleDateString()) {
47-
let previousDate: Date = item.date;
48-
while (previousDate.toLocaleDateString() !== startDate.toLocaleDateString()) {
49-
previousDate = DateUtils.previous(1, previousDate);
50-
spacedData.push(getPlaceholder(previousDate));
51-
}
52-
}
53-
return spacedData.push(item);
41+
const padStartInterval = (spacedData: CalendarItem[], { item, startDate, added = 0 }: { item: CalendarItem; startDate: Date; added?: number }) => {
42+
let _added = added;
43+
// if the first item isn't the start date, add placeholders
44+
if (item.date.getTime() > startDate.getTime() && item.date.toLocaleDateString() !== startDate.toLocaleDateString()) {
45+
let currentDate: Date = startDate;
46+
while (item.date.toLocaleDateString() !== currentDate.toLocaleDateString()) {
47+
_added += spacedData.push(getPlaceholder(currentDate));
48+
currentDate = DateUtils.next(1, currentDate);
5449
}
50+
}
51+
return _added;
52+
};
5553

56-
if (index === data.length - 1) {
57-
spacedData.push(item);
58-
59-
// if the last item isn't one day before the end date, add placeholders
60-
const dayBeforeEnd = DateUtils.previous(1, endDate);
61-
if (item.date.getTime() < dayBeforeEnd.getTime() && item.date.toLocaleDateString() !== dayBeforeEnd.toLocaleDateString()) {
62-
let nextDate: Date = item.date;
63-
while (nextDate.toLocaleDateString() !== dayBeforeEnd.toLocaleDateString()) {
64-
nextDate = DateUtils.next(1, nextDate);
65-
spacedData.push(getPlaceholder(nextDate));
66-
}
67-
}
68-
return;
54+
const padEndInterval = (spacedData: CalendarItem[], { item, endDate, added = 0 }: { item: CalendarItem; endDate: Date; added?: number }) => {
55+
let _added = added;
56+
// if the last item isn't one day before the end date, add placeholders
57+
if (item.date.getTime() < endDate.getTime() && item.date.toLocaleDateString() !== endDate.toLocaleDateString()) {
58+
let currentDate: Date = DateUtils.next(1, item.date);
59+
while (currentDate.toLocaleDateString() !== endDate.toLocaleDateString()) {
60+
_added += spacedData.push(getPlaceholder(currentDate));
61+
currentDate = DateUtils.next(1, currentDate);
6962
}
63+
}
64+
return _added;
65+
};
7066

71-
const previous = data[index - 1];
67+
const padBetweenInterval = (spacedData: CalendarItem[], { item, previous }: { previous: CalendarItem; item: CalendarItem }) => {
68+
// if the item us the same date as the previous one, no need to pad
69+
if (item.date.toLocaleDateString() === previous.date.toLocaleDateString()) return 0;
70+
71+
// if the item is the same as the day after the previous one, no need to pad
72+
let nextDate: Date = DateUtils.next(1, previous.date);
73+
if (item.date.toLocaleDateString() === nextDate.toLocaleDateString()) return 0;
74+
75+
let added = 0;
76+
// if the item isn't at least 1 day after the previous date, add placeholders
77+
while (item.date.toLocaleDateString() !== nextDate.toLocaleDateString()) {
78+
added += spacedData.push(getPlaceholder(nextDate));
79+
nextDate = DateUtils.next(1, nextDate);
80+
}
81+
return added;
82+
};
7283

73-
if (item.date.toLocaleDateString() === previous.date.toLocaleDateString()) return spacedData.push(item);
74-
if (item.date.toLocaleDateString() === DateUtils.next(1, previous.date).toLocaleDateString()) return spacedData.push(item);
84+
export const spaceDate = (data: CalendarItem[], { startDate, endDate, days }: { startDate: Date; endDate: Date; days?: number }): CalendarItem[] => {
85+
const spacedData: CalendarItem[] = [];
86+
data?.forEach((item, index, array) => {
87+
// if we are on the first item
88+
if (index === 0) {
89+
padStartInterval(spacedData, { item, startDate });
90+
// If we have more than 1 item, we can return (end padding in the next iteration)
91+
if (array.length > 1) return spacedData.push(item);
92+
}
7593

76-
// if the item isn't at least 1 day after the previous date, add placeholders
77-
let previousDate: Date = previous.date;
78-
while (item.date.toLocaleDateString() !== DateUtils.next(1, previousDate).toLocaleDateString()) {
79-
previousDate = DateUtils.next(1, previousDate);
80-
spacedData.push(getPlaceholder(previousDate));
94+
// if we have are at least on the 2 item
95+
if (index && array.length > 1) padBetweenInterval(spacedData, { item, previous: array[index - 1] });
96+
97+
// if we are on the last item
98+
if (index === array.length - 1) {
99+
spacedData.push(item);
100+
return padEndInterval(spacedData, { item, endDate });
81101
}
82102
spacedData.push(item);
83103
});
84-
if (!spacedData.length) spacedData.push(...getEmptyWeeks(startDate));
85104

86105
// if no data in response fill with placeholders
106+
if (!spacedData.length) spacedData.push(...getEmptyWeeks({ startDate, days }));
107+
87108
return spacedData;
88109
};
89110

0 commit comments

Comments
 (0)