Skip to content

Commit

Permalink
feat: register message listener logic refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
createhb21 committed Aug 5, 2024
1 parent 077bfc0 commit 4760094
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
27 changes: 13 additions & 14 deletions hooks/fcm/mutaion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { useEffect } from "react";
import { isWebView } from "../../utils/appEnvUtils";
import { urlBase64ToUint8Array } from "../../utils/convertUtils/convertBase64";
import { nativeMethodUtils } from "../../utils/nativeMethodUtils";
import { isEmpty } from "../../utils/validationUtils";
import { isEmpty, isNil } from "../../utils/validationUtils";
import { registerPushServiceWithApp, registerPushServiceWithPWA } from "./apis";
import { DeviceInfo } from "./types";
import { requestNotificationPermission } from "./utils";

export const usePushServiceInitialize = () => {
export const usePushServiceInitialize = ({ uid }: { uid?: string }) => {
useEffect(() => {
if (isWebView()) {
const deviceInfoMessageListener = ({ data }: MessageEvent) => {
if (typeof data === "string" && data.includes("deviceInfo")) {
subscribePushServiceOnAPP(data);
const deviceInfos: DeviceInfo = data ? JSON.parse(data) : {};
const shouldRegisterListener = !isNil(uid) && !isEmpty(deviceInfos);

if (shouldRegisterListener) {
subscribePushServiceOnAPP(data, uid);
}
};

Expand All @@ -30,17 +33,13 @@ export const usePushServiceInitialize = () => {
}, []);
};

const subscribePushServiceOnAPP = async (data: string) => {
const subscribePushServiceOnAPP = async (data: DeviceInfo, uid: string) => {
try {
const deviceInfos: DeviceInfo = data ? JSON.parse(data) : {};
const hasInfos = !isEmpty(deviceInfos);

if (hasInfos) {
await registerPushServiceWithApp({
fcmToken: deviceInfos.fcmToken,
platform: deviceInfos.platform,
});
}
await registerPushServiceWithApp({
uid,
fcmToken: data.fcmToken,
platform: data.platform,
});
} catch (err) {
console.error("Error parsing device info message:", err);
}
Expand Down
6 changes: 4 additions & 2 deletions hooks/fcm/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export type DeviceInfo = {
import { IUserSummary } from "../../types/models/userTypes/userInfoTypes";

export interface DeviceInfo extends Pick<IUserSummary, "uid"> {
fcmToken: string;
platform: string;
};
}
6 changes: 4 additions & 2 deletions pageTemplates/home/HomeInitialSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import { checkAndSetLocalStorage } from "../../utils/storageUtils";
import { detectDevice } from "../../utils/validationUtils";

function HomeInitialSetting() {
usePushServiceInitialize();
const { data: session } = useSession();
usePushServiceInitialize({
uid: session?.user?.uid,
});

const router = useRouter();
const toast = useToast();
const { data: session } = useSession();
const searchParams = useSearchParams();
const dateParam = searchParams.get("date");
const isGuest = session?.user.name === "guest";
Expand Down

0 comments on commit 4760094

Please sign in to comment.