1
1
<script setup lang="ts">
2
- import { computed , type PropType , toRefs } from ' vue' ;
2
+ import { computed , type PropType , toRefs , watch } from ' vue' ;
3
3
4
4
import type { ButtonProps , IconProps } from ' naive-ui' ;
5
5
@@ -9,15 +9,20 @@ import IconRestore from '~/components/icons/IconRestore.vue';
9
9
import { type ListScrollItem } from ' ~/models/list-scroll.model' ;
10
10
import { isListItemType , ListType } from ' ~/models/list.model' ;
11
11
import { useItemCollected } from ' ~/stores/composable/use-item-played' ;
12
- import { useWatchedUpdates } from ' ~/stores/composable/use-list-update' ;
12
+ import {
13
+ type AddOrRemoveQuery ,
14
+ useWatchedUpdates ,
15
+ } from ' ~/stores/composable/use-list-update' ;
13
16
import { useListStore } from ' ~/stores/data/list.store' ;
17
+ import { useMovieStoreRefs } from ' ~/stores/data/movie.store' ;
18
+ import { useShowStore } from ' ~/stores/data/show.store' ;
14
19
import {
15
20
QuickActionDate ,
16
21
type QuickActionDates ,
17
22
} from ' ~/stores/settings/extension.store' ;
18
23
import { useI18n } from ' ~/utils/i18n.utils' ;
19
24
20
- const i18n = useI18n (' list' , ' watched ' );
25
+ const i18n = useI18n (' list' , ' button ' , ' collected ' );
21
26
22
27
const props = defineProps ({
23
28
item: {
@@ -32,6 +37,10 @@ const props = defineProps({
32
37
type: Boolean ,
33
38
required: false ,
34
39
},
40
+ disableFetch: {
41
+ type: Boolean ,
42
+ required: false ,
43
+ },
35
44
buttonProps: {
36
45
type: Object as PropType <ButtonProps >,
37
46
required: false ,
@@ -42,47 +51,76 @@ const props = defineProps({
42
51
},
43
52
});
44
53
45
- const { disabled, item, dateType } = toRefs (props );
54
+ const emit = defineEmits <{
55
+ (e : ' onClick' , payload : { query: AddOrRemoveQuery ; request: Promise <unknown > }): void ;
56
+ }>();
57
+
58
+ const { disabled, disableFetch, item, dateType } = toRefs (props );
46
59
47
60
const { collected, date : dateCollected } = useItemCollected (item );
48
61
49
62
const { isItemListLoading } = useListStore ();
50
63
51
- const isLoading = computed (() => {
52
- if (! item .value ?.id ) return ;
64
+ const isListLoading = computed (() => {
53
65
if (! isListItemType (item .value .type )) return ;
66
+ const itemId = item .value .meta ?.ids ?.[item .value .type ]?.trakt ;
67
+ if (! itemId ) return false ;
54
68
return isItemListLoading ({
55
69
listType: ListType .Collection ,
56
70
itemType: item .value .type ,
57
- itemId: item . value ?. id ,
58
- }). value ;
71
+ itemId ,
72
+ });
59
73
});
60
74
61
- const { addOrRemoveCollected } = useWatchedUpdates ();
75
+ const { loadingCollected } = useMovieStoreRefs ();
76
+ const { getShowCollectionLoading } = useShowStore ();
62
77
63
- const onClick = () => {
78
+ const isLoading = computed (() => {
79
+ if (isListLoading .value ) return true ;
80
+ if (item .value .type === ' movie' ) return loadingCollected .value ;
81
+ if (! item .value ?.meta ?.ids ?.show ?.trakt ) return false ;
82
+ return getShowCollectionLoading (item .value ?.meta ?.ids ?.show ?.trakt );
83
+ });
84
+
85
+ const { addOrRemoveCollected, fetchCollection } = useWatchedUpdates ();
86
+
87
+ const getQuery = (): AddOrRemoveQuery | undefined => {
64
88
if (! isListItemType (item .value .type )) return ;
65
89
const trakt = item .value ?.meta ?.ids ?.[item .value .type ]?.trakt ;
66
90
if (! trakt ) return ;
67
91
68
- let date: Date | string | undefined ;
69
- if (dateType ?.value === QuickActionDate .Release ) {
70
- date = item .value ?.meta ?.released [item .value .type ];
71
- }
72
-
73
- return addOrRemoveCollected ({
92
+ return {
74
93
itemIds: { trakt },
75
94
itemType: item .value .type ,
76
95
remove: !! collected .value ,
77
96
showId: item .value ?.meta ?.ids ?.show ?.trakt ,
78
- date ,
79
- });
97
+ };
80
98
};
99
+
100
+ const onClick = () => {
101
+ if (! isListItemType (item .value .type )) return ;
102
+ const query = getQuery ();
103
+ if (! query ) return ;
104
+
105
+ if (dateType ?.value === QuickActionDate .Release ) {
106
+ query .date = item .value ?.meta ?.released ?.[item .value .type ];
107
+ }
108
+
109
+ const request = addOrRemoveCollected (query );
110
+ emit (' onClick' , { query , request });
111
+ };
112
+
113
+ watch (disabled , () => {
114
+ if (disabled .value || disableFetch .value ) return ;
115
+ const query = getQuery ();
116
+ if (! query ) return ;
117
+ return fetchCollection (query );
118
+ });
81
119
</script >
82
120
83
121
<template >
84
122
<ListButton
85
- :disabled =" isLoading || disabled"
123
+ :disabled =" disabled"
86
124
:loading =" isLoading"
87
125
:colored =" !!collected"
88
126
:button-props =" { type: 'info', ...buttonProps }"
@@ -93,6 +131,6 @@ const onClick = () => {
93
131
"
94
132
@on-click =" onClick"
95
133
>
96
- <span >{{ i18n(collected ? 'remove' : 'collected ', 'common', 'button') }}</span >
134
+ <span >{{ i18n(collected ? 'remove' : 'collect ', 'common', 'button') }}</span >
97
135
</ListButton >
98
136
</template >
0 commit comments