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 inveniosoftware#718
  • Loading branch information
nico committed Jul 7, 2022
1 parent 53bfef3 commit b73ac1c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ 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,
}) => {
let dropzoneParams = {
preventDropOnDocument: true,
onDropAccepted: async (acceptedFiles) => {
Expand Down Expand Up @@ -137,6 +143,11 @@ 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());

return (
<Dropzone {...dropzoneParams}>
{({ getRootProps, getInputProps, open: openFileDialog }) => (
Expand All @@ -145,7 +156,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 Down Expand Up @@ -185,6 +196,11 @@ const LogoUploader = ({ community, defaultLogo, hasLogo, onError, logoMaxSize })
onError={onError}
/>
)}
<label className="helptext mt-5 mb-0">
{i18next.t(
"It may take a few moments for changes to be visible everywhere"
)}
</label>
</>
)}
</Dropzone>
Expand Down Expand Up @@ -456,6 +472,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 b73ac1c

Please sign in to comment.