-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved methods before merge with the other parts
- Loading branch information
1 parent
e15bc15
commit 3c750c1
Showing
11 changed files
with
116 additions
and
28 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
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
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
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
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
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
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,42 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { useLocation, useNavigate } from 'react-router'; | ||
|
||
import { addQueryParam } from '#src/utils/location'; | ||
import { checkEntitlements, reloadActiveSubscription } from '#src/stores/AccountController'; | ||
|
||
type intervalCheckAccessPayload = { | ||
interval?: number; | ||
iterations?: number; | ||
offerId?: string; | ||
}; | ||
const useCheckAccess = () => { | ||
const intervalRef = useRef<number>(); | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
|
||
const intervalCheckAccess = ({ interval = 3000, iterations = 5, offerId }: intervalCheckAccessPayload) => { | ||
intervalRef.current = window.setInterval(async () => { | ||
if (!offerId) { | ||
offerId = '115047'; | ||
} | ||
const hasAccess = await checkEntitlements(offerId); | ||
|
||
if (hasAccess) { | ||
await reloadActiveSubscription(); | ||
navigate(addQueryParam(location, 'u', 'welcome')); | ||
} else if (--iterations === 0) { | ||
window.clearInterval(intervalRef.current); | ||
} | ||
}, interval); | ||
}; | ||
|
||
useEffect(() => { | ||
return () => { | ||
window.clearInterval(intervalRef.current); | ||
}; | ||
}, []); | ||
|
||
return { intervalCheckAccess }; | ||
}; | ||
|
||
export default useCheckAccess; |
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,31 @@ | ||
import { useEffect } from 'react'; | ||
import { useLocation, useNavigate } from 'react-router'; | ||
|
||
import { useNotificationStore } from '#src/stores/NotificationStore'; | ||
import { addQueryParams } from '#src/utils/formatting'; | ||
import { addQueryParam } from '#src/utils/location'; | ||
|
||
const useNotifications = () => { | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
const notification = useNotificationStore((state) => state); | ||
|
||
useEffect(() => { | ||
if (notification.type?.endsWith('.failed')) { | ||
navigate( | ||
addQueryParams(window.location.href, { | ||
u: 'paypal-error', | ||
message: (notification.resource as Error)?.message, | ||
}), | ||
); | ||
} else if (notification.type === 'access.granted') { | ||
navigate(addQueryParam(location, 'u', 'welcome')); | ||
} | ||
|
||
//eslint-disable-next-line | ||
}, [notification]); | ||
|
||
return notification; | ||
}; | ||
|
||
export default useNotifications; |
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
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
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