Skip to content

Commit 4979193

Browse files
committed
fix(loading): fix calendar loading indicator
1 parent 0238c60 commit 4979193

6 files changed

+27
-9
lines changed

src/stores/data/history.store.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export const useHistoryStore = defineStore(HistoryStoreConstants.Store, () => {
110110

111111
Logger.debug('Fetching History', { page, limit, start, end });
112112
loading.value = true;
113-
const { clearLoading } = debounceLoading(history, loadingPlaceholder, !page);
113+
const { clearLoading } = debounceLoading(history, loadingPlaceholder, { clear: !page });
114114
const query: TraktHistoryGetQuery = {
115115
pagination: { page, limit },
116116
start_at: start?.toISOString(),

src/stores/data/list.store.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ export const useListStore = defineStore(ListStoreConstants.Store, () => {
346346
if (!parallel) loading.value = true;
347347
typeLoading[list.type] = true;
348348
listDictionaryLoading[list.id.toString()] = true;
349-
const { clearLoading } = debounceLoading(listItems, loadingPlaceholder, !page, 1000);
349+
const { clearLoading } = debounceLoading(listItems, loadingPlaceholder, { clear: !page, time: 1000 });
350350

351351
try {
352352
const response = await fetchItems(list, { pagination: { page, limit } });

src/stores/data/progress.store.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export const useProgressStore = defineStore('data.progress', () => {
114114

115115
Logger.debug('Fetching progress');
116116
loading.value = true;
117-
const { clearLoading } = debounceLoading(progress, loadingPlaceholder, true);
117+
const { clearLoading } = debounceLoading(progress, loadingPlaceholder, { clear: true });
118118
try {
119119
progress.value = await fetchProgressData();
120120
evicted.progress = false;

src/stores/data/search.store.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export const useSearchStore = defineStore(SearchStoreConstants.Store, () => {
134134
Logger.debug('Fetching search results', { types: types.value, query: query.value, search: search.value });
135135

136136
loading.value = true;
137-
const { clearLoading } = debounceLoading(searchResults, loadingPlaceholder, !page);
137+
const { clearLoading } = debounceLoading(searchResults, loadingPlaceholder, { clear: !page });
138138
const request: TraktSearch = {
139139
type: types.value,
140140
escape: !query.value,

src/utils/calendar.utils.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
type VirtualListRef,
1313
type VirtualListScrollToOptions,
1414
} from '~/models/list-scroll.model';
15+
import { defaultDebounceLoadingDelay } from '~/utils/store.utils';
1516

1617
export type CalendarItem = (TraktCalendarShow | TraktCalendarMovie | Record<never, never>) & {
1718
id: ListScrollItem['id'];
@@ -29,7 +30,7 @@ export const getPlaceholder = (date: Date) => ({ ...CalendarPlaceholder, id: `em
2930
export const getLoadingPlaceholder = (date: Date) =>
3031
({ ...getPlaceholder(date), id: `loading-${date.getTime()}`, type: ListScrollItemType.loading }) as CalendarItem;
3132

32-
export const getEmptyWeeks = ({ startDate, loading = false, days = 14 }: { startDate: Date; loading?: boolean; days?: number }) => {
33+
export const getEmptyWeeks = ({ startDate, loading = false, days = 14 }: { startDate: Date; loading?: boolean; days?: number }): CalendarItem[] => {
3334
return Array(days)
3435
.fill(CalendarPlaceholder)
3536
.map((_, index) => {
@@ -162,10 +163,17 @@ export const useCalendar = ({
162163

163164
const onScrollTop = async () => {
164165
const first = list.value[0];
165-
await fetchData('start');
166+
const fetching = fetchData('start');
166167

168+
const timeout = setTimeout(() => {
169+
listRef.value?.list.scrollTo({ top: 145 });
170+
}, defaultDebounceLoadingDelay); // default debounceLoading delay
171+
172+
await fetching;
173+
174+
clearTimeout(timeout);
167175
listRef.value?.list.scrollTo({
168-
top: (list.value.findIndex(item => item.id === first.id) - 1) * 145,
176+
top: list.value.findIndex(item => item.id === first.id) * 145,
169177
});
170178
};
171179

src/utils/store.utils.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,19 @@ export const useDebouncedSearch = (search: Ref<string>, delay = 350, disabled?:
127127
return debouncedSearch;
128128
};
129129

130-
export const debounceLoading = <T>(data: Ref<T[]>, placeholder: Ref<T[]>, clear?: boolean, time = 100) => {
130+
export const defaultDebounceLoadingDelay = 100;
131+
export type DebounceLoadingOptions<T> = { clear?: boolean; time?: number; splice?: (_data: T[], _placeholder: T[]) => T[] };
132+
export const debounceLoading = <T>(
133+
data: Ref<T[]>,
134+
placeholder: Ref<T[]>,
135+
{
136+
clear,
137+
time = defaultDebounceLoadingDelay,
138+
splice = (_data: T[], _placeholder: T[]) => [..._data, ..._placeholder],
139+
}: DebounceLoadingOptions<T> = {},
140+
) => {
131141
const timeout = setTimeout(() => {
132-
data.value = clear ? placeholder.value : [...data.value, ...placeholder.value];
142+
data.value = clear ? placeholder.value : splice(data.value, placeholder.value);
133143
}, time);
134144
return {
135145
timeout,

0 commit comments

Comments
 (0)