Skip to content

Commit

Permalink
community logo: set restriction and force fresh image
Browse files Browse the repository at this point in the history
now will set the restriction param based on the community visibility and it will force the community logo in the settings page to always update so that any changes
are immediately shown to the user, no matter the browser cache.

closes #718
  • Loading branch information
nico authored and kpsherva committed Jul 29, 2022
1 parent 3420a16 commit 3009e30
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@ const removeEmptyValues = (obj) => {
return _isNumber(obj) || _isBoolean(obj) || obj ? obj : null;
};

const LogoUploader = ({ community, defaultLogo, hasLogo, onError, logoMaxSize }) => {
const LogoUploader = ({
community,
defaultLogo,
hasLogo,
onError,
logoMaxSize,
}) => {
const currentUrl = new URL(window.location.href);
let dropzoneParams = {
preventDropOnDocument: true,
onDropAccepted: async (acceptedFiles) => {
Expand All @@ -113,7 +120,10 @@ const LogoUploader = ({ community, defaultLogo, hasLogo, onError, logoMaxSize })
try {
const client = new CommunityApi();
await client.updateLogo(community.id, file);
window.location.reload();
// set param that logo was updated
currentUrl.searchParams.set("updated", "true");

window.location.replace(currentUrl.href);
} catch (error) {
onError(error);
}
Expand All @@ -137,6 +147,13 @@ const LogoUploader = ({ community, defaultLogo, hasLogo, onError, logoMaxSize })
await client.deleteLogo(community.id);
};

// when uploading a new logo, the previously cached logo will be displayed instead of the new one. Avoid it by randomizing the URL.
const logoURL = new URL(community.links.logo);
const noCacheRandomValue = new Date().getMilliseconds() * 5;
logoURL.searchParams.set("no-cache", noCacheRandomValue.toString());

const logoWasUpdated = currentUrl.searchParams.has("updated");

return (
<Dropzone {...dropzoneParams}>
{({ getRootProps, getInputProps, open: openFileDialog }) => (
Expand All @@ -145,7 +162,7 @@ const LogoUploader = ({ community, defaultLogo, hasLogo, onError, logoMaxSize })
<input {...getInputProps()} />
<Header className="mt-0">{i18next.t("Profile picture")}</Header>
<Image
src={community.links.logo}
src={logoURL}
fallbackSrc={defaultLogo}
loadFallbackFirst={true}
fluid
Expand All @@ -169,13 +186,13 @@ const LogoUploader = ({ community, defaultLogo, hasLogo, onError, logoMaxSize })
{i18next.t("Upload new picture")}
</Button>
<label className="helptext">
{i18next.t('File must be smaller than ')}
{i18next.t("File must be smaller than ")}
{humanReadableBytes(logoMaxSize, true)}
</label>
{hasLogo && (
<DeleteButton
label={i18next.t("Delete picture")}
redirectURL={`${community.links.self_html}/settings`}
redirectURL={`${community.links.self_html}/settings?updated=true`}
confirmationMessage={
<Header as="h2" size="medium">
{i18next.t("Are you sure you want to delete this picture?")}
Expand All @@ -185,6 +202,16 @@ const LogoUploader = ({ community, defaultLogo, hasLogo, onError, logoMaxSize })
onError={onError}
/>
)}
{logoWasUpdated && (
<Message
info
icon="warning circle"
size="small"
content={i18next.t(
"It may take a few moments for changes to be visible everywhere"
)}
/>
)}
</>
)}
</Dropzone>
Expand Down Expand Up @@ -456,6 +483,7 @@ class CommunityProfileForm extends Component {
}
}
};

render() {
const { types } = this.props;
return (
Expand Down
13 changes: 11 additions & 2 deletions invenio_communities/communities/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
)
from invenio_records_resources.resources.records.utils import es_preference

from invenio_communities.proxies import current_communities

request_community_requests_search_args = request_parser(
from_conf("request_community_requests_search_args"), location="args"
)
Expand Down Expand Up @@ -166,11 +168,18 @@ def rename(self):
@request_view_args
def read_logo(self):
"""Read logo's content."""
community_pid = resource_requestctx.view_args["pid_value"]
item = self.service.read_logo(
g.identity,
resource_requestctx.view_args["pid_value"],
community_pid,
)
return item.send_file(), 200
community = current_communities.service.read(
id_=community_pid, identity=g.identity
).to_dict()

is_restricted = community["access"]["visibility"] == "restricted"

return item.send_file(restricted=is_restricted)

@request_view_args
@request_stream
Expand Down

0 comments on commit 3009e30

Please sign in to comment.