Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ability to Select Default Poster/Fanart/Banner. #678

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/core/rtkQuery/splitV3Api/seriesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ const seriesApi = splitV3Api.injectEndpoints({
query: ({ seriesId }) => ({
url: `Series/${seriesId}/Images`,
}),
providesTags: ['SeriesAniDB'],
}),

changeSeriesImage: build.mutation<
ResponseType,
{ seriesId: string, image: Pick<ImageType, 'ID' | 'Source' | 'Type'> }
>({
query: ({ image, seriesId }) => ({
url: `Series/${seriesId}/Images/${image.Type}`,
method: 'PUT',
body: { ID: image.ID, Source: image.Source },
}),
invalidatesTags: ['SeriesAniDB'],
}),
ElementalCrisis marked this conversation as resolved.
Show resolved Hide resolved

// Queue a refresh of all the TvDB data linked to a series using the seriesID.
Expand Down Expand Up @@ -230,6 +243,7 @@ const seriesApi = splitV3Api.injectEndpoints({
});

export const {
useChangeSeriesImageMutation,
useDeleteSeriesMutation,
useGetAniDBRecommendedAnimeQuery,
useGetAniDBRelatedQuery,
Expand Down
15 changes: 9 additions & 6 deletions src/pages/collection/Series.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@mdi/js';
import { Icon } from '@mdi/react';
import cx from 'classnames';
import { get, isArray } from 'lodash';
import { get } from 'lodash';

import BackgroundImagePlaceholderDiv from '@/components/BackgroundImagePlaceholderDiv';
import AnidbDescription from '@/components/Collection/AnidbDescription';
Expand All @@ -28,6 +28,7 @@ import {
import useMainPoster from '@/hooks/useMainPoster';

import type { CollectionGroupType } from '@/core/types/api/collection';
import type { ImageType } from '@/core/types/api/common';
import type { SeriesDetailsType } from '@/core/types/api/series';
import type { TagType } from '@/core/types/api/tags';

Expand Down Expand Up @@ -77,11 +78,13 @@ const Series = () => {
const group = groupData?.data ?? {} as CollectionGroupType;

useEffect(() => {
const fanarts = get(images, 'data.Fanarts', []);
if (!isArray(fanarts) || fanarts.length === 0) return;
const randomIdx = Math.floor(Math.random() * fanarts.length);
const randomImage = fanarts[randomIdx];
setFanartUri(`/api/v3/Image/${randomImage.Source}/${randomImage.Type}/${randomImage.ID}`);
const allFanarts: ImageType[] = get(images, 'data.Fanarts', []);
if (!Array.isArray(allFanarts) || allFanarts.length === 0) return;

const defaultFanart = allFanarts.find(fanart => fanart.Preferred);
if (defaultFanart) {
setFanartUri(`/api/v3/Image/${defaultFanart.Source}/${defaultFanart.Type}/${defaultFanart.ID}`);
}
}, [images, imagesData]);

if (!seriesId || !seriesData.isSuccess) return null;
Expand Down
35 changes: 27 additions & 8 deletions src/pages/collection/series/SeriesImages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ import BackgroundImagePlaceholderDiv from '@/components/BackgroundImagePlacehold
import Button from '@/components/Input/Button';
import Checkbox from '@/components/Input/Checkbox';
import ShokoPanel from '@/components/Panels/ShokoPanel';
import { useGetSeriesImagesQuery } from '@/core/rtkQuery/splitV3Api/seriesApi';
import toast from '@/components/Toast';
import { useChangeSeriesImageMutation, useGetSeriesImagesQuery } from '@/core/rtkQuery/splitV3Api/seriesApi';

import type { ImageType } from '@/core/types/api/common';

const Heading = React.memo(({ setType, type }: { type: string, setType: (type: string) => void }) => (
const Heading = React.memo((
{ onTypeChange, setType, type }: { type: string, setType: (type: string) => void, onTypeChange: () => void },
) => (
<div className="flex cursor-pointer items-center gap-x-2 text-xl font-semibold">
Images
<Icon path={mdiChevronRight} size={1} />
<div className="flex gap-x-1">
<span
onClick={() => {
setType('Posters');
onTypeChange();
}}
className={cx(type === 'Posters' && 'text-panel-text-primary')}
>
Poster
Posters
</span>
|
<span
Expand All @@ -33,7 +37,7 @@ const Heading = React.memo(({ setType, type }: { type: string, setType: (type: s
}}
className={cx(type === 'Fanarts' && 'text-panel-text-primary')}
>
Fanart
Fanarts
</span>
|
<span
Expand All @@ -60,8 +64,8 @@ const SeriesImages = () => {

const [type, setType] = useState('Posters');
const [selectedImage, setSelectedImage] = useState<ImageType>({} as ImageType);

const imagesData = useGetSeriesImagesQuery({ seriesId: seriesId! }, { skip: !seriesId });
const [changeImage] = useChangeSeriesImageMutation();
const images = imagesData.data;

const splitPath = split(selectedImage?.RelativeFilepath ?? '-', '/');
Expand All @@ -74,6 +78,10 @@ const SeriesImages = () => {
Banners: 'h-[8rem] w-[43.25rem]',
};

const resetSelectedImage = () => {
setSelectedImage({} as ImageType);
};

if (!seriesId) return null;

return (
Expand All @@ -96,16 +104,27 @@ const SeriesImages = () => {
<Button
buttonType="primary"
className="rounded-md border border-panel-border px-4 py-3 font-semibold"
disabled
disabled={!Object.keys(selectedImage).length || selectedImage.Preferred}
onClick={() => {
changeImage({
seriesId,
image: selectedImage,
})
.then(() => {
setSelectedImage({} as ImageType);
toast.success(`Series ${selectedImage.Type} image has been changed.`);
})
.catch(console.error);
}}
>
Set As Series Poster
{`Set As Default ${type.slice(0, -1)}`}
</Button>
</ShokoPanel>
</div>

<div className="flex grow flex-col gap-y-8">
<div className="flex items-center justify-between rounded-md border border-panel-border bg-panel-background-transparent px-8 py-4">
<Heading type={type} setType={setType} />
<Heading type={type} setType={setType} onTypeChange={resetSelectedImage} />
<div className="text-xl font-semibold">
<span className="text-panel-text-important">{get(images, type, []).length}</span>
&nbsp;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/collection/series/SeriesTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const SeriesTags = () => {
</div>
</div>
<div className="grid grid-cols-3 gap-8">
{map(tags, item => <SeriesTag item={item} />)}
{map(tags, item => <SeriesTag key={item.ID} item={item} />)}
</div>
</div>
</div>
Expand Down