48
resources/client/episodes/requests/use-create-episode.ts
Executable file
48
resources/client/episodes/requests/use-create-episode.ts
Executable file
@@ -0,0 +1,48 @@
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {apiClient, queryClient} from '@common/http/query-client';
|
||||
import {showHttpErrorToast} from '@common/utils/http/show-http-error-toast';
|
||||
import {UseFormReturn} from 'react-hook-form';
|
||||
import {onFormQueryError} from '@common/errors/on-form-query-error';
|
||||
import {seasonQueryKey} from '@app/seasons/requests/use-season';
|
||||
import {Episode} from '@app/titles/models/episode';
|
||||
|
||||
interface Response extends BackendResponse {
|
||||
episode: Episode;
|
||||
}
|
||||
|
||||
export interface CreateEpisodePayload {
|
||||
name: string;
|
||||
description: string;
|
||||
release_date: string;
|
||||
runtime: number;
|
||||
popularity: number;
|
||||
poster: string;
|
||||
}
|
||||
|
||||
export function useCreateEpisode(
|
||||
form: UseFormReturn<CreateEpisodePayload>,
|
||||
titleId: number,
|
||||
season: number | string,
|
||||
) {
|
||||
return useMutation({
|
||||
mutationFn: (payload: CreateEpisodePayload) =>
|
||||
createEpisode(titleId, season, payload),
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: seasonQueryKey(titleId, season),
|
||||
});
|
||||
},
|
||||
onError: r => (form ? onFormQueryError(r, form) : showHttpErrorToast(r)),
|
||||
});
|
||||
}
|
||||
|
||||
function createEpisode(
|
||||
titleId: number,
|
||||
season: number | string,
|
||||
payload: CreateEpisodePayload,
|
||||
): Promise<Response> {
|
||||
return apiClient
|
||||
.post(`titles/${titleId}/seasons/${season}/episodes`, payload)
|
||||
.then(r => r.data);
|
||||
}
|
||||
27
resources/client/episodes/requests/use-delete-episode.ts
Executable file
27
resources/client/episodes/requests/use-delete-episode.ts
Executable file
@@ -0,0 +1,27 @@
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {apiClient, queryClient} from '@common/http/query-client';
|
||||
import {showHttpErrorToast} from '@common/utils/http/show-http-error-toast';
|
||||
import {toast} from '@common/ui/toast/toast';
|
||||
import {message} from '@common/i18n/message';
|
||||
import {Episode} from '@app/titles/models/episode';
|
||||
import {seasonQueryKey} from '@app/seasons/requests/use-season';
|
||||
|
||||
interface Response extends BackendResponse {}
|
||||
|
||||
export function useDeleteEpisode(episode: Episode) {
|
||||
return useMutation({
|
||||
mutationFn: () => deleteEpisode(episode.id),
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: seasonQueryKey(episode.title_id, episode.season_number),
|
||||
});
|
||||
toast(message('Episode deleted'));
|
||||
},
|
||||
onError: r => showHttpErrorToast(r),
|
||||
});
|
||||
}
|
||||
|
||||
function deleteEpisode(seasonId: number | string): Promise<Response> {
|
||||
return apiClient.delete(`episodes/${seasonId}`).then(r => r.data);
|
||||
}
|
||||
55
resources/client/episodes/requests/use-episode.ts
Executable file
55
resources/client/episodes/requests/use-episode.ts
Executable file
@@ -0,0 +1,55 @@
|
||||
import {useQuery} from '@tanstack/react-query';
|
||||
import {apiClient} from '@common/http/query-client';
|
||||
import {useParams} from 'react-router-dom';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {Title} from '@app/titles/models/title';
|
||||
import {Episode} from '@app/titles/models/episode';
|
||||
import {seasonQueryKey} from '@app/seasons/requests/use-season';
|
||||
import {GroupTitleCredits} from '@app/titles/requests/use-title';
|
||||
import {getBootstrapData} from '@common/core/bootstrap-data/use-backend-bootstrap-data';
|
||||
|
||||
export interface GetEpisodeResponse extends BackendResponse {
|
||||
episode: Episode;
|
||||
title: Title;
|
||||
credits?: GroupTitleCredits;
|
||||
}
|
||||
|
||||
export function useEpisode(
|
||||
loader: 'episodePage' | 'episodeCreditsPage' | 'episode',
|
||||
) {
|
||||
const {titleId, season, episode} = useParams();
|
||||
return useQuery({
|
||||
queryKey: [
|
||||
...seasonQueryKey(titleId!, season!),
|
||||
'episodes',
|
||||
`${episode}`,
|
||||
loader,
|
||||
],
|
||||
queryFn: () => fetchEpisode(titleId!, season!, episode!, loader),
|
||||
initialData: () => {
|
||||
const data = getBootstrapData().loaders?.[loader];
|
||||
if (
|
||||
data?.title.id == titleId &&
|
||||
data?.episode.season_number == season &&
|
||||
data?.episode.episode_number == episode
|
||||
) {
|
||||
return data;
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function fetchEpisode(
|
||||
titleId: string,
|
||||
seasonNumber: string,
|
||||
episodeNumber: string,
|
||||
loader: string,
|
||||
) {
|
||||
return apiClient
|
||||
.get<GetEpisodeResponse>(
|
||||
`titles/${titleId}/seasons/${seasonNumber}/episodes/${episodeNumber}`,
|
||||
{params: {loader}},
|
||||
)
|
||||
.then(response => response.data);
|
||||
}
|
||||
41
resources/client/episodes/requests/use-update-episode.ts
Executable file
41
resources/client/episodes/requests/use-update-episode.ts
Executable file
@@ -0,0 +1,41 @@
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {apiClient, queryClient} from '@common/http/query-client';
|
||||
import {UseFormReturn} from 'react-hook-form';
|
||||
import {onFormQueryError} from '@common/errors/on-form-query-error';
|
||||
import {seasonQueryKey} from '@app/seasons/requests/use-season';
|
||||
import {CreateEpisodePayload} from '@app/episodes/requests/use-create-episode';
|
||||
import {Episode} from '@app/titles/models/episode';
|
||||
|
||||
interface Response extends BackendResponse {
|
||||
episode: Episode;
|
||||
}
|
||||
|
||||
export function useUpdateEpisode(
|
||||
titleId: number | string,
|
||||
season: number | string,
|
||||
episode: number | string,
|
||||
form: UseFormReturn<CreateEpisodePayload>,
|
||||
) {
|
||||
return useMutation({
|
||||
mutationFn: (payload: CreateEpisodePayload) =>
|
||||
updateEpisode(titleId, season, episode, payload),
|
||||
onSuccess: async ({episode}) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: seasonQueryKey(episode.title_id, episode.season_number),
|
||||
});
|
||||
},
|
||||
onError: r => onFormQueryError(r, form),
|
||||
});
|
||||
}
|
||||
|
||||
function updateEpisode(
|
||||
titleId: number | string,
|
||||
season: number | string,
|
||||
episode: number | string,
|
||||
payload: CreateEpisodePayload,
|
||||
): Promise<Response> {
|
||||
return apiClient
|
||||
.put(`titles/${titleId}/seasons/${season}/episodes/${episode}`, payload)
|
||||
.then(r => r.data);
|
||||
}
|
||||
Reference in New Issue
Block a user