-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notifications: contextualize email notifications
- Loading branch information
Showing
2 changed files
with
171 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
src/components/EmailNotifications/EmailNotificationsProvider.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import React, { | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from 'react' | ||
import PropTypes from 'prop-types' | ||
import { useWallet } from '../../providers/Wallet' | ||
import { | ||
getJurorEmail, | ||
getSubscriptionDetails, | ||
subscribeExistingEmail, | ||
subscribeToNotifications, | ||
} from '../../services/servicesRequests' | ||
|
||
const EmailNotificationsContext = React.createContext() | ||
|
||
function EmailNotificationsProvider({ children }) { | ||
const { account } = useWallet() | ||
|
||
const asyncCancelled = useRef(false) | ||
const [email, setEmail] = useState(null) | ||
const [needsSignature, setNeedsSignature] = useState(false) | ||
const [subscriptionDetails, setSubscriptionDetails] = useState({ | ||
error: null, | ||
fetching: false, | ||
}) | ||
|
||
const handleOnSubscribe = useCallback( | ||
async email => { | ||
const response = await subscribeToNotifications(account, email) | ||
|
||
if (response.error && !response.needsSignature) { | ||
return response.error | ||
} | ||
|
||
if (!asyncCancelled.current) { | ||
setEmail(email) | ||
if (needsSignature !== Boolean(response.needsSignature)) { | ||
setNeedsSignature(!needsSignature) | ||
} | ||
} | ||
}, | ||
[account, needsSignature] | ||
) | ||
|
||
const handleOnSubscribeExistingEmail = useCallback(async () => { | ||
const response = await subscribeExistingEmail(account) | ||
|
||
if (response.error && !response.needsSignature) { | ||
return response.error | ||
} | ||
|
||
if (!asyncCancelled.current) { | ||
if (email !== response.email) { | ||
setEmail(response.email) | ||
if (needsSignature !== Boolean(response.needsSignature)) { | ||
setNeedsSignature(!needsSignature) | ||
} | ||
} | ||
} | ||
}, [account, email, needsSignature]) | ||
|
||
// Cancel any async requests if this provider is unmounted | ||
useEffect(() => { | ||
return () => { | ||
asyncCancelled.current = true | ||
} | ||
}, []) | ||
|
||
// When account connects, fetch their subscription details | ||
useEffect(() => { | ||
if (!account) { | ||
return | ||
} | ||
|
||
const fetchSubscriptionDetails = async () => { | ||
const response = await getSubscriptionDetails(account) | ||
|
||
if (!asyncCancelled.current) { | ||
setSubscriptionDetails({ | ||
...response, | ||
fetching: false, | ||
}) | ||
} | ||
} | ||
|
||
setSubscriptionDetails(subscriptionDetails => ({ | ||
...subscriptionDetails, | ||
error: null, | ||
fetching: true, | ||
})) | ||
fetchSubscriptionDetails() | ||
}, [account]) | ||
|
||
// Once we know the account has an associated email, fetch its email | ||
useEffect(() => { | ||
if (!subscriptionDetails.emailExists) { | ||
return | ||
} | ||
|
||
const getEmail = async () => { | ||
const { needsSignature, email } = await getJurorEmail(account) | ||
|
||
if (!asyncCancelled.current) { | ||
if (email) { | ||
setEmail(email) | ||
} | ||
if (needsSignature) { | ||
setNeedsSignature(true) | ||
} | ||
} | ||
} | ||
|
||
getEmail() | ||
}, [account, subscriptionDetails.emailExists]) | ||
|
||
return ( | ||
<EmailNotificationsContext.Provider | ||
value={{ | ||
email, | ||
handleOnSubscribe, | ||
handleOnSubscribeExistingEmail, | ||
needsSignature, | ||
subscriptionDetails, | ||
}} | ||
> | ||
{children} | ||
</EmailNotificationsContext.Provider> | ||
) | ||
} | ||
|
||
EmailNotificationsProvider.propTypes = { | ||
children: PropTypes.node, | ||
} | ||
|
||
function useEmailNotifications() { | ||
return useContext(EmailNotificationsContext) | ||
} | ||
|
||
export { EmailNotificationsProvider, useEmailNotifications } |