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

Add since=<date> to notifcations fetching #3346

Merged
merged 58 commits into from
Sep 29, 2023
Merged
Changes from 8 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
c939d19
Add since=<date> to notifcations fetching
CarolineDenis Apr 14, 2023
d68c312
Use formatUrl
CarolineDenis Aug 16, 2023
bef9a7f
Lint code with ESLint and Prettier
CarolineDenis Aug 16, 2023
daab08e
Use spread operator for notifications
CarolineDenis Aug 16, 2023
f3ae9a5
Merge remote-tracking branch 'origin/v7.9-dev' into issue-2606
CarolineDenis Aug 16, 2023
a7a6509
filter notifications
CarolineDenis Aug 16, 2023
74d1104
Address comments
CarolineDenis Aug 17, 2023
3f0cc0f
Format since argument
CarolineDenis Aug 18, 2023
a88b411
Reorganize code
CarolineDenis Aug 28, 2023
1a81d16
Export helper function
CarolineDenis Aug 29, 2023
074bfbe
Move function to dateFormat.ts
CarolineDenis Aug 30, 2023
2c900c4
Merge branch 'v7.9-dev' into issue-2606
melton-jason Sep 7, 2023
6c0784d
Fix faiing tests
CarolineDenis Sep 8, 2023
5ba0fd4
Lint code with ESLint and Prettier
CarolineDenis Sep 8, 2023
f1508de
Add return
CarolineDenis Sep 8, 2023
d11e8a3
Format code
CarolineDenis Sep 11, 2023
85096e9
Back end tests for notiifcations
CarolineDenis Sep 11, 2023
f90fde6
Fix param syntax
CarolineDenis Sep 11, 2023
b5410ea
Fix API call
CarolineDenis Sep 11, 2023
70b33e2
Test logger
CarolineDenis Sep 11, 2023
93af641
test
CarolineDenis Sep 11, 2023
a025237
.
CarolineDenis Sep 11, 2023
844deb7
.
CarolineDenis Sep 11, 2023
7735e97
Test time format
CarolineDenis Sep 11, 2023
c1eba19
.
CarolineDenis Sep 11, 2023
4d2bfda
test
CarolineDenis Sep 11, 2023
6bc25b6
Format time
CarolineDenis Sep 11, 2023
b499712
Test: refactor doFetch to a hook
CarolineDenis Sep 12, 2023
e310d15
Lint code with ESLint and Prettier
CarolineDenis Sep 12, 2023
1bf19cf
Test notifications
CarolineDenis Sep 12, 2023
bf536a2
Extract fetchNotifications to a hook
CarolineDenis Sep 13, 2023
8f1b9e6
Lint code with ESLint and Prettier
CarolineDenis Sep 13, 2023
814c7d0
fix notification unit test
acwhite211 Sep 15, 2023
9ed764a
remove test warning
acwhite211 Sep 15, 2023
ed999e8
Create notificationFetch test file
CarolineDenis Sep 18, 2023
188dc8f
Lint code with ESLint and Prettier
CarolineDenis Sep 18, 2023
4d61794
Extract sorting and merging logic from hook
CarolineDenis Sep 19, 2023
b9e27a7
Add unit test for mergeAndSortNotifications
CarolineDenis Sep 19, 2023
0b364f3
e console log Please enter the commit message for your changes. Lines…
CarolineDenis Sep 19, 2023
44585b5
Add url + since test
CarolineDenis Sep 19, 2023
8e33fbe
Start of unit test for notifications fetching hook
CarolineDenis Sep 19, 2023
8b9424f
Skip failing test, need to fix new Date with mockTime
CarolineDenis Sep 21, 2023
de9f655
Lint code with ESLint and Prettier
CarolineDenis Sep 21, 2023
b41f564
Add mockTime in test
CarolineDenis Sep 22, 2023
c1db4c0
Merge branch 'production' into issue-2606
grantfitzsimmons Sep 26, 2023
6e393e1
Merge remote-tracking branch 'origin/production' into issue-2606
CarolineDenis Sep 26, 2023
eae0201
Fix month being behind from 1 month
CarolineDenis Sep 28, 2023
5268ba5
Update month
CarolineDenis Sep 28, 2023
55e63fa
Merge branch 'production' into issue-2606
CarolineDenis Sep 28, 2023
ca1ec54
add assertion in test
CarolineDenis Sep 28, 2023
d74e161
Lint code with ESLint and Prettier
CarolineDenis Sep 28, 2023
82a9c48
Test
CarolineDenis Sep 28, 2023
9064ab8
Lint code with ESLint and Prettier
CarolineDenis Sep 28, 2023
3a6d07c
remove comment
CarolineDenis Sep 28, 2023
ab13204
test
CarolineDenis Sep 28, 2023
e9b7a18
Add a global variable to call API without since on first run
CarolineDenis Sep 29, 2023
29fda2a
Merge branch 'production' into issue-2606
CarolineDenis Sep 29, 2023
f3ece66
Merge remote-tracking branch 'origin/production' into issue-2606
CarolineDenis Sep 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 67 additions & 18 deletions specifyweb/frontend/js_src/lib/components/Header/Notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { icons } from '../Atoms/Icons';
import { ErrorBoundary } from '../Errors/ErrorBoundary';
import { DateElement } from '../Molecules/DateElement';
import { Dialog, dialogClassNames, LoadingScreen } from '../Molecules/Dialog';
import { formatUrl } from '../Router/queryString';
import { MenuButton } from './index';
import type { GenericNotification } from './NotificationRenderers';
import { notificationRenderers } from './NotificationRenderers';
Expand Down Expand Up @@ -42,16 +43,52 @@ export function Notifications({

React.useEffect(() => {
let pullInterval = INITIAL_INTERVAL;
let lastFetchedTimestamp: Date | undefined;

const handler = (): void => {
if (timeout !== undefined) globalThis.clearTimeout(timeout);
if (timeout !== undefined) {
CarolineDenis marked this conversation as resolved.
Show resolved Hide resolved
globalThis.clearTimeout(timeout);
}
pullInterval = INITIAL_INTERVAL;
if (document.visibilityState === 'visible') doFetch();
if (document.visibilityState === 'visible') {
doFetch();
}
};

document.addEventListener('visibilitychange', handler);

let timeout: NodeJS.Timeout | undefined = undefined;

function doFetch(): void {
function doFetch(since?: Date): void {
const startFetchTimestamp = new Date();

const now = new Date();
CarolineDenis marked this conversation as resolved.
Show resolved Hide resolved
const year = now.getFullYear();
const month = now.getMonth().toString();
const day = now.getDate().toString();
const hours = now.getHours().toString();
const minutes = now.getMinutes().toString();
const seconds = now.getSeconds().toString();

const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

const sinceDate = since;
const sinceYear = sinceDate?.getFullYear();
const sinceMonth = sinceDate?.getMonth().toString();
const sinceDay = sinceDate?.getDate().toString();
const sinceHours = sinceDate?.getHours().toString();
const sinceMinutes = sinceDate?.getMinutes().toString();
const sinceSeconds = sinceDate?.getSeconds().toString();

const formattedSince = `${sinceYear}-${sinceMonth}-${sinceDay} ${sinceHours}:${sinceMinutes}:${sinceSeconds}`;

const queryString = since ? formattedSince : formattedDate;
CarolineDenis marked this conversation as resolved.
Show resolved Hide resolved
const url = formatUrl(`/notifications/messages/`, {
CarolineDenis marked this conversation as resolved.
Show resolved Hide resolved
since: queryString,
});

lastFetchedTimestamp = new Date();
CarolineDenis marked this conversation as resolved.
Show resolved Hide resolved

/*
* Poll interval is scaled exponentially to reduce requests if the tab is
* left open.
Expand All @@ -69,8 +106,8 @@ export function Notifications({
}
>
>(
`/notifications/messages/`,

url,
// eslint-disable-next-line @typescript-eslint/naming-convention
{
headers: { Accept: 'application/json' },
/*
Expand All @@ -85,11 +122,20 @@ export function Notifications({
}
)
)
.then(({ data: notifications }) => {
.then(({ data: newNotifications }) => {
if (destructorCalled) return undefined;
setNotifications(
notifications
.map(
setNotifications((existingNotifications) => {
const filteredNewNotifications = newNotifications.filter(
(newNotification) =>
!existingNotifications?.some(
(existingNotification) =>
existingNotification.messageId ===
newNotification.message_id
CarolineDenis marked this conversation as resolved.
Show resolved Hide resolved
)
);
return [
...(existingNotifications ?? []),
...filteredNewNotifications.map(
// eslint-disable-next-line @typescript-eslint/naming-convention
({ message_id, read, timestamp, type, ...rest }) => ({
messageId: message_id,
Expand All @@ -98,21 +144,24 @@ export function Notifications({
type,
payload: rest as IR<LocalizedString>,
})
)
),
// Make most recent notification first
.sort(
sortFunction(
({ timestamp }) => new Date(timestamp).getTime(),
true
)
].sort(
CarolineDenis marked this conversation as resolved.
Show resolved Hide resolved
sortFunction(
({ timestamp }) => new Date(timestamp).getTime(),
true
)
);
);
});
lastFetchedTimestamp = startFetchTimestamp;
// Stop updating if tab is hidden
timeout =
document.visibilityState === 'hidden'
? undefined
: globalThis.setTimeout(doFetch, pullInterval);
return undefined;
: globalThis.setTimeout(
() => doFetch(lastFetchedTimestamp),
pullInterval
);
})
.catch(console.error);
}
Expand Down