Skip to content

PM-973 - invite by email - fix awaiting for project updates to propagate #1634

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

Merged
merged 2 commits into from
Apr 9, 2025
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
22 changes: 14 additions & 8 deletions src/containers/ProjectInvitations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ConfirmationModal from '../../components/Modal/ConfirmationModal'
import styles from './ProjectInvitations.module.scss'
import { updateProjectMemberInvite } from '../../services/projectMemberInvites'
import { PROJECT_MEMBER_INVITE_STATUS_ACCEPTED, PROJECT_MEMBER_INVITE_STATUS_REFUSED } from '../../config/constants'
import { delay } from '../../util/delay'

const theme = {
container: styles.modalContainer
Expand Down Expand Up @@ -44,9 +45,16 @@ const ProjectInvitations = ({ match, auth, isProjectLoading, history, projectDet
const updateInvite = useCallback(async (status) => {
setIsUpdating(status)
await updateProjectMemberInvite(projectId, invitation.id, status)

// await for the project details to propagate
await delay(1000)
await loadProject(projectId)
toastr.success('Success', `Successfully ${status} the invitation.`)

// await for the project details to fetch
await delay(1000)
history.push(status === PROJECT_MEMBER_INVITE_STATUS_ACCEPTED ? `/projects/${projectId}/challenges` : '/projects')
}, [invitation])
}, [projectId, invitation, loadProject, history])

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

setTimeout(() => {
if (automaticAction === PROJECT_MEMBER_INVITE_STATUS_ACCEPTED) {
acceptInvite()
} else if (automaticAction === PROJECT_MEMBER_INVITE_STATUS_REFUSED) {
declineInvite()
}
}, [1500])
if (automaticAction === PROJECT_MEMBER_INVITE_STATUS_ACCEPTED) {
acceptInvite()
} else if (automaticAction === PROJECT_MEMBER_INVITE_STATUS_REFUSED) {
declineInvite()
}
}, [invitation, automaticAction])

return (
Expand Down
25 changes: 25 additions & 0 deletions src/util/delay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Creates a delay that can be awaited, and optionally aborted via an AbortSignal.
*
* @param {number} ms - The number of milliseconds to delay.
* @param {AbortSignal} signal - Optional AbortSignal to cancel the delay early.
* @returns A Promise that resolves after the delay, or rejects if aborted.
*/
export function delay (ms, signal) {
return new Promise((resolve, reject) => {
// Start a timer that will resolve the promise after `ms` milliseconds
const timeout = setTimeout(resolve, ms)

// If an AbortSignal is provided, handle abort events
if (signal) {
// Listen for the 'abort' event
signal.addEventListener('abort', () => {
// Cancel the timeout so it doesn't resolve the promise later
clearTimeout(timeout)
// Reject the promise with a DOMException for consistency with other abortable APIs
// eslint-disable-next-line no-undef
reject(new DOMException('Delay aborted', 'AbortError'))
}, { once: true }) // 'once' ensures the handler is removed automatically after it runs
}
})
}