refactor: update MangaCard to use correct DTO types and fix query cache update logic

This commit is contained in:
Rodrigo Verdiani 2026-04-05 20:33:02 -03:00
parent 451b45f2d3
commit 7dab8ec21c

View File

@ -3,8 +3,8 @@ import { Calendar, Database, Heart, Star } from "lucide-react";
import { useCallback } from "react";
import { Link } from "react-router";
import type {
DefaultResponseDTOPageMangaListDTO,
MangaListDTO,
PageMangaListDTO,
} from "@/api/generated/api.schemas.ts";
import {
useSetFavorite,
@ -18,7 +18,7 @@ import { formatToTwoDigitsDateRange } from "@/utils/dateFormatter.ts";
interface MangaCardProps {
manga: MangaListDTO;
queryKey: unknown;
queryKey: readonly unknown[];
}
export const MangaCard = ({ manga, queryKey }: MangaCardProps) => {
@ -26,14 +26,20 @@ export const MangaCard = ({ manga, queryKey }: MangaCardProps) => {
const queryClient = useQueryClient();
const updateQueryData = useCallback(
(oldData: PageMangaListDTO | undefined, isFavorite: boolean) => ({
(
oldData: DefaultResponseDTOPageMangaListDTO | undefined,
isFavorite: boolean,
) => ({
...oldData,
content:
oldData?.content?.map((manga) =>
manga.id === manga.id ? { ...manga, favorite: isFavorite } : manga,
) || [],
data: {
...oldData?.data,
content:
oldData?.data?.content?.map((item) =>
item.id === manga.id ? { ...item, favorite: isFavorite } : item,
) || [],
},
}),
[],
[manga.id],
);
const { mutate: mutateFavorite, isPending: isPendingFavorite } =
@ -41,8 +47,8 @@ export const MangaCard = ({ manga, queryKey }: MangaCardProps) => {
mutation: {
onSuccess: () =>
queryClient.setQueryData(
[queryKey],
(oldData: PageMangaListDTO | undefined) =>
queryKey,
(oldData: DefaultResponseDTOPageMangaListDTO | undefined) =>
updateQueryData(oldData, true),
),
},
@ -53,8 +59,8 @@ export const MangaCard = ({ manga, queryKey }: MangaCardProps) => {
mutation: {
onSuccess: () =>
queryClient.setQueryData(
[queryKey],
(oldData: PageMangaListDTO | undefined) =>
queryKey,
(oldData: DefaultResponseDTOPageMangaListDTO | undefined) =>
updateQueryData(oldData, false),
),
},