refactor: move all buttons to tsx #338
This commit is contained in:
13 files changed
+170
-170
No files matched your search
@@ -0,0 +1,101 @@
|
||||
import React from "react";
|
||||
|
||||
import IconButton from "@mui/material/IconButton";
|
||||
|
||||
import { useApiInContext } from "@/utils/store/api";
|
||||
import type { UserItemDataDto } from "@jellyfin/sdk/lib/generated-client";
|
||||
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
|
||||
import { pink } from "@mui/material/colors";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useSnackbar } from "notistack";
|
||||
|
||||
export default function LikeButton({
|
||||
itemId,
|
||||
isFavorite,
|
||||
queryKey,
|
||||
userId,
|
||||
itemName,
|
||||
}: {
|
||||
itemId: string;
|
||||
isFavorite: boolean;
|
||||
queryKey: string[];
|
||||
userId: string;
|
||||
itemName: string;
|
||||
}) {
|
||||
const api = useApiInContext((s) => s.api);
|
||||
if (!api) {
|
||||
console.error("Unable to display like button, api is not available");
|
||||
return null;
|
||||
}
|
||||
const queryClient = useQueryClient();
|
||||
const { enqueueSnackbar } = useSnackbar();
|
||||
|
||||
const handleLiking = async () => {
|
||||
let result: UserItemDataDto = null!;
|
||||
if (isFavorite) {
|
||||
result = (
|
||||
await getUserLibraryApi(api).unmarkFavoriteItem({
|
||||
userId: userId,
|
||||
itemId: itemId,
|
||||
})
|
||||
).data;
|
||||
} else if (!isFavorite) {
|
||||
result = (
|
||||
await getUserLibraryApi(api).markFavoriteItem({
|
||||
userId: userId,
|
||||
itemId: itemId,
|
||||
})
|
||||
).data;
|
||||
}
|
||||
return result; // We need to return the result so that the onSettled function can invalidate the query
|
||||
};
|
||||
const mutation = useMutation({
|
||||
mutationFn: handleLiking,
|
||||
onError: (error) => {
|
||||
enqueueSnackbar(`An error occured while updating "${itemName}"`, {
|
||||
variant: "error",
|
||||
});
|
||||
console.error(error);
|
||||
},
|
||||
onSettled: async () => {
|
||||
return await queryClient.invalidateQueries({
|
||||
queryKey,
|
||||
});
|
||||
},
|
||||
mutationKey: ["likeButton", itemId],
|
||||
});
|
||||
|
||||
return (
|
||||
<IconButton
|
||||
onClick={(e) => {
|
||||
if (!mutation.isPending) {
|
||||
mutation.mutate();
|
||||
e.stopPropagation();
|
||||
}
|
||||
}}
|
||||
style={{
|
||||
opacity: mutation.isPending ? 0.5 : 1,
|
||||
transition: "opacity 250ms",
|
||||
}}
|
||||
>
|
||||
<div
|
||||
className="material-symbols-rounded"
|
||||
style={
|
||||
isFavorite
|
||||
? {
|
||||
//@ts-ignore
|
||||
"--fill": mutation.isPending ? 0 : 1,
|
||||
color: mutation.isPending ? "white" : pink.A700,
|
||||
}
|
||||
: {
|
||||
//@ts-ignore
|
||||
"--fill": mutation.isPending ? 1 : 0,
|
||||
color: mutation.isPending ? pink.A700 : "white",
|
||||
}
|
||||
}
|
||||
>
|
||||
favorite
|
||||
</div>
|
||||
</IconButton>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user