Use gql for "updating mangas"

This commit is contained in:
schroda
2023-09-01 20:33:35 +02:00
parent e1f338d013
commit c75e184c57
3 changed files with 70 additions and 40 deletions

View File

@@ -152,12 +152,16 @@ const MangaDetails: React.FC<IProps> = ({ manga }) => {
const addToLibrary = () => { const addToLibrary = () => {
mutate(`/api/v1/manga/${manga.id}`, { ...manga, inLibrary: true }, { revalidate: false }); mutate(`/api/v1/manga/${manga.id}`, { ...manga, inLibrary: true }, { revalidate: false });
requestManager.addMangaToLibrary(manga.id).response.then(() => mutate(`/api/v1/manga/${manga.id}`)); requestManager
.updateManga(manga.id, { inLibrary: true })
.response.then(() => mutate(`/api/v1/manga/${manga.id}`));
}; };
const removeFromLibrary = () => { const removeFromLibrary = () => {
mutate(`/api/v1/manga/${manga.id}`, { ...manga, inLibrary: false }, { revalidate: false }); mutate(`/api/v1/manga/${manga.id}`, { ...manga, inLibrary: false }, { revalidate: false });
requestManager.removeMangaFromLibrary(manga.id).response.then(() => mutate(`/api/v1/manga/${manga.id}`)); requestManager
.updateManga(manga.id, { inLibrary: false })
.response.then(() => mutate(`/api/v1/manga/${manga.id}`));
}; };
return ( return (

View File

@@ -31,6 +31,7 @@ export default function CategorySelect(props: IProps) {
const { data: mangaCategoriesData, mutate } = requestManager.useGetMangaCategories(mangaId); const { data: mangaCategoriesData, mutate } = requestManager.useGetMangaCategories(mangaId);
const { data: categoriesData } = requestManager.useGetCategories(); const { data: categoriesData } = requestManager.useGetCategories();
const [triggerMutate] = requestManager.useUpdateMangaCategories();
const allCategories = useMemo(() => { const allCategories = useMemo(() => {
const cats = [...(categoriesData ?? [])]; // make copy const cats = [...(categoriesData ?? [])]; // make copy
@@ -53,10 +54,18 @@ export default function CategorySelect(props: IProps) {
const handleChange = (event: React.ChangeEvent<HTMLInputElement>, categoryId: number) => { const handleChange = (event: React.ChangeEvent<HTMLInputElement>, categoryId: number) => {
const { checked } = event.target as HTMLInputElement; const { checked } = event.target as HTMLInputElement;
(checked // TODO - update to only update categories when clicking OK - can now be updated in one go with graphql
? requestManager.addMangaToCategory(mangaId, categoryId) triggerMutate({
: requestManager.removeMangaFromCategory(mangaId, categoryId) variables: {
).response.then(() => mutate()); input: {
id: mangaId,
patch: {
addToCategories: checked ? [categoryId] : [],
removeFromCategories: !checked ? [categoryId] : [],
},
},
},
}).then(() => mutate());
}; };
return ( return (

View File

@@ -58,9 +58,15 @@ import {
InstallExternalExtensionMutationVariables, InstallExternalExtensionMutationVariables,
SetGlobalMetadataMutation, SetGlobalMetadataMutation,
SetGlobalMetadataMutationVariables, SetGlobalMetadataMutationVariables,
SetMangaMetadataMutation,
UpdateExtensionMutation, UpdateExtensionMutation,
UpdateExtensionMutationVariables, UpdateExtensionMutationVariables,
UpdateExtensionPatchInput, UpdateExtensionPatchInput,
UpdateMangaCategoriesMutation,
UpdateMangaCategoriesMutationVariables,
UpdateMangaMutation,
UpdateMangaMutationVariables,
UpdateMangaPatchInput,
} from '@/lib/graphql/generated/graphql.ts'; } from '@/lib/graphql/generated/graphql.ts';
import { GET_GLOBAL_METADATA, GET_GLOBAL_METADATAS } from '@/lib/graphql/queries/GlobalMetadataQuery.ts'; import { GET_GLOBAL_METADATA, GET_GLOBAL_METADATAS } from '@/lib/graphql/queries/GlobalMetadataQuery.ts';
import { SET_GLOBAL_METADATA } from '@/lib/graphql/mutations/GlobalMetadataMutation.ts'; import { SET_GLOBAL_METADATA } from '@/lib/graphql/mutations/GlobalMetadataMutation.ts';
@@ -72,6 +78,10 @@ import {
UPDATE_EXTENSION, UPDATE_EXTENSION,
} from '@/lib/graphql/mutations/ExtensionMutation.ts'; } from '@/lib/graphql/mutations/ExtensionMutation.ts';
import { GET_SOURCE, GET_SOURCES } from '@/lib/graphql/queries/SourceQuery.ts'; import { GET_SOURCE, GET_SOURCES } from '@/lib/graphql/queries/SourceQuery.ts';
import { SET_MANGA_METADATA, UPDATE_MANGA, UPDATE_MANGA_CATEGORIES } from '@/lib/graphql/mutations/MangaMutation.ts';
import { GET_MANGA, GET_MANGAS } from '@/lib/graphql/queries/MangaQuery.ts';
import { GET_CATEGORIES, GET_CATEGORY, GET_CATEGORY_MANGAS } from '@/lib/graphql/queries/CategoryQuery.ts';
import { GET_SOURCE_MANGAS_FETCH } from '@/lib/graphql/mutations/SourceMutation.ts';
enum SWRHttpMethod { enum SWRHttpMethod {
SWR_GET, SWR_GET,
@@ -661,24 +671,51 @@ export class RequestManager {
return this.doRequest(HttpMethod.SWR_GET, `manga/${mangaId}/category`, { swrOptions }); return this.doRequest(HttpMethod.SWR_GET, `manga/${mangaId}/category`, { swrOptions });
} }
public addMangaToCategory(mangaId: number, categoryId: number): AbortableAxiosResponse { public useUpdateMangaCategories(
return this.doRequest(HttpMethod.GET, `manga/${mangaId}/category/${categoryId}`); options?: MutationHookOptions<UpdateMangaCategoriesMutation, UpdateMangaCategoriesMutationVariables>,
): AbortableApolloUseMutationResponse<UpdateMangaCategoriesMutation, UpdateMangaCategoriesMutationVariables> {
return this.doRequestNew(GQLMethod.USE_MUTATION, UPDATE_MANGA_CATEGORIES, undefined, {
refetchQueries: [GET_MANGA, GET_MANGAS, GET_CATEGORY, GET_CATEGORIES, GET_CATEGORY_MANGAS],
...options,
});
} }
public removeMangaFromCategory(mangaId: number, categoryId: number): AbortableAxiosResponse { public updateManga(
return this.doRequest(HttpMethod.DELETE, `manga/${mangaId}/category/${categoryId}`); id: number,
patch: UpdateMangaPatchInput,
options?: MutationHookOptions<UpdateMangaMutation, UpdateMangaMutationVariables>,
): AbortableApolloMutationResponse<UpdateMangaMutation> {
return this.doRequestNew<UpdateMangaMutation, UpdateMangaMutationVariables>(
GQLMethod.MUTATION,
UPDATE_MANGA,
{ input: { id, patch } },
{
refetchQueries: [
GET_MANGA,
GET_MANGAS,
GET_CATEGORY_MANGAS,
GET_CATEGORY,
GET_CATEGORIES,
GET_SOURCE_MANGAS_FETCH,
],
...options,
},
);
} }
public addMangaToLibrary(mangaId: number | string): AbortableAxiosResponse { public setMangaMeta(
return this.doRequest(HttpMethod.GET, `manga/${mangaId}/library`); mangaId: number,
} key: string,
value: any,
public removeMangaFromLibrary(mangaId: number | string): AbortableAxiosResponse { ): AbortableApolloMutationResponse<SetMangaMetadataMutation> {
return this.doRequest(HttpMethod.DELETE, `manga/${mangaId}/library`); return this.doRequestNew(
} GQLMethod.MUTATION,
SET_MANGA_METADATA,
public setMangaMeta(mangaId: number, key: string, value: any): AbortableAxiosResponse { {
return this.doRequest(HttpMethod.PATCH, `manga/${mangaId}/meta`, { formData: { key, value } }); input: { meta: { mangaId, key, value: `${value}` } },
},
{ refetchQueries: [GET_MANGA, GET_MANGAS, GET_CATEGORY_MANGAS, GET_SOURCE_MANGAS_FETCH] },
);
} }
public useGetMangaChapters( public useGetMangaChapters(
@@ -696,26 +733,6 @@ export class RequestManager {
return this.doRequest(HttpMethod.GET, `manga/${mangaId}/chapters${onlineFetch}`); return this.doRequest(HttpMethod.GET, `manga/${mangaId}/chapters${onlineFetch}`);
} }
public updateMangaChapters(
mangaId: number | string,
{
chapterIds,
chapterIndexes,
change,
}: (
| { chapterIds?: number[]; chapterIndexes: number[] }
| { chapterIds: number[]; chapterIndexes?: number[] }
) & { change: BatchChaptersChange },
): AbortableAxiosResponse {
return this.doRequest(HttpMethod.POST, `manga/${mangaId}/chapter/batch`, {
data: {
chapterIds,
chapterIndexes,
change,
},
});
}
public useGetChapter( public useGetChapter(
mangaId: number | string, mangaId: number | string,
chapterIndex: number | string, chapterIndex: number | string,