Skip to content

Commit 8255022

Browse files
authored
Merge pull request #1634 from topcoder-platform/PM-973_invite-by-mail
PM-973 - invite by email - fix awaiting for project updates to propagate
2 parents 91b2eb2 + 7c2a793 commit 8255022

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/containers/ProjectInvitations/index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import ConfirmationModal from '../../components/Modal/ConfirmationModal'
1111
import styles from './ProjectInvitations.module.scss'
1212
import { updateProjectMemberInvite } from '../../services/projectMemberInvites'
1313
import { PROJECT_MEMBER_INVITE_STATUS_ACCEPTED, PROJECT_MEMBER_INVITE_STATUS_REFUSED } from '../../config/constants'
14+
import { delay } from '../../util/delay'
1415

1516
const theme = {
1617
container: styles.modalContainer
@@ -44,9 +45,16 @@ const ProjectInvitations = ({ match, auth, isProjectLoading, history, projectDet
4445
const updateInvite = useCallback(async (status) => {
4546
setIsUpdating(status)
4647
await updateProjectMemberInvite(projectId, invitation.id, status)
48+
49+
// await for the project details to propagate
50+
await delay(1000)
51+
await loadProject(projectId)
4752
toastr.success('Success', `Successfully ${status} the invitation.`)
53+
54+
// await for the project details to fetch
55+
await delay(1000)
4856
history.push(status === PROJECT_MEMBER_INVITE_STATUS_ACCEPTED ? `/projects/${projectId}/challenges` : '/projects')
49-
}, [invitation])
57+
}, [projectId, invitation, loadProject, history])
5058

5159
const acceptInvite = useCallback(() => updateInvite(PROJECT_MEMBER_INVITE_STATUS_ACCEPTED), [updateInvite])
5260
const declineInvite = useCallback(() => updateInvite(PROJECT_MEMBER_INVITE_STATUS_REFUSED), [updateInvite])
@@ -56,13 +64,11 @@ const ProjectInvitations = ({ match, auth, isProjectLoading, history, projectDet
5664
return
5765
}
5866

59-
setTimeout(() => {
60-
if (automaticAction === PROJECT_MEMBER_INVITE_STATUS_ACCEPTED) {
61-
acceptInvite()
62-
} else if (automaticAction === PROJECT_MEMBER_INVITE_STATUS_REFUSED) {
63-
declineInvite()
64-
}
65-
}, [1500])
67+
if (automaticAction === PROJECT_MEMBER_INVITE_STATUS_ACCEPTED) {
68+
acceptInvite()
69+
} else if (automaticAction === PROJECT_MEMBER_INVITE_STATUS_REFUSED) {
70+
declineInvite()
71+
}
6672
}, [invitation, automaticAction])
6773

6874
return (

src/util/delay.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Creates a delay that can be awaited, and optionally aborted via an AbortSignal.
3+
*
4+
* @param {number} ms - The number of milliseconds to delay.
5+
* @param {AbortSignal} signal - Optional AbortSignal to cancel the delay early.
6+
* @returns A Promise that resolves after the delay, or rejects if aborted.
7+
*/
8+
export function delay (ms, signal) {
9+
return new Promise((resolve, reject) => {
10+
// Start a timer that will resolve the promise after `ms` milliseconds
11+
const timeout = setTimeout(resolve, ms)
12+
13+
// If an AbortSignal is provided, handle abort events
14+
if (signal) {
15+
// Listen for the 'abort' event
16+
signal.addEventListener('abort', () => {
17+
// Cancel the timeout so it doesn't resolve the promise later
18+
clearTimeout(timeout)
19+
// Reject the promise with a DOMException for consistency with other abortable APIs
20+
// eslint-disable-next-line no-undef
21+
reject(new DOMException('Delay aborted', 'AbortError'))
22+
}, { once: true }) // 'once' ensures the handler is removed automatically after it runs
23+
}
24+
})
25+
}

0 commit comments

Comments
 (0)