forked from ohcnetwork/care_fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'coronasafe:develop' into Fix-ohcnetwork#7708
- Loading branch information
Showing
27 changed files
with
252 additions
and
514 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useEffect, useState } from "react"; | ||
import routes from "../../Redux/api"; | ||
import request from "../../Utils/request/request"; | ||
import useAuthUser from "./useAuthUser"; | ||
import * as Sentry from "@sentry/browser"; | ||
|
||
export type NotificationSubscriptionState = | ||
| "unsubscribed" | ||
| "subscribed" | ||
| "subscribed_on_other_device" | ||
| "subscribed" | ||
| "pending" | ||
| "error"; | ||
|
||
/** | ||
* This is a temporary hook and will be removed in the future. | ||
* | ||
* This hook is used to get the initial notification subscription state of the user. | ||
* @returns NotificationSubscriptionState | ||
*/ | ||
export default function useNotificationSubscriptionState( | ||
dependencies: any[] = [], | ||
) { | ||
const { username } = useAuthUser(); | ||
const [subscriptionState, setSubscriptionState] = | ||
useState<NotificationSubscriptionState>("pending"); | ||
|
||
const getSubscriptionState = async () => { | ||
try { | ||
const res = await request(routes.getUserPnconfig, { | ||
pathParams: { username }, | ||
}); | ||
|
||
const reg = await navigator.serviceWorker.ready; | ||
const subscription = await reg.pushManager.getSubscription(); | ||
|
||
if (!subscription && !res.data?.pf_endpoint) { | ||
setSubscriptionState("unsubscribed"); | ||
} else if (subscription?.endpoint !== res.data?.pf_endpoint) { | ||
setSubscriptionState("subscribed_on_other_device"); | ||
} else { | ||
setSubscriptionState("subscribed"); | ||
} | ||
} catch (error) { | ||
setSubscriptionState("error"); | ||
Sentry.captureException(error); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
getSubscriptionState(); | ||
}, [username, ...dependencies]); | ||
|
||
return subscriptionState; | ||
} |
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
Oops, something went wrong.