From 2c972fdd53642d2401fc50c170de67a475edfc8c Mon Sep 17 00:00:00 2001 From: Patel Divyesh Date: Wed, 6 Dec 2023 17:48:28 +0530 Subject: [PATCH] fix: Update post removes photos or videos (#1111) * Fixed update post photo and video bug * Fixed removing of posts by user * Handled all cases of updating of posts in photos and videos --- src/components/OrgPostCard/OrgPostCard.tsx | 29 +++++++++++----------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/components/OrgPostCard/OrgPostCard.tsx b/src/components/OrgPostCard/OrgPostCard.tsx index 5845ef90ba..cfa221714d 100644 --- a/src/components/OrgPostCard/OrgPostCard.tsx +++ b/src/components/OrgPostCard/OrgPostCard.tsx @@ -42,7 +42,8 @@ export default function OrgPostCard( postvideo: '', pinned: false, }); - + const [postPhotoUpdated, setPostPhotoUpdated] = useState(false); + const [postVideoUpdated, setPostVideoUpdated] = useState(false); const [togglePost, setPostToggle] = useState('Read more'); const [showEditModal, setShowEditModal] = useState(false); const [showDeleteModal, setShowDeleteModal] = useState(false); @@ -80,6 +81,8 @@ export default function OrgPostCard( postvideo: props.postVideo, pinned: props.pinned, }); + setPostPhotoUpdated(false); + setPostVideoUpdated(false); setShowEditModal((prev) => !prev); }; const toggleShowDeleteModal = (): void => setShowDeleteModal((prev) => !prev); @@ -105,6 +108,7 @@ export default function OrgPostCard( ...postformState, postphoto: '', }); + setPostPhotoUpdated(true); const fileInput = document.getElementById( 'postImageUrl' ) as HTMLInputElement; @@ -118,6 +122,7 @@ export default function OrgPostCard( ...postformState, postvideo: '', }); + setPostVideoUpdated(true); const fileInput = document.getElementById( 'postVideoUrl' ) as HTMLInputElement; @@ -201,24 +206,17 @@ export default function OrgPostCard( e.preventDefault(); try { - let imageUrl = null; - let videoUrl = null; - - if (e.target?.postphoto && e.target?.postphoto.files.length > 0) { - imageUrl = postformState.postphoto; - } - - if (e.target?.postvideo && e.target?.postvideo.files.length > 0) { - videoUrl = postformState.postvideo; - } - const { data } = await updatePostMutation({ variables: { id: props.id, title: postformState.posttitle, text: postformState.postinfo, - imageUrl, - videoUrl, + ...(postPhotoUpdated && { + imageUrl: postformState.postphoto, + }), + ...(postVideoUpdated && { + videoUrl: postformState.postvideo, + }), }, }); @@ -531,7 +529,7 @@ export default function OrgPostCard( ...prevPostFormState, postphoto: '', })); - + setPostPhotoUpdated(true); const file = e.target.files?.[0]; if (file) { setPostFormState({ @@ -580,6 +578,7 @@ export default function OrgPostCard( ...prevPostFormState, postvideo: '', })); + setPostVideoUpdated(true); const target = e.target as HTMLInputElement; const file = target.files && target.files[0]; if (file) {