Add display loading state when updating item

This commit is contained in:
Prayag Prajapati committed 2023-08-27 23:44:43 +05:30
1 parent 38a7369650
commit 7df0f3f9d3
8 files changed
+231 -39

No files matched your search

+49 -15
View File
@@ -9,11 +9,16 @@ import { pink } from "@mui/material/colors";
import { MdiHeart } from "../icons/mdiHeart";
import { MdiHeartOutline } from "../icons/mdiHeartOutline";
import { useSnackbar } from "notistack";
import { useAppLoadingStore } from "../../utils/store/appLoading";
const LikeButton = ({ itemId, isFavorite, queryKey, userId, itemName }) => {
const queryClient = useQueryClient();
const { enqueueSnackbar } = useSnackbar();
const [setIsLoading, setIsSuccess, setIsError] = useAppLoadingStore(
(state) => [state.setIsLoading, state.setIsSuccess, state.setIsError],
);
const handleLiking = async () => {
let result;
console.log(isFavorite);
@@ -32,25 +37,46 @@ const LikeButton = ({ itemId, isFavorite, queryKey, userId, itemName }) => {
const mutation = useMutation({
mutationFn: handleLiking,
onMutate: () => {
setIsLoading(true);
setIsError(false);
setIsSuccess(false);
queryClient.cancelQueries(queryKey);
const currentLatestMedia = queryClient.getQueryData(queryKey);
queryClient.setQueryData(queryKey, (oldMedia) =>
oldMedia.map((oitem) => {
if (oitem.Id == itemId) {
return {
...oitem,
UserData: {
...oitem.UserData,
IsFavorite: !isFavorite,
},
};
}
return oitem;
}),
);
queryClient.setQueryData(queryKey, (oldMedia) => {
try {
oldMedia.Items.map((oitem) => {
if (oitem.Id == itemId) {
return {
...oitem,
UserData: {
...oitem.UserData,
IsFavorite: !isFavorite,
},
};
}
return oitem;
});
} catch (error) {
oldMedia.map((oitem) => {
if (oitem.Id == itemId) {
return {
...oitem,
UserData: {
...oitem.UserData,
IsFavorite: !isFavorite,
},
};
}
return oitem;
});
}
});
return { currentLatestMedia };
},
onError: (error, a, context) => {
setIsLoading(false);
setIsError(true);
enqueueSnackbar(
`An error occured while updating "${itemName}"`,
{
@@ -61,12 +87,20 @@ const LikeButton = ({ itemId, isFavorite, queryKey, userId, itemName }) => {
console.error(error);
},
onSuccess: () => {
setIsLoading(false);
setIsSuccess(true);
console.log("Successfully updated", itemId);
queryClient.invalidateQueries(queryKey);
},
});
return (
<IconButton onClick={mutation.mutate} disabled={mutation.isLoading}>
<IconButton
onClick={(e) => {
e.stopPropagation();
mutation.mutate();
}}
disabled={mutation.isLoading}
>
{isFavorite ? (
<MdiHeart sx={{ color: pink[700] }} />
) : (
+48 -15
View File
@@ -10,11 +10,16 @@ import { green } from "@mui/material/colors";
import { getPlaystateApi } from "@jellyfin/sdk/lib/utils/api/playstate-api";
import { MdiCheck } from "../icons/mdiCheck";
import { useSnackbar } from "notistack";
import { useAppLoadingStore } from "../../utils/store/appLoading";
const MarkPlayedButton = ({ itemId, isPlayed, queryKey, userId, itemName }) => {
const queryClient = useQueryClient();
const { enqueueSnackbar } = useSnackbar();
const [setIsLoading, setIsSuccess, setIsError] = useAppLoadingStore(
(state) => [state.setIsLoading, state.setIsSuccess, state.setIsError],
);
const handleMarking = async () => {
if (!isPlayed) {
await getPlaystateApi(window.api).markPlayedItem({
@@ -31,25 +36,45 @@ const MarkPlayedButton = ({ itemId, isPlayed, queryKey, userId, itemName }) => {
const mutation = useMutation({
mutationFn: handleMarking,
onMutate: () => {
setIsLoading(true);
setIsError(false);
setIsSuccess(false);
queryClient.cancelQueries(queryKey);
const previousData = queryClient.getQueryData(queryKey);
queryClient.setQueryData(queryKey, (oldMedia) =>
oldMedia.map((oitem) => {
if (oitem.Id == itemId) {
return {
...oitem,
UserData: {
...oitem.UserData,
Played: !isPlayed,
},
};
}
return oitem;
}),
);
queryClient.setQueryData(queryKey, (oldMedia) => {
try {
oldMedia.Items.map((oitem) => {
if (oitem.Id == itemId) {
return {
...oitem,
UserData: {
...oitem.UserData,
Played: !isPlayed,
},
};
}
return oitem;
});
} catch (error) {
oldMedia.map((oitem) => {
if (oitem.Id == itemId) {
return {
...oitem,
UserData: {
...oitem.UserData,
Played: !isPlayed,
},
};
}
return oitem;
});
}
});
return { previousData };
},
onError: (error, a, context) => {
setIsLoading(false);
setIsError(true);
enqueueSnackbar(
`An error occured while updating "${itemName}"`,
{
@@ -60,12 +85,20 @@ const MarkPlayedButton = ({ itemId, isPlayed, queryKey, userId, itemName }) => {
console.error(error);
},
onSuccess: () => {
setIsLoading(false);
setIsSuccess(true);
console.log("Successfully updated", itemId);
queryClient.invalidateQueries(queryKey);
},
});
return (
<IconButton onClick={mutation.mutate} disabled={mutation.isLoading}>
<IconButton
onClick={(e) => {
e.stopPropagation();
mutation.mutate();
}}
disabled={mutation.isLoading}
>
<MdiCheck
sx={{
color: isPlayed ? green[400] : "white",