Skip to content
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

CLDR-16900 Fix bugs in the previous PR #3587

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading