Skip to content

Commit

Permalink
CLDR-16900 Fix bugs in the previous PR
Browse files Browse the repository at this point in the history
-Check whether user is logged in before calling schedule.tooSoon

-When the Announcements page gets opened, fetch immediately, since previous fetch only set unread count

-New method cldrSchedule.reset facilitates immediate re-fetch

-In cldrSchedule.tooSoon return false if this.lastRequestTime is zero (reset or not yet set)

-Comments
  • Loading branch information
btangmu authored and Squash Bot committed Mar 29, 2024
1 parent 8833c52 commit 57ab468
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
33 changes: 28 additions & 5 deletions tools/cldr-apps/js/src/esm/cldrAnnounce.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,47 @@ let thePosts = null;
let callbackSetData = null;
let callbackSetUnread = null;

/**
* Get the number of unread announcements, to display in the main menu
*
* @param {Function} setUnreadCount the callback function for setting the unread count
*/
async function getUnreadCount(setUnreadCount) {
callbackSetUnread = setUnreadCount;
if (setUnreadCount) {
callbackSetUnread = setUnreadCount;
}
await refresh(callbackSetData);
}

/**
* Refresh the Announcements page and/or unread count
*
* @param {Function} viewCallbackSetData the callback function for the Announcements page, or null
*
* The callback function for setting the data may be null if the Announcements page isn't open and
* we're only getting the number of unread announcements to display in the main header
*/
async function refresh(viewCallbackSetData) {
if (DISABLE_ANNOUNCEMENTS) {
return;
}
if (schedule.tooSoon()) {
return;
if (viewCallbackSetData) {
if (!callbackSetData) {
// The Announcements page was just opened, so re-fetch the data immediately regardless
// of how recently it was fetched previously (for unread count)
schedule.reset();
}
callbackSetData = viewCallbackSetData;
}
if (!cldrStatus.getSurveyUser()) {
if (viewCallbackSetData) {
viewCallbackSetData(null);
}
return;
}
callbackSetData = viewCallbackSetData;
if (schedule.tooSoon()) {
return;
}
const url = cldrAjax.makeApiUrl("announce", null);
schedule.setRequestTime();
return await cldrAjax
Expand All @@ -63,7 +85,7 @@ function setPosts(json) {
callbackSetData(thePosts);
}
if (callbackSetUnread) {
let totalCount = thePosts.announcements?.length || 0;
const totalCount = thePosts.announcements?.length || 0;
let checkedCount = 0;
for (let announcement of thePosts.announcements) {
if (announcement.checked) {
Expand All @@ -85,6 +107,7 @@ function canChooseAllOrgs() {
}

async function compose(formState, viewCallbackComposeResult) {
schedule.reset();
const init = cldrAjax.makePostData(formState);
const url = cldrAjax.makeApiUrl("announce", null);
return await cldrAjax
Expand Down
9 changes: 7 additions & 2 deletions tools/cldr-apps/js/src/esm/cldrSchedule.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class FetchSchedule {
}
}

reset() {
this.lastRequestTime = this.lastResponseTime = 0;
}

/**
* Is it too soon to make a request?
*
Expand All @@ -44,8 +48,9 @@ export class FetchSchedule {
tooSoon() {
const now = Date.now();
if (
now < this.lastResponseTime + this.refreshMillis ||
now < this.lastRequestTime + this.refreshMillis
this.lastRequestTime &&
(now < this.lastResponseTime + this.refreshMillis ||
now < this.lastRequestTime + this.refreshMillis)
) {
if (this.debug) {
console.log(
Expand Down

0 comments on commit 57ab468

Please sign in to comment.