Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
fjsj committed Feb 5, 2025
1 parent de03d73 commit 8b6588d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/(app)/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export default function AppLayout() {
const isPractitioner = profile?.resourceType === "Practitioner";

useEffect(() => {
if (profile && medplum.getActiveLogin()) {
if (profile) {
// Set up push notifications when user is logged in
setUpPushNotifications();
}
}, [medplum, profile, setUpPushNotifications]);
}, [profile, setUpPushNotifications]);

if (medplum.isLoading()) {
return <LoadingScreen />;
Expand Down
58 changes: 28 additions & 30 deletions contexts/NotificationsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const NotificationsContext = createContext<NotificationsContextType>({
setUpPushNotifications: async () => false,
});

async function registerForPushNotificationsAsync() {
async function getPushToken() {
let token;

if (!Device.isDevice) {
Expand Down Expand Up @@ -56,36 +56,34 @@ async function registerForPushNotificationsAsync() {
}
}

function updateMedplumPushToken(medplum: MedplumClient, token: string): Promise<void> {
return (async () => {
try {
const profile = medplum.getProfile();
if (!profile) return;

// Check if token already exists and matches
const existingToken = profile.extension?.find(
(e) => e.url === "https://medplum.com/push-token",
)?.valueString;
async function updateProfilePushToken(medplum: MedplumClient, token: string) {
try {
const profile = medplum.getProfile();
if (!profile) return;

if (existingToken === token) {
return; // Token hasn't changed, no need to update
}
// Check if token already exists and matches
const existingToken = profile.extension?.find(
(e) => e.url === "https://medplum.com/push-token",
)?.valueString;

// Update the token
const extensions =
profile.extension?.filter((e) => e.url !== "https://medplum.com/push-token") || [];
extensions.push({
url: "https://medplum.com/push-token",
valueString: token,
});
await medplum.updateResource({
...profile,
extension: extensions,
});
} catch (error) {
console.error("Error updating push token in Medplum:", error);
if (existingToken === token) {
return; // Token hasn't changed, no need to update
}
})();

// Update the token
const extensions =
profile.extension?.filter((e) => e.url !== "https://medplum.com/push-token") || [];
extensions.push({
url: "https://medplum.com/push-token",
valueString: token,
});
await medplum.updateResource({
...profile,
extension: extensions,
});
} catch (error) {
console.error("Error updating push token in Medplum:", error);
}
}

function handleMessageNotificationInteraction(response: Notifications.NotificationResponse) {
Expand All @@ -112,9 +110,9 @@ export function NotificationsProvider({ children }: { children: React.ReactNode
const isEnabled = finalStatus === "granted";
setIsNotificationsEnabled(isEnabled);
if (isEnabled) {
const token = await registerForPushNotificationsAsync();
const token = await getPushToken();
if (token) {
await updateMedplumPushToken(medplum, token);
await updateProfilePushToken(medplum, token);
}

// Set up notification listeners
Expand Down

0 comments on commit 8b6588d

Please sign in to comment.