Skip to content

Commit bf4baf6

Browse files
authored
CLDR-16900 Fix bugs in the previous PR (#3587)
1 parent 8833c52 commit bf4baf6

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

tools/cldr-apps/js/src/esm/cldrAnnounce.mjs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,47 @@ let thePosts = null;
2727
let callbackSetData = null;
2828
let callbackSetUnread = null;
2929

30+
/**
31+
* Get the number of unread announcements, to display in the main menu
32+
*
33+
* @param {Function} setUnreadCount the callback function for setting the unread count
34+
*/
3035
async function getUnreadCount(setUnreadCount) {
31-
callbackSetUnread = setUnreadCount;
36+
if (setUnreadCount) {
37+
callbackSetUnread = setUnreadCount;
38+
}
3239
await refresh(callbackSetData);
3340
}
3441

42+
/**
43+
* Refresh the Announcements page and/or unread count
44+
*
45+
* @param {Function} viewCallbackSetData the callback function for the Announcements page, or null
46+
*
47+
* The callback function for setting the data may be null if the Announcements page isn't open and
48+
* we're only getting the number of unread announcements to display in the main header
49+
*/
3550
async function refresh(viewCallbackSetData) {
3651
if (DISABLE_ANNOUNCEMENTS) {
3752
return;
3853
}
39-
if (schedule.tooSoon()) {
40-
return;
54+
if (viewCallbackSetData) {
55+
if (!callbackSetData) {
56+
// The Announcements page was just opened, so re-fetch the data immediately regardless
57+
// of how recently it was fetched previously (for unread count)
58+
schedule.reset();
59+
}
60+
callbackSetData = viewCallbackSetData;
4161
}
4262
if (!cldrStatus.getSurveyUser()) {
4363
if (viewCallbackSetData) {
4464
viewCallbackSetData(null);
4565
}
4666
return;
4767
}
48-
callbackSetData = viewCallbackSetData;
68+
if (schedule.tooSoon()) {
69+
return;
70+
}
4971
const url = cldrAjax.makeApiUrl("announce", null);
5072
schedule.setRequestTime();
5173
return await cldrAjax
@@ -63,7 +85,7 @@ function setPosts(json) {
6385
callbackSetData(thePosts);
6486
}
6587
if (callbackSetUnread) {
66-
let totalCount = thePosts.announcements?.length || 0;
88+
const totalCount = thePosts.announcements?.length || 0;
6789
let checkedCount = 0;
6890
for (let announcement of thePosts.announcements) {
6991
if (announcement.checked) {
@@ -85,6 +107,7 @@ function canChooseAllOrgs() {
85107
}
86108

87109
async function compose(formState, viewCallbackComposeResult) {
110+
schedule.reset();
88111
const init = cldrAjax.makePostData(formState);
89112
const url = cldrAjax.makeApiUrl("announce", null);
90113
return await cldrAjax

tools/cldr-apps/js/src/esm/cldrSchedule.mjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export class FetchSchedule {
3636
}
3737
}
3838

39+
reset() {
40+
this.lastRequestTime = this.lastResponseTime = 0;
41+
}
42+
3943
/**
4044
* Is it too soon to make a request?
4145
*
@@ -44,8 +48,9 @@ export class FetchSchedule {
4448
tooSoon() {
4549
const now = Date.now();
4650
if (
47-
now < this.lastResponseTime + this.refreshMillis ||
48-
now < this.lastRequestTime + this.refreshMillis
51+
this.lastRequestTime &&
52+
(now < this.lastResponseTime + this.refreshMillis ||
53+
now < this.lastRequestTime + this.refreshMillis)
4954
) {
5055
if (this.debug) {
5156
console.log(

0 commit comments

Comments
 (0)