diff --git a/src/components/item/publish/ItemPublishButton.tsx b/src/components/item/publish/ItemPublishButton.tsx index 5b3238396..caec99460 100644 --- a/src/components/item/publish/ItemPublishButton.tsx +++ b/src/components/item/publish/ItemPublishButton.tsx @@ -1,10 +1,10 @@ -import { useEffect, useState } from 'react'; +import { useState } from 'react'; import { CheckCircle, InfoRounded } from '@mui/icons-material'; +import { LoadingButton } from '@mui/lab'; import { Checkbox, FormControlLabel, Stack, Typography } from '@mui/material'; import { DiscriminatedItem } from '@graasp/sdk'; -import { Button, Loader } from '@graasp/ui'; import { useBuilderTranslation } from '../../../config/i18n'; import { hooks, mutations } from '../../../config/queryClient'; @@ -32,30 +32,18 @@ const ItemPublishButton = ({ const { t: translateBuilder } = useBuilderTranslation(); // item tags - const { mutate: unpublish } = useUnpublishItem(); - const { mutate: publishItem } = usePublishItem(); + const { mutate: unpublish, isLoading: isUnPublishing } = useUnpublishItem(); + const { mutate: publishItem, isLoading: isPublishing } = usePublishItem(); - const { data: itemPublishedEntry, isLoading: isItemTagsLoading } = - useItemPublishedInformation({ itemId: item.id }); + const { data: itemPublishedEntry, isFetching } = useItemPublishedInformation({ + itemId: item.id, + }); - const [isPublished, setIsPublished] = useState(false); - const [isDisabled, setIsDisabled] = useState(false); const [emailNotification, setEmailNotification] = useState(false); - // update state variables depending on fetch values - useEffect(() => { - if (itemPublishedEntry) { - setIsPublished(Boolean(itemPublishedEntry)); - - // disable setting if any publication is set on any ancestor items - setIsDisabled(itemPublishedEntry?.item?.path !== item?.path); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [itemPublishedEntry, item]); - - if (isItemTagsLoading) { - return ; - } + const isPublished = Boolean(itemPublishedEntry); + const isDisabled = + itemPublishedEntry && itemPublishedEntry?.item?.path !== item?.path; if (!isValidated) { return null; @@ -91,25 +79,39 @@ const ItemPublishButton = ({ )} - - + + + } + id={ITEM_PUBLISH_BUTTON_ID} + // show a loading state when we are fetching the entry and when we are publishing + loading={isFetching || isPublishing} + > + {isPublished + ? translateBuilder(BUILDER.LIBRARY_SETTINGS_PUBLISH_BUTTON_DISABLED) + : translateBuilder(BUILDER.LIBRARY_SETTINGS_PUBLISH_BUTTON)} + + + {translateBuilder(BUILDER.LIBRARY_SETTINGS_UNPUBLISH_BUTTON)} + +