diff --git a/src/components/manga/MangaDetails.tsx b/src/components/manga/MangaDetails.tsx index 4fa4a125..22aa6daa 100644 --- a/src/components/manga/MangaDetails.tsx +++ b/src/components/manga/MangaDetails.tsx @@ -152,12 +152,16 @@ const MangaDetails: React.FC = ({ manga }) => { const addToLibrary = () => { 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 = () => { 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 ( diff --git a/src/components/navbar/action/CategorySelect.tsx b/src/components/navbar/action/CategorySelect.tsx index 218e58a6..467e688e 100644 --- a/src/components/navbar/action/CategorySelect.tsx +++ b/src/components/navbar/action/CategorySelect.tsx @@ -31,6 +31,7 @@ export default function CategorySelect(props: IProps) { const { data: mangaCategoriesData, mutate } = requestManager.useGetMangaCategories(mangaId); const { data: categoriesData } = requestManager.useGetCategories(); + const [triggerMutate] = requestManager.useUpdateMangaCategories(); const allCategories = useMemo(() => { const cats = [...(categoriesData ?? [])]; // make copy @@ -53,10 +54,18 @@ export default function CategorySelect(props: IProps) { const handleChange = (event: React.ChangeEvent, categoryId: number) => { const { checked } = event.target as HTMLInputElement; - (checked - ? requestManager.addMangaToCategory(mangaId, categoryId) - : requestManager.removeMangaFromCategory(mangaId, categoryId) - ).response.then(() => mutate()); + // TODO - update to only update categories when clicking OK - can now be updated in one go with graphql + triggerMutate({ + variables: { + input: { + id: mangaId, + patch: { + addToCategories: checked ? [categoryId] : [], + removeFromCategories: !checked ? [categoryId] : [], + }, + }, + }, + }).then(() => mutate()); }; return ( diff --git a/src/lib/requests/RequestManager.ts b/src/lib/requests/RequestManager.ts index 203d9f61..d27d3330 100644 --- a/src/lib/requests/RequestManager.ts +++ b/src/lib/requests/RequestManager.ts @@ -58,9 +58,15 @@ import { InstallExternalExtensionMutationVariables, SetGlobalMetadataMutation, SetGlobalMetadataMutationVariables, + SetMangaMetadataMutation, UpdateExtensionMutation, UpdateExtensionMutationVariables, UpdateExtensionPatchInput, + UpdateMangaCategoriesMutation, + UpdateMangaCategoriesMutationVariables, + UpdateMangaMutation, + UpdateMangaMutationVariables, + UpdateMangaPatchInput, } from '@/lib/graphql/generated/graphql.ts'; import { GET_GLOBAL_METADATA, GET_GLOBAL_METADATAS } from '@/lib/graphql/queries/GlobalMetadataQuery.ts'; import { SET_GLOBAL_METADATA } from '@/lib/graphql/mutations/GlobalMetadataMutation.ts'; @@ -72,6 +78,10 @@ import { UPDATE_EXTENSION, } from '@/lib/graphql/mutations/ExtensionMutation.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 { SWR_GET, @@ -661,24 +671,51 @@ export class RequestManager { return this.doRequest(HttpMethod.SWR_GET, `manga/${mangaId}/category`, { swrOptions }); } - public addMangaToCategory(mangaId: number, categoryId: number): AbortableAxiosResponse { - return this.doRequest(HttpMethod.GET, `manga/${mangaId}/category/${categoryId}`); + public useUpdateMangaCategories( + options?: MutationHookOptions, + ): AbortableApolloUseMutationResponse { + 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 { - return this.doRequest(HttpMethod.DELETE, `manga/${mangaId}/category/${categoryId}`); + public updateManga( + id: number, + patch: UpdateMangaPatchInput, + options?: MutationHookOptions, + ): AbortableApolloMutationResponse { + return this.doRequestNew( + 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 { - return this.doRequest(HttpMethod.GET, `manga/${mangaId}/library`); - } - - public removeMangaFromLibrary(mangaId: number | string): AbortableAxiosResponse { - return this.doRequest(HttpMethod.DELETE, `manga/${mangaId}/library`); - } - - public setMangaMeta(mangaId: number, key: string, value: any): AbortableAxiosResponse { - return this.doRequest(HttpMethod.PATCH, `manga/${mangaId}/meta`, { formData: { key, value } }); + public setMangaMeta( + mangaId: number, + key: string, + value: any, + ): AbortableApolloMutationResponse { + return this.doRequestNew( + GQLMethod.MUTATION, + SET_MANGA_METADATA, + { + input: { meta: { mangaId, key, value: `${value}` } }, + }, + { refetchQueries: [GET_MANGA, GET_MANGAS, GET_CATEGORY_MANGAS, GET_SOURCE_MANGAS_FETCH] }, + ); } public useGetMangaChapters( @@ -696,26 +733,6 @@ export class RequestManager { 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( mangaId: number | string, chapterIndex: number | string,