fix(web): Displaying of markAsPlayed and likeButton on state change. Improve Card performace
This commit is contained in:
5 files changed
+78
-94
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import PropTypes from "prop-types";
|
||||
|
||||
import React from "react";
|
||||
import React, { useEffect } from "react";
|
||||
|
||||
import IconButton from "@mui/material/IconButton";
|
||||
|
||||
@@ -18,19 +18,28 @@ const LikeButton = ({ itemId, isFavorite, queryKey, userId, itemName }) => {
|
||||
const { enqueueSnackbar } = useSnackbar();
|
||||
|
||||
const handleLiking = async () => {
|
||||
let result = null;
|
||||
if (isFavorite) {
|
||||
await getUserLibraryApi(api).unmarkFavoriteItem({
|
||||
result = await getUserLibraryApi(api).unmarkFavoriteItem({
|
||||
userId: userId,
|
||||
itemId: itemId,
|
||||
});
|
||||
} else if (!isFavorite) {
|
||||
await getUserLibraryApi(api).markFavoriteItem({
|
||||
result = await getUserLibraryApi(api).markFavoriteItem({
|
||||
userId: userId,
|
||||
itemId: itemId,
|
||||
});
|
||||
}
|
||||
return result.data;
|
||||
};
|
||||
const mutation = useMutation({
|
||||
// mutationFn: async () => {
|
||||
// new Promise((resolve, reject) => {
|
||||
// setTimeout(() => {
|
||||
// resolve("foo");
|
||||
// }, 4000);
|
||||
// });
|
||||
// },
|
||||
mutationFn: handleLiking,
|
||||
onError: (error) => {
|
||||
enqueueSnackbar(`${error}`, {
|
||||
@@ -43,66 +52,42 @@ const LikeButton = ({ itemId, isFavorite, queryKey, userId, itemName }) => {
|
||||
},
|
||||
onSettled: async () => {
|
||||
return await queryClient.invalidateQueries({
|
||||
queryKey: queryKey,
|
||||
queryKey,
|
||||
});
|
||||
},
|
||||
mutationKey: ["likeButton", itemId],
|
||||
});
|
||||
|
||||
return (
|
||||
<div
|
||||
<IconButton
|
||||
onClick={(e) => {
|
||||
if (!mutation.isPending) {
|
||||
mutation.mutate();
|
||||
e.stopPropagation();
|
||||
}
|
||||
}}
|
||||
style={{
|
||||
opacity: mutation.isPending ? 0.5 : 1,
|
||||
transition: "opacity 250ms",
|
||||
position: "relative",
|
||||
display: "inline-flex",
|
||||
}}
|
||||
>
|
||||
<CircularProgress
|
||||
style={{
|
||||
opacity: mutation.isPending ? 1 : 0,
|
||||
transition: "opacity 200ms",
|
||||
}}
|
||||
thickness={2}
|
||||
/>
|
||||
<IconButton
|
||||
onClick={(e) => {
|
||||
if (!mutation.isPending) {
|
||||
e.stopPropagation();
|
||||
mutation.mutate();
|
||||
}
|
||||
}}
|
||||
style={{
|
||||
position: "absolute",
|
||||
top: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
opacity: mutation.isPending ? 0.5 : 1,
|
||||
}}
|
||||
<div
|
||||
className="material-symbols-rounded"
|
||||
style={
|
||||
isFavorite
|
||||
? {
|
||||
"--fill": mutation.isPending ? 0 : 1,
|
||||
color: mutation.isPending ? "white" : pink.A700,
|
||||
}
|
||||
: {
|
||||
"--fill": mutation.isPending ? 1 : 0,
|
||||
color: mutation.isPending ? pink.A700 : "white",
|
||||
}
|
||||
}
|
||||
>
|
||||
<div
|
||||
className="material-symbols-rounded"
|
||||
style={
|
||||
isFavorite
|
||||
? {
|
||||
fontVariationSettings:
|
||||
'"FILL" 1, "wght" 300, "GRAD" 25, "opsz" 40',
|
||||
color: pink[700],
|
||||
}
|
||||
: {
|
||||
fontVariationSettings:
|
||||
'"FILL" 0, "wght" 300, "GRAD" 25, "opsz" 40',
|
||||
color: "white",
|
||||
}
|
||||
}
|
||||
>
|
||||
favorite
|
||||
</div>
|
||||
</IconButton>
|
||||
</div>
|
||||
favorite
|
||||
</div>
|
||||
</IconButton>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -20,12 +20,12 @@ const MarkPlayedButton = ({ itemId, isPlayed, queryKey, userId, itemName }) => {
|
||||
|
||||
const handleMarking = async () => {
|
||||
if (!isPlayed) {
|
||||
await getPlaystateApi(api).markPlayedItem({
|
||||
return await getPlaystateApi(api).markPlayedItem({
|
||||
userId: userId,
|
||||
itemId: itemId,
|
||||
});
|
||||
} else if (isPlayed) {
|
||||
await getPlaystateApi(api).markUnplayedItem({
|
||||
return await getPlaystateApi(api).markUnplayedItem({
|
||||
userId: userId,
|
||||
itemId: itemId,
|
||||
});
|
||||
@@ -50,49 +50,33 @@ const MarkPlayedButton = ({ itemId, isPlayed, queryKey, userId, itemName }) => {
|
||||
mutationKey: ["markPlayedButton", itemId],
|
||||
});
|
||||
return (
|
||||
<div
|
||||
<IconButton
|
||||
onClick={(e) => {
|
||||
if (!mutation.isPending) {
|
||||
mutation.mutate();
|
||||
e.stopPropagation();
|
||||
}
|
||||
}}
|
||||
style={{
|
||||
opacity: mutation.isPending ? 0.5 : 1,
|
||||
transition: "opacity 250ms",
|
||||
position: "relative",
|
||||
display: "inline-flex",
|
||||
}}
|
||||
>
|
||||
<CircularProgress
|
||||
<div
|
||||
className="material-symbols-rounded"
|
||||
style={{
|
||||
opacity: mutation.isPending ? 1 : 0,
|
||||
transition: "opacity 200ms",
|
||||
}}
|
||||
thickness={2}
|
||||
/>
|
||||
<IconButton
|
||||
onClick={(e) => {
|
||||
if (!mutation.isPending) {
|
||||
e.stopPropagation();
|
||||
mutation.mutate();
|
||||
}
|
||||
}}
|
||||
style={{
|
||||
position: "absolute",
|
||||
top: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
opacity: mutation.isPending ? 0.5 : 1,
|
||||
color: isPlayed
|
||||
? mutation.isPending
|
||||
? "white"
|
||||
: green.A700
|
||||
: mutation.isPending
|
||||
? green.A700
|
||||
: "white",
|
||||
}}
|
||||
>
|
||||
<div
|
||||
className="material-symbols-rounded"
|
||||
style={{
|
||||
color: isPlayed ? green[400] : "white",
|
||||
}}
|
||||
>
|
||||
done
|
||||
</div>
|
||||
</IconButton>
|
||||
</div>
|
||||
done
|
||||
</div>
|
||||
</IconButton>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user