-
Notifications
You must be signed in to change notification settings - Fork 430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bust cover image cache after uploading a new image #8489
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import { NonReadOnlyUsers } from "../../Utils/AuthorizeFor"; | |
import { FacilityModel } from "./models"; | ||
import { FACILITY_FEATURE_TYPES, USER_TYPES } from "../../Common/constants"; | ||
import DropdownMenu, { DropdownItem } from "../Common/components/Menu"; | ||
import { lazy, useState } from "react"; | ||
import { lazy, useEffect, useState } from "react"; | ||
|
||
import ButtonV2 from "../Common/components/ButtonV2"; | ||
import CareIcon from "../../CAREUI/icons/CareIcon"; | ||
|
@@ -58,6 +58,8 @@ export const FacilityHome = ({ facilityId }: Props) => { | |
const [openDeleteDialog, setOpenDeleteDialog] = useState(false); | ||
const [editCoverImage, setEditCoverImage] = useState(false); | ||
const [coverImageEdited, setCoverImageEdited] = useState(false); | ||
const [coverImageLoaded, setCoverImageLoaded] = useState(false); | ||
const [coverImageUrl, setCoverImageUrl] = useState<string | undefined>(""); | ||
const authUser = useAuthUser(); | ||
|
||
const { | ||
|
@@ -75,6 +77,27 @@ export const FacilityHome = ({ facilityId }: Props) => { | |
}, | ||
}); | ||
|
||
const fetchImage = async () => { | ||
try { | ||
let imageUrl = facilityData?.read_cover_image_url; | ||
if (imageUrl) { | ||
await fetch(`${imageUrl}`, { | ||
headers: { "Force-Revalidate": "1" }, | ||
credentials: "include", | ||
mode: "no-cors", | ||
Comment on lines
+86
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are these headers required? how does this differ from with and without these? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. credentials isn't required, mode is though; was getting 502 error for the fetch call without it. |
||
}); | ||
imageUrl += "?" + Date.now(); | ||
setCoverImageUrl(imageUrl); | ||
Comment on lines
+89
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not add query params. We know for sure that it always fetches the latest image with query params present. There's no need to perform the vary header method when giving query params right. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will remove 👍 I added query params since fetch/vary header behavior wasn't consistent on Firefox (though it's fine on Safari). |
||
} | ||
} catch (error) { | ||
return; | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
setCoverImageUrl(facilityData?.read_cover_image_url); | ||
}, [facilityData?.read_cover_image_url]); | ||
|
||
const handleDeleteClose = () => { | ||
setOpenDeleteDialog(false); | ||
}; | ||
|
@@ -125,11 +148,12 @@ export const FacilityHome = ({ facilityId }: Props) => { | |
const CoverImage = () => ( | ||
<> | ||
<img | ||
src={`${facilityData?.read_cover_image_url}`} | ||
src={coverImageUrl} | ||
alt={facilityData?.name} | ||
className="h-full w-full rounded-lg object-cover" | ||
onLoad={() => setCoverImageLoaded(true)} | ||
/> | ||
{coverImageEdited && ( | ||
{coverImageEdited && !coverImageLoaded && ( | ||
<div className="absolute inset-x-0 bottom-0 w-full rounded-b-md bg-black/70 px-2 pb-0.5 backdrop-blur-sm"> | ||
<span className="text-center text-xs font-medium text-secondary-100"> | ||
{t("cover_image_updated_note")} | ||
Jacobjeevan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -163,6 +187,7 @@ export const FacilityHome = ({ facilityId }: Props) => { | |
open={editCoverImage} | ||
onSave={() => { | ||
facilityFetch(); | ||
fetchImage(); | ||
setCoverImageEdited(true); | ||
Comment on lines
189
to
191
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we wait till the fetch image is done (in the loading state itself of the image edit modal) before running facility fetch? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, but ensure the loading state is shown in the image edit modal till it completes. |
||
}} | ||
onClose={() => setEditCoverImage(false)} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And these unnecessay useStates too could be removed that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, coverImageLoaded is set based on the image load (addressing #8480 (comment)). Can remove the second one though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but that state is being used only to decide whether the tooltip should be shown or not right? but when we are going to remove the tooltip anyhow, this state is not needed in the first place right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, but are we removing the tool tip entirely? I thought it was to remove tool tip on successful image load.
Hmm, well the modal closes immediately, so shouldn't be an issue. On a side note, we could use the tool tip here (and remove the coverImageLoaded state).