Add display loading state when updating item
This commit is contained in:
8 files changed
+231
-39
No files matched your search
@@ -1,6 +1,8 @@
|
||||
/** @format */
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import { AnimatePresence, motion } from "framer-motion";
|
||||
|
||||
import { relaunch } from "@tauri-apps/api/process";
|
||||
|
||||
import MuiAppBar from "@mui/material/AppBar";
|
||||
@@ -12,6 +14,7 @@ import Box from "@mui/material/Box";
|
||||
import ListItemIcon from "@mui/material/ListItemIcon";
|
||||
import Menu from "@mui/material/Menu";
|
||||
import MenuItem from "@mui/material/MenuItem";
|
||||
import CircularProgress from "@mui/material/CircularProgress";
|
||||
import useScrollTrigger from "@mui/material/useScrollTrigger";
|
||||
|
||||
import { MdiMagnify } from "../icons/mdiMagnify";
|
||||
@@ -33,6 +36,10 @@ import { useDrawerStore } from "../../utils/store/drawer";
|
||||
import { delServer } from "../../utils/storage/servers";
|
||||
import { delUser } from "../../utils/storage/user";
|
||||
import { MdiDelete } from "../icons/mdiDelete";
|
||||
import { MdiCogSync } from "../icons/mdiCogSync";
|
||||
import { useAppLoadingStore } from "../../utils/store/appLoading";
|
||||
import { MdiCheck } from "../icons/mdiCheck";
|
||||
import { MdiAlertDecagram } from "../icons/mdiAlertDecagram";
|
||||
|
||||
export const AppBar = () => {
|
||||
const navigate = useNavigate();
|
||||
@@ -72,6 +79,11 @@ export const AppBar = () => {
|
||||
setDrawerOpen(true);
|
||||
};
|
||||
|
||||
const [syncLoading, syncSuccess, syncError] = useAppLoadingStore(
|
||||
(state) => [state.isLoading, state.isSuccess, state.isError],
|
||||
);
|
||||
console.log([syncLoading, syncSuccess, syncError]);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
location.pathname.includes("login") ||
|
||||
@@ -150,6 +162,52 @@ export const AppBar = () => {
|
||||
</IconButton>
|
||||
</Box>
|
||||
<Box sx={{ display: "flex", gap: 2 }}>
|
||||
<AnimatePresence>
|
||||
{syncLoading && (
|
||||
<Box
|
||||
component={motion.div}
|
||||
position="relative"
|
||||
display="inline-flex"
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
exit={{
|
||||
opacity: 0,
|
||||
transition: {
|
||||
delay: 10,
|
||||
duration: 5,
|
||||
},
|
||||
}}
|
||||
>
|
||||
<CircularProgress
|
||||
sx={{
|
||||
opacity: syncLoading
|
||||
? 1
|
||||
: 0,
|
||||
}}
|
||||
/>
|
||||
<IconButton
|
||||
sx={{
|
||||
position: "absolute",
|
||||
top: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
}}
|
||||
>
|
||||
{syncSuccess ? (
|
||||
<MdiCheck />
|
||||
) : syncError ? (
|
||||
<MdiAlertDecagram />
|
||||
) : (
|
||||
<MdiCogSync />
|
||||
)}
|
||||
</IconButton>
|
||||
</Box>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
<IconButton>
|
||||
<MdiHeartOutline />
|
||||
</IconButton>
|
||||
|
||||
@@ -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] }} />
|
||||
) : (
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
/** @format */
|
||||
|
||||
import SvgIcon from "@mui/material/SvgIcon";
|
||||
|
||||
import { mdiCogSync } from "@mdi/js";
|
||||
|
||||
export const MdiCogSync = (props) => {
|
||||
return (
|
||||
<SvgIcon {...props}>
|
||||
<path d={mdiCogSync}></path>
|
||||
</SvgIcon>
|
||||
);
|
||||
};
|
||||
@@ -292,8 +292,7 @@ const Home = () => {
|
||||
}
|
||||
queryKey={[
|
||||
"home",
|
||||
"resume",
|
||||
"video",
|
||||
"upNext",
|
||||
]}
|
||||
isPlayed={
|
||||
item.UserData.Played
|
||||
@@ -393,7 +392,7 @@ const Home = () => {
|
||||
queryKey={[
|
||||
"home",
|
||||
"resume",
|
||||
"video",
|
||||
"audio",
|
||||
]}
|
||||
isPlayed={
|
||||
item.UserData.Played
|
||||
|
||||
@@ -968,6 +968,50 @@ const LibraryView = () => {
|
||||
? "square"
|
||||
: "portrait"
|
||||
}
|
||||
isFavorite={
|
||||
item.UserData.IsFavorite
|
||||
}
|
||||
queryKey={[
|
||||
"libraryView",
|
||||
"currentLibItems",
|
||||
id,
|
||||
`page: ${page}`,
|
||||
[
|
||||
currentViewType,
|
||||
|
||||
sortAscending
|
||||
? SortOrder.Ascending
|
||||
: SortOrder.Descending,
|
||||
,
|
||||
sortBy,
|
||||
[
|
||||
isPlayed,
|
||||
isUnPlayed,
|
||||
isResumable,
|
||||
isFavorite,
|
||||
isLiked,
|
||||
isUnliked,
|
||||
],
|
||||
[
|
||||
hasSubtitles,
|
||||
hasTrailer,
|
||||
hasSpecialFeature,
|
||||
hasThemeSong,
|
||||
hasThemeVideo,
|
||||
],
|
||||
[
|
||||
isBluRay,
|
||||
isDVD,
|
||||
isHD,
|
||||
is4K,
|
||||
is3D,
|
||||
],
|
||||
],
|
||||
]}
|
||||
isPlayed={
|
||||
item.UserData.Played
|
||||
}
|
||||
userId={user.data.Id}
|
||||
></Card>
|
||||
</Grid2>
|
||||
);
|
||||
|
||||
@@ -284,13 +284,10 @@ export const UserLogin = () => {
|
||||
key={index}
|
||||
itemName={item.Name}
|
||||
itemId={item.Id}
|
||||
imageTags={
|
||||
!!item.PrimaryImageTag
|
||||
}
|
||||
disableOverlay
|
||||
iconType="User"
|
||||
cardOrientation="sqaure"
|
||||
onClickEvent={() =>
|
||||
itemType="User"
|
||||
cardType="square"
|
||||
onClick={() =>
|
||||
navigate(
|
||||
`/login/withImg/${item.Name}/${item.Id}/`,
|
||||
)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
/** @format */
|
||||
|
||||
import { create } from "zustand";
|
||||
|
||||
export const useAppLoadingStore = create((set) => ({
|
||||
isLoading: false,
|
||||
isSuccess: false,
|
||||
isError: false,
|
||||
setIsLoading: (loading) =>
|
||||
set((state) => ({ ...state, isLoading: loading })),
|
||||
setIsSuccess: (success) =>
|
||||
set((state) => ({ ...state, isSuccess: success })),
|
||||
setIsError: (error) => set((state) => ({ ...state, isError: error })),
|
||||
}));
|
||||
Reference in new issue
Block a user