Skip to content

Commit 0c9f95c

Browse files
committed
feat(ratings): adds rating addition & removal in store
1 parent a14e06d commit 0c9f95c

File tree

7 files changed

+227
-25
lines changed

7 files changed

+227
-25
lines changed

src/components/views/panel/MoviePanel.vue

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<script setup lang="ts">
22
import { deCapitalise } from '@dvcol/common-utils/common/string';
33
4-
import { TraktRatingType } from '@dvcol/trakt-http-client/models';
4+
import {
5+
TraktRatingType,
6+
type TraktSyncRatingValue,
7+
} from '@dvcol/trakt-http-client/models';
58
import { NFlex, NSkeleton } from 'naive-ui';
69
import { computed, onMounted, toRefs, watch } from 'vue';
710
@@ -256,7 +259,8 @@ const onCheckin = async (cancel: boolean) => {
256259
257260
const { openTab } = useLinksStore();
258261
259-
const { loadRatings, getRatings, getLoading } = useRatingsStore();
262+
const { loadRatings, getRatings, getLoading, addRating, removeRating } =
263+
useRatingsStore();
260264
261265
const scoreLoading = computed(() => getLoading(TraktRatingType.Movies));
262266
@@ -274,6 +278,18 @@ const ratingUrl = computed(() => {
274278
});
275279
});
276280
281+
const onScoreEdit = async (_score: TraktSyncRatingValue) => {
282+
if (!movie.value?.ids?.trakt) return;
283+
return (_score ? addRating : removeRating)(TraktRatingType.Movies, {
284+
movies: [
285+
{
286+
ids: movie.value.ids,
287+
rating: _score,
288+
},
289+
],
290+
});
291+
};
292+
277293
onMounted(() => {
278294
watch(
279295
movieId,
@@ -324,6 +340,7 @@ onMounted(() => {
324340
:loading-score="scoreLoading"
325341
:loading-rating="movieLoading"
326342
:url="ratingUrl"
343+
@on-score-edit="onScoreEdit"
327344
>
328345
<PanelPoster :tmdb="movie?.ids.tmdb" mode="movie" />
329346
</PanelStatistics>

src/components/views/panel/PanelScore.vue

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import { NFlex, NSkeleton } from 'naive-ui';
33
import { computed, ref, toRefs } from 'vue';
44
5+
import type { TraktSyncRatingValue } from '@dvcol/trakt-http-client/models';
6+
57
import ProgressNumber from '~/components/common/numbers/ProgressNumber.vue';
68
79
import TextField from '~/components/common/typography/TextField.vue';
@@ -31,7 +33,7 @@ const props = defineProps({
3133
});
3234
3335
const emit = defineEmits<{
34-
(e: 'onEdit', progress: number): void;
36+
(e: 'onEdit', progress: TraktSyncRatingValue): void;
3537
}>();
3638
3739
const i18n = useI18n('panel', 'ratings');
@@ -51,7 +53,8 @@ const _scoreLabel = computed(() => {
5153
});
5254
5355
const transformProgress = (progress: number) => Math.round(progress / 10) * 10;
54-
const onEdit = (_progress: number) => emit('onEdit', _progress);
56+
const onEdit = (_progress: number) =>
57+
emit('onEdit', (_progress / 10) as TraktSyncRatingValue);
5558
5659
const containerRef = ref<InstanceType<typeof TextField>>();
5760
</script>

src/components/views/panel/PanelStatistics.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script setup lang="ts">
22
import { NFlex } from 'naive-ui';
33
4+
import type { TraktSyncRatingValue } from '@dvcol/trakt-http-client/models';
5+
46
import PanelRating from '~/components/views/panel/PanelRating.vue';
57
import PanelScore from '~/components/views/panel/PanelScore.vue';
68
import { useExtensionSettingsStoreRefs } from '~/stores/settings/extension.store';
@@ -33,12 +35,12 @@ defineProps({
3335
});
3436
3537
const emit = defineEmits<{
36-
(e: 'onScoreEdit', progress: number): void;
38+
(e: 'onScoreEdit', progress: TraktSyncRatingValue): void;
3739
}>();
3840
3941
const { enableRatings } = useExtensionSettingsStoreRefs();
4042
41-
const onScoreEdit = (progress: number) => emit('onScoreEdit', progress);
43+
const onScoreEdit = (progress: TraktSyncRatingValue) => emit('onScoreEdit', progress);
4244
</script>
4345

4446
<template>

src/components/views/panel/ShowPanel.vue

+65-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<script setup lang="ts">
22
import { deCapitalise } from '@dvcol/common-utils/common/string';
3-
import { TraktRatingType, type TraktRatingTypes } from '@dvcol/trakt-http-client/models';
3+
import {
4+
TraktRatingType,
5+
type TraktRatingTypes,
6+
type TraktSyncRatingValue,
7+
} from '@dvcol/trakt-http-client/models';
48
import { NFlex, NSkeleton } from 'naive-ui';
59
610
import { computed, onMounted, toRefs, watch } from 'vue';
@@ -352,7 +356,8 @@ const onCheckin = async (cancel: boolean) => {
352356
await fetchShowProgress(showId.value, { force: true });
353357
};
354358
355-
const { loadRatings, getRatings, getLoading } = useRatingsStore();
359+
const { loadRatings, getRatings, getLoading, addRating, removeRating } =
360+
useRatingsStore();
356361
357362
const ratingLoading = computed(() => {
358363
if (panelType.value === 'episode') {
@@ -361,8 +366,8 @@ const ratingLoading = computed(() => {
361366
return getEpisodesLoading(showId.value, seasonNb.value, episodeNb.value).value;
362367
}
363368
if (panelType.value === 'season') {
364-
if (!showId?.value || seasonNb.value === undefined) return false;
365-
return getSeasonsLoading(showId.value, seasonNb.value).value;
369+
if (!showId?.value) return false;
370+
return getSeasonsLoading(showId.value).value;
366371
}
367372
if (!showId?.value) return false;
368373
return getShowLoading(showId.value).value;
@@ -427,6 +432,61 @@ const ratingUrl = computed(() => {
427432
});
428433
});
429434
435+
const votes = computed(() => {
436+
if (panelType.value === 'episode') return episode.value?.votes;
437+
if (panelType.value === 'season') return season.value?.votes;
438+
return show.value?.votes;
439+
});
440+
441+
const rating = computed(() => {
442+
if (panelType.value === 'episode') return episode.value?.rating;
443+
if (panelType.value === 'season') return season.value?.rating;
444+
return show.value?.rating;
445+
});
446+
447+
const onScoreEdit = async (_score: TraktSyncRatingValue) => {
448+
if (!show.value?.ids?.trakt) return;
449+
const addOrRemove = _score ? addRating : removeRating;
450+
if (panelType.value === 'episode') {
451+
if (!episode.value?.ids?.trakt) return;
452+
return addOrRemove(
453+
TraktRatingType.Episodes,
454+
{
455+
episodes: [
456+
{
457+
ids: episode.value.ids,
458+
rating: _score,
459+
},
460+
],
461+
},
462+
show.value.ids.trakt,
463+
);
464+
}
465+
if (panelType.value === 'season') {
466+
if (!season.value?.ids?.trakt) return;
467+
return addOrRemove(
468+
TraktRatingType.Seasons,
469+
{
470+
seasons: [
471+
{
472+
ids: season.value.ids,
473+
rating: _score,
474+
},
475+
],
476+
},
477+
show.value.ids.trakt,
478+
);
479+
}
480+
return addOrRemove(TraktRatingType.Shows, {
481+
shows: [
482+
{
483+
ids: show.value.ids,
484+
rating: _score,
485+
},
486+
],
487+
});
488+
};
489+
430490
onMounted(() => {
431491
watch(
432492
[showId, seasonNb, episodeNb],
@@ -459,18 +519,6 @@ onMounted(() => {
459519
);
460520
});
461521
462-
const votes = computed(() => {
463-
if (panelType.value === 'episode') return episode.value?.votes;
464-
if (panelType.value === 'season') return season.value?.votes;
465-
return show.value?.votes;
466-
});
467-
468-
const rating = computed(() => {
469-
if (panelType.value === 'episode') return episode.value?.rating;
470-
if (panelType.value === 'season') return season.value?.rating;
471-
return show.value?.rating;
472-
});
473-
474522
const { openTab } = useLinksStore();
475523
</script>
476524

@@ -499,6 +547,7 @@ const { openTab } = useLinksStore();
499547
:url="ratingUrl"
500548
:loading-score="scoreLoading"
501549
:loading-rating="ratingLoading"
550+
@on-score-edit="onScoreEdit"
502551
>
503552
<PanelPoster
504553
:tmdb="show?.ids.tmdb"

src/i18n/en/common/rating.json

+16
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,21 @@
4242
"common__rating__not_rated": {
4343
"message": "Not rated",
4444
"description": "Rating for a value that has not been rated"
45+
},
46+
"common__rating__rating_added_success": {
47+
"message": "Rating successfully saved",
48+
"description": "Success message"
49+
},
50+
"common__rating__rating_added_error": {
51+
"message": "An error occurred while saving the rating",
52+
"description": "Error message"
53+
},
54+
"common__rating__rating_remove_success": {
55+
"message": "Rating successfully removed",
56+
"description": "Success message"
57+
},
58+
"common__rating__rating_remove_error": {
59+
"message": "An error occurred while removing the rating",
60+
"description": "Error message"
4561
}
4662
}

src/services/trakt.service.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,9 @@ export class TraktService {
634634
add: async (query: TraktRatingRequest) => {
635635
const response = await TraktService.traktClient.sync.ratings.add(query);
636636
TraktService.traktClient.sync.ratings.get.cached.evict().catch(err => logger.error('Failed to evict ratings cache', { query, err }));
637-
return response.json();
637+
const { added, not_found } = await response.json();
638+
if (Object.values(not_found).some(value => value?.length)) throw not_found;
639+
return added;
638640
},
639641
remove: async (query: TraktSyncRequest) => {
640642
const response = await TraktService.traktClient.sync.ratings.remove(query);

0 commit comments

Comments
 (0)