From 734ed78f3a8105d78a8bd5de57847abd0f3dc42e Mon Sep 17 00:00:00 2001 From: Thibault Reidy Date: Fri, 17 May 2024 16:41:57 +0200 Subject: [PATCH] fix: solve PublicationThumbnail issue to refresh image --- src/components/common/ContentLoader.tsx | 8 +- src/components/common/ThumbnailUploader.tsx | 1 + .../item/publish/PublicationThumbnail.tsx | 107 ++++++++++++------ 3 files changed, 78 insertions(+), 38 deletions(-) diff --git a/src/components/common/ContentLoader.tsx b/src/components/common/ContentLoader.tsx index 7440775b0..60e3c91c9 100644 --- a/src/components/common/ContentLoader.tsx +++ b/src/components/common/ContentLoader.tsx @@ -1,17 +1,23 @@ import { Skeleton } from '@mui/material'; +const DEFAULT_WIDTH = '100%'; + type Props = { children: JSX.Element | JSX.Element[]; isLoading: boolean; + width?: string | number; + height?: string | number; }; export const ContentLoader = ({ children, isLoading, + width = DEFAULT_WIDTH, + height, }: Props): JSX.Element | JSX.Element[] => { if (isLoading) { return ( - + {children} ); diff --git a/src/components/common/ThumbnailUploader.tsx b/src/components/common/ThumbnailUploader.tsx index 58dca1869..a54693cfb 100644 --- a/src/components/common/ThumbnailUploader.tsx +++ b/src/components/common/ThumbnailUploader.tsx @@ -65,6 +65,7 @@ const ThumbnailUploader = ({ thumbnailSize = THUMBNAIL_DIMENSION, currentThumbnail, }: Props): JSX.Element => { + // TODO: merge with ThumbnailSetting ? const inputRef = useRef(null); const [showCropModal, setShowCropModal] = useState(false); const [newAvatar, setNewAvatar] = useState(currentThumbnail ?? ''); diff --git a/src/components/item/publish/PublicationThumbnail.tsx b/src/components/item/publish/PublicationThumbnail.tsx index c9c8db361..2f8147552 100644 --- a/src/components/item/publish/PublicationThumbnail.tsx +++ b/src/components/item/publish/PublicationThumbnail.tsx @@ -9,6 +9,8 @@ import { theme } from '@graasp/ui'; import Uppy from '@uppy/core'; import { title } from 'process'; +import ContentLoader from '@/components/common/ContentLoader'; +import StatusBar from '@/components/file/StatusBar'; import { WARNING_COLOR } from '@/config/constants'; import { hooks, mutations } from '@/config/queryClient'; import { buildPublishWarningIcon } from '@/config/selectors'; @@ -16,6 +18,8 @@ import { configureThumbnailUppy } from '@/utils/uppy'; import ThumbnailUploader from '../../common/ThumbnailUploader'; +const THUMBNAIL_SIZE = 150; + type ItemThumbnail = { hasThumbnail?: boolean; url?: string; @@ -25,26 +29,45 @@ type Props = { item: DiscriminatedItem; }; export const PublicationThumbnail = ({ item }: Props): JSX.Element => { + // TODO: extract logic in hook and reuse in ThumbnailSetting and ThumbnailUploader ? const { mutate: deleteThumbnail } = mutations.useDeleteItemThumbnail(); const { mutate: onFileUploadComplete } = mutations.useUploadFiles(); - const itemId = item.id; - const { data: thumbnailUrl } = hooks.useItemThumbnailUrl({ - id: itemId, - size: ThumbnailSize.Medium, - }); + const { id: itemId, settings } = item; + const { data: thumbnailUrl, isLoading: isThumbnailLoading } = + hooks.useItemThumbnailUrl({ + id: itemId, + size: ThumbnailSize.Medium, + }); const [uppy, setUppy] = useState(); + const [openStatusBar, setOpenStatusBar] = useState(false); + // TODO: check if this state is needed const [itemThumbnail, setItemThumbnail] = useState({ url: thumbnailUrl, - hasThumbnail: item.settings.hasThumbnail, + hasThumbnail: settings.hasThumbnail, }); + // TODO: check if it is possible to avoid useEffect + // Used to synchronize when thumbnailUrl change + useEffect(() => { + setItemThumbnail({ + url: thumbnailUrl, + hasThumbnail: settings.hasThumbnail, + }); + }, [settings, thumbnailUrl]); + + const handleClose = () => { + setOpenStatusBar(false); + }; + useEffect(() => { setUppy( configureThumbnailUppy({ itemId, - onUpload: () => {}, + onUpload: () => { + setOpenStatusBar(true); + }, onError: (error: Error) => { onFileUploadComplete({ id: itemId, error }); }, @@ -56,6 +79,9 @@ export const PublicationThumbnail = ({ item }: Props): JSX.Element => { onFileUploadComplete({ id: itemId, data }); setItemThumbnail({ hasThumbnail: true }); } + // close progress bar of uppy + handleClose(); + return false; }, }), ); @@ -86,36 +112,43 @@ export const PublicationThumbnail = ({ item }: Props): JSX.Element => { }; return ( - - - - {/* TODO: handle slow 3G */} - {!itemThumbnail.hasThumbnail && ( - - - - )} - - + <> + + + + + {/* TODO: handle slow 3G */} + {!itemThumbnail.hasThumbnail && ( + + + + )} + + + + {uppy && ( + + )} + ); };