1
1
import { defineStore , storeToRefs } from 'pinia' ;
2
- import { ref , watch } from 'vue' ;
2
+ import { computed , reactive , ref , watch } from 'vue' ;
3
3
4
4
import type { TraktClientPagination } from '~/models/trakt/trakt-client.model' ;
5
5
@@ -168,6 +168,9 @@ export const useListsStore = defineStore('data.lists', () => {
168
168
169
169
export const useListsStoreRefs = ( ) => storeToRefs ( useListsStore ( ) ) ;
170
170
171
+ type ListDictionary = Record < string , Record < string , boolean > > ;
172
+ type ListDictionaryLoading = Record < string , boolean > ;
173
+
171
174
export const useListStore = defineStore ( 'data.list' , ( ) => {
172
175
const firstLoad = ref ( true ) ;
173
176
const loading = ref ( true ) ;
@@ -178,6 +181,9 @@ export const useListStore = defineStore('data.list', () => {
178
181
const searchList = ref ( '' ) ;
179
182
const threshold = ref ( 10 ) ;
180
183
184
+ const listDictionary = reactive < ListDictionary > ( { } ) ;
185
+ const listDictionaryLoading = reactive < ListDictionaryLoading > ( { } ) ;
186
+
181
187
const saveState = async ( ) => storage . local . set ( 'data.list.page-size' , pageSize . value ) ;
182
188
const restoreState = async ( ) => {
183
189
const restored = await storage . local . get < number > ( 'data.list.page-size' ) ;
@@ -189,6 +195,23 @@ export const useListStore = defineStore('data.list', () => {
189
195
listItems . value = [ ] ;
190
196
pagination . value = undefined ;
191
197
searchList . value = '' ;
198
+
199
+ Object . assign ( listDictionary , { } ) ;
200
+ Object . assign ( listDictionaryLoading , { } ) ;
201
+ } ;
202
+
203
+ const addToDictionary = ( list : ListEntity , item : AnyList ) => {
204
+ if ( ! [ ListType . List , ListType . Watchlist ] . map ( String ) . includes ( list . type ) ) return ;
205
+ if ( ! listDictionary [ list . id ] ) listDictionary [ list . id ] = { } ;
206
+ if ( 'movie' in item && item . movie ?. ids ?. trakt ) {
207
+ listDictionary [ list . id ] [ item . movie . ids . trakt ] = true ;
208
+ } else if ( 'show' in item && item . show ?. ids ?. trakt ) {
209
+ listDictionary [ list . id ] [ item . show . ids . trakt ] = true ;
210
+ } else if ( 'season' in item && item . season ?. ids ?. trakt ) {
211
+ listDictionary [ list . id ] [ item . season . ids . trakt ] = true ;
212
+ } else if ( 'episode' in item && item . episode ?. ids ?. trakt ) {
213
+ listDictionary [ list . id ] [ item . episode . ids . trakt ] = true ;
214
+ }
192
215
} ;
193
216
194
217
const { activeList } = useListsStoreRefs ( ) ;
@@ -212,6 +235,7 @@ export const useListStore = defineStore('data.list', () => {
212
235
213
236
console . info ( 'Fetching List' , list ) ;
214
237
loading . value = true ;
238
+ listDictionaryLoading [ list . id ] = true ;
215
239
const timeout = debounceLoading ( listItems , loadingPlaceholder , ! page ) ;
216
240
try {
217
241
const query = {
@@ -238,6 +262,7 @@ export const useListStore = defineStore('data.list', () => {
238
262
throw new Error ( 'Invalid list type' ) ;
239
263
}
240
264
const newData = response . data . map ( ( item , index ) => {
265
+ addToDictionary ( list , item as AnyList ) ;
241
266
if ( 'id' in item ) return item ;
242
267
return { ...item , id : `${ page } -${ index } ` } ;
243
268
} ) ;
@@ -251,9 +276,13 @@ export const useListStore = defineStore('data.list', () => {
251
276
} finally {
252
277
clearTimeout ( timeout ) ;
253
278
loading . value = false ;
279
+ listDictionaryLoading [ list . id ] = false ;
254
280
}
255
281
} ;
256
282
283
+ const isListLoading = ( listId : ListEntity [ 'id' ] ) => computed ( ( ) => listDictionaryLoading [ listId ] ) ;
284
+ const isItemInList = ( listId : ListEntity [ 'id' ] , itemId : string | number ) => computed ( ( ) => listDictionary [ listId ] ?. [ itemId ] ) ;
285
+
257
286
const initListStore = async ( ) => {
258
287
await restoreState ( ) ;
259
288
@@ -281,6 +310,8 @@ export const useListStore = defineStore('data.list', () => {
281
310
fetchListItems,
282
311
filteredListItems,
283
312
initListStore,
313
+ isListLoading,
314
+ isItemInList,
284
315
} ;
285
316
} ) ;
286
317
0 commit comments