Skip to content

Commit df788d3

Browse files
committed
fix(list): change key to index since trakt api can return duplicates
1 parent 71757d8 commit df788d3

File tree

8 files changed

+28
-12
lines changed

8 files changed

+28
-12
lines changed

src/components/common/buttons/use-back-to-top.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { ref } from 'vue';
22

3-
import type { VirtualListRef } from '~/models/list-scroll.model';
3+
import type { ScrollTo, VirtualListRef } from '~/models/list-scroll.model';
44

55
export const useBackToTop = () => {
66
const listRef = ref<{ list: VirtualListRef }>();
77
const scrolled = ref(false);
88

9-
const onClick = () => {
10-
listRef.value?.list?.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
9+
const onClick = (scrollTo?: ScrollTo) => {
10+
listRef.value?.list?.scrollTo({ top: 0, left: 0, behavior: 'smooth', ...scrollTo });
1111
scrolled.value = false;
1212
};
1313

src/components/common/list/ListItem.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,11 @@ const onClick = () => emit('onItemClick', { item: item?.value });
141141
:style="{
142142
'--list-item-height': itemHeight,
143143
}"
144-
:data-key="item.id"
144+
:data-id="item.id"
145145
:data-index="item.index"
146+
:data-key="item.key"
146147
:data-type="item.type"
148+
:aria-label="item.title"
147149
:line-type="loading ? 'dashed' : lineType"
148150
:color="loading ? 'grey' : color"
149151
@click="onClick"

src/components/common/list/ListScroll.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ const onLoadMore = (payload: { page: number; pageCount: number; pageSize: number
144144
}"
145145
:padding-top="listOptions?.paddingTop ?? 60"
146146
:padding-bottom="listOptions?.paddingBottom ?? 32"
147-
:key-field="'id'"
147+
:key-field="'key'"
148148
@scroll="onScrollHandler"
149149
@vue:updated="onUpdatedHandler"
150150
>

src/components/common/list/use-list-scroll.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export const useListScroll = <T extends ListScrollSourceItemWithDate<D>, D exten
143143
const _item: ListScrollItem = {
144144
...item,
145145
index,
146+
key: `${index}-${item.id}`,
146147
loading: (typeof item.id === 'number' && item.id < 0) || item.type === ListScrollItemType.loading,
147148
};
148149

@@ -225,7 +226,9 @@ export const addLoadMore = (
225226
if (!pagination.value?.pageCount) return array;
226227
if (pagination.value.page === pagination.value.pageCount) return array;
227228
if (array.length && array[array.length - 1].type === ListScrollItemType.loadMore) return array;
228-
const loadMore: ListScrollItem = { id: ListScrollItemType.loadMore, type: ListScrollItemType.loadMore, index: items.value.length };
229+
const type = ListScrollItemType.loadMore;
230+
const index = items.value.length;
231+
const loadMore: ListScrollItem = { id: type, type, index, key: `${index}-${type}` };
229232
return [...array, loadMore];
230233
});
231234
};

src/components/views/watchlist/WatchlistComponent.vue

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
type AnyList,
1616
anyListDateGetter,
1717
type AnyListDateTypes,
18+
useListsStoreRefs,
1819
useListStore,
1920
useListStoreRefs,
2021
} from '~/stores/data/list.store';
@@ -41,6 +42,10 @@ const {
4142
searchList,
4243
} = useListStoreRefs();
4344
const { fetchListItems, clearState } = useListStore();
45+
const { activeList } = useListsStoreRefs();
46+
47+
const { scrolled, listRef, onClick } = useBackToTop();
48+
const { onItemClick } = usePanelItem();
4449
4550
watchUserChange({
4651
fetch: fetchListItems,
@@ -51,6 +56,7 @@ onMounted(() => {
5156
watch(panel, async value => {
5257
if (!value) await fetchListItems();
5358
});
59+
watch(activeList, () => onClick());
5460
});
5561
5662
const list = useListScroll<AnyList, AnyListDateTypes>(
@@ -66,9 +72,6 @@ const { onScroll, onUpdated, onLoadMore } = useListScrollEvents(fetchListItems,
6672
loading: listLoading,
6773
belowThreshold,
6874
});
69-
70-
const { scrolled, listRef, onClick } = useBackToTop();
71-
const { onItemClick } = usePanelItem();
7275
</script>
7376

7477
<template>

src/models/list-scroll.model.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { TraktShow } from '~/models/trakt/trakt-show.model';
1313
import type { BaseCacheOption } from '~/services/common/base-client';
1414

1515
export type VirtualListRef = VirtualListInst & InstanceType<typeof NVirtualList>;
16+
export type ScrollTo = Parameters<VirtualListInst['scrollTo']>[0];
1617
export type VirtualListProps = {
1718
itemSize?: number;
1819
visibleItemsTag?: string | ObjectConstructor;
@@ -83,6 +84,7 @@ export const ListScrollItemType = {
8384
export type ListScrollItem<T = Record<string, any>> = Omit<PosterItem, 'type'> & {
8485
id: string | number;
8586
index: number;
87+
key: string;
8688

8789
type?: (typeof ListScrollItemType)[keyof typeof ListScrollItemType];
8890
title?: string;

src/stores/data/list.store.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ export const useListStore = defineStore('data.list', () => {
214214
pagination.value = undefined;
215215
searchList.value = '';
216216

217+
Object.assign(typeLoading, {});
218+
Object.assign(typeItemLoading, {});
219+
217220
Object.assign(listDictionary, {});
218221
Object.assign(listDictionaryLoading, {});
219222
};
@@ -257,7 +260,7 @@ export const useListStore = defineStore('data.list', () => {
257260
}
258261
if (firstLoad.value) firstLoad.value = false;
259262

260-
console.info('Fetching List', list);
263+
console.info('Fetching List', { list, page, limit });
261264

262265
loading.value = true;
263266
typeLoading[list.type] = true;
@@ -287,7 +290,7 @@ export const useListStore = defineStore('data.list', () => {
287290
} else {
288291
throw new Error('Invalid list type');
289292
}
290-
const newData = response.data.map((item, index) => {
293+
const newData = response.data.map<AnyList>((item, index) => {
291294
updateDictionary(list, item as MinimalItem);
292295
if ('id' in item) return item;
293296
return { ...item, id: `${page}-${index}` };

src/stores/data/progress.store.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ export const progressToListItem = (progress: ProgressItem, index: number): Progr
3737

3838
const poster = progress.fanart ?? progress.screenshot;
3939

40+
const id = Number(progress.episodeId ?? progress.seasonId ?? progress.showId);
41+
4042
return {
41-
id: Number(progress.episodeId ?? progress.seasonId ?? progress.showId),
43+
id,
4244
index,
45+
key: `${index}-${id}`,
4346
title: getTitle({ show, episode }),
4447
content: getContent({ show, episode }),
4548
poster,

0 commit comments

Comments
 (0)