continue generating flow, add backend connection on main tabs
This commit is contained in:
@@ -1,155 +0,0 @@
|
||||
import {
|
||||
createContext,
|
||||
useContext,
|
||||
useEffect,
|
||||
useGlobal,
|
||||
setGlobal,
|
||||
} from "reactn";
|
||||
import messaging from "@react-native-firebase/messaging";
|
||||
import notifee, { EventType } from "@notifee/react-native";
|
||||
import { CommonActions } from "@react-navigation/core";
|
||||
import { useAppState } from "@react-native-community/hooks";
|
||||
import { Linking } from "react-native";
|
||||
|
||||
import { dispatch } from "../navigation/NavigationService";
|
||||
import { Routes } from "../navigation";
|
||||
import firebase, { usersRef } from "../config/firebase";
|
||||
import alert from "../components/Alert";
|
||||
|
||||
export const NotificationContext = createContext();
|
||||
|
||||
export async function displayNotification(data) {
|
||||
const { title, message, picture = null, video = null } = data || {};
|
||||
|
||||
let ios = {
|
||||
categoryId: "default",
|
||||
};
|
||||
|
||||
if (picture) {
|
||||
ios.attachments = [{ url: picture }];
|
||||
}
|
||||
|
||||
if (video) {
|
||||
ios.attachments = [
|
||||
{
|
||||
url: video,
|
||||
thumbnailTime: data?.thumbnailTime
|
||||
? parseInt(data?.thumbnailTime, 10)
|
||||
: 0,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
await notifee.displayNotification({
|
||||
title,
|
||||
body: message,
|
||||
ios,
|
||||
android: {
|
||||
channelId: "default",
|
||||
},
|
||||
data,
|
||||
});
|
||||
|
||||
await notifee.incrementBadgeCount();
|
||||
}
|
||||
|
||||
export async function triggerEvent({ type, detail }) {
|
||||
const { notification } = detail;
|
||||
|
||||
if (type === EventType.PRESS) {
|
||||
const { data = {} } = notification || {};
|
||||
const { projectID = null, taskID = null, messageID = null } = data || {};
|
||||
|
||||
if (projectID) {
|
||||
setGlobal({ currentProjectID: projectID });
|
||||
|
||||
if (taskID) {
|
||||
dispatch(
|
||||
CommonActions.navigate({
|
||||
name: Routes.TaskDetails,
|
||||
params: { taskID, messageID },
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default ({ children }) => {
|
||||
const [currentUID] = useGlobal("currentUID");
|
||||
|
||||
const currentAppState = useAppState();
|
||||
|
||||
useEffect(() => {
|
||||
if (currentUID) {
|
||||
updateFCMToken({ uid: currentUID });
|
||||
}
|
||||
}, [currentUID]);
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = messaging().onMessage(async (remoteMessage) => {
|
||||
console.log("Message handled in the foreground!", remoteMessage);
|
||||
const { data } = remoteMessage;
|
||||
|
||||
await displayNotification(data);
|
||||
});
|
||||
const foregroundSubscription = notifee.onForegroundEvent(triggerEvent);
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
foregroundSubscription();
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (currentAppState === "active") {
|
||||
notifee.setBadgeCount(0);
|
||||
}
|
||||
}, [currentAppState]);
|
||||
|
||||
const updateFCMToken = async ({ uid }) => {
|
||||
try {
|
||||
const authResponse = await messaging().requestPermission();
|
||||
const enabled = authResponse === messaging.AuthorizationStatus.AUTHORIZED;
|
||||
if (enabled) {
|
||||
const fcmToken = await messaging().getToken();
|
||||
if (fcmToken) {
|
||||
await usersRef.doc(uid).update({
|
||||
fcmTokens: firebase.firestore.FieldValue.arrayUnion(fcmToken),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
alert(
|
||||
"Activez les notifications",
|
||||
"Afin de rester informé de l'avancée de votre projet, il est important d'activer les notifications.",
|
||||
[
|
||||
{
|
||||
text: "Annuler",
|
||||
onPress: () => {},
|
||||
style: "cancel",
|
||||
},
|
||||
{
|
||||
text: "Paramètres",
|
||||
onPress: async () => {
|
||||
Linking.openSettings();
|
||||
},
|
||||
style: "confirm",
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<NotificationContext.Provider value={{}}>
|
||||
{children}
|
||||
</NotificationContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useNotification = () => {
|
||||
return useContext(NotificationContext);
|
||||
};
|
||||
@@ -1,56 +0,0 @@
|
||||
import React, { useContext, createContext, useEffect, useState } from "react";
|
||||
|
||||
import firebase from "../config/firebase";
|
||||
import { useUserData } from "./UserDataProvider";
|
||||
|
||||
export const PremiumContext = createContext();
|
||||
|
||||
export default ({ children }) => {
|
||||
const { currentUID } = useUserData();
|
||||
|
||||
const [subscriptions, setSubscriptions] = useState(null);
|
||||
|
||||
const subscriptionType = subscriptions?.length > 0 ? "PREMIUM" : "FREE";
|
||||
const isPaying = subscriptionType !== "FREE";
|
||||
|
||||
useEffect(() => {
|
||||
if (currentUID) {
|
||||
getPremiumStatus();
|
||||
}
|
||||
}, [currentUID]);
|
||||
|
||||
const getPremiumStatus = async () => {
|
||||
try {
|
||||
const { data } = await firebase
|
||||
.functions()
|
||||
.httpsCallable("payment-getPremiumStatus")({
|
||||
userID: currentUID,
|
||||
});
|
||||
|
||||
if (data?.subscriptions?.length) {
|
||||
setSubscriptions(
|
||||
(data.subscriptions || [])?.filter((sub) => sub.status === "active")
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<PremiumContext.Provider
|
||||
value={{
|
||||
getPremiumStatus,
|
||||
|
||||
subscriptionType,
|
||||
subscriptions,
|
||||
|
||||
isPaying,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</PremiumContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const usePremium = () => useContext(PremiumContext);
|
||||
@@ -6,7 +6,6 @@ import UserDataProvider from "./UserDataProvider";
|
||||
import WebViewProvider from "./WebViewProvider";
|
||||
import SplashAnimationProvider from "./SplashAnimationProvider";
|
||||
import UniversalLinkProvider from "./UniversalLinkProvider";
|
||||
import PremiumProvider from "./PremiumProvider";
|
||||
import StripeEmbeddedProvider from "./StripeEmbeddedProvider";
|
||||
import { SheetProvider } from "react-native-actions-sheet";
|
||||
|
||||
@@ -18,7 +17,6 @@ const SharedProviders = ({ children }) => {
|
||||
[WebViewProvider, {}],
|
||||
[UserDataProvider, {}],
|
||||
[StripeEmbeddedProvider, {}],
|
||||
[PremiumProvider, {}],
|
||||
[UniversalLinkProvider, {}],
|
||||
[SheetProvider, {}],
|
||||
];
|
||||
|
||||
@@ -18,6 +18,8 @@ export default ({ children }) => {
|
||||
const [currentUID] = useGlobal("currentUID");
|
||||
const [currentUserData, setCurrentUserData] = useGlobal("currentUserData");
|
||||
const [currentUserRoles] = useGlobal("currentUserRoles");
|
||||
const [userProjects, setUserProjects] = useState([]);
|
||||
const [userLikedProjects, setUserLikedProjects] = useState([]);
|
||||
|
||||
useDataFromRef({
|
||||
ref: currentUID ? usersRef.doc(currentUID) : null,
|
||||
@@ -34,6 +36,38 @@ export default ({ children }) => {
|
||||
},
|
||||
});
|
||||
|
||||
// Subscribe to user's projects (musics)
|
||||
useDataFromRef({
|
||||
ref: currentUID
|
||||
? firebase
|
||||
.firestore()
|
||||
.collection("projects")
|
||||
.where("userId", "==", currentUID)
|
||||
.orderBy("updatedAt", "desc")
|
||||
: null,
|
||||
simpleRef: false,
|
||||
listener: true,
|
||||
condition: !!currentUID,
|
||||
refreshArray: [currentUID],
|
||||
onUpdate: (list) => setUserProjects(Array.isArray(list) ? list : []),
|
||||
});
|
||||
|
||||
// Subscribe to user's liked projects
|
||||
useDataFromRef({
|
||||
ref: currentUID
|
||||
? firebase
|
||||
.firestore()
|
||||
.collection("projects")
|
||||
.where("likedBy", "array-contains", currentUID)
|
||||
.orderBy("updatedAt", "desc")
|
||||
: null,
|
||||
simpleRef: false,
|
||||
listener: true,
|
||||
condition: !!currentUID,
|
||||
refreshArray: [currentUID],
|
||||
onUpdate: (list) => setUserLikedProjects(Array.isArray(list) ? list : []),
|
||||
});
|
||||
|
||||
const updatePendingUserData = (newData) => {
|
||||
setPendingUserData((prevData) => ({
|
||||
...prevData,
|
||||
@@ -144,6 +178,8 @@ export default ({ children }) => {
|
||||
currentUserRoles,
|
||||
currentUID,
|
||||
currentUserData,
|
||||
userProjects,
|
||||
userLikedProjects,
|
||||
|
||||
setCurrentUserData,
|
||||
|
||||
@@ -163,3 +199,8 @@ export default ({ children }) => {
|
||||
export const useUserData = () => {
|
||||
return useContext(UserDataContext);
|
||||
};
|
||||
|
||||
// Convenience alias returning the same context, as requested
|
||||
export const useUser = () => {
|
||||
return useContext(UserDataContext);
|
||||
};
|
||||
|
||||
@@ -2,7 +2,6 @@ import React from "react";
|
||||
import { MinuitProvider } from "react-native-minuit";
|
||||
|
||||
import SharedProviders from "./SharedProviders";
|
||||
import NotificationProvider from "./NotificationProvider";
|
||||
|
||||
import { Palette } from "../styles";
|
||||
|
||||
@@ -16,9 +15,7 @@ export default ({ children }) => {
|
||||
destructive: "red",
|
||||
}}
|
||||
>
|
||||
<SharedProviders>
|
||||
<NotificationProvider>{children}</NotificationProvider>
|
||||
</SharedProviders>
|
||||
<SharedProviders>{children}</SharedProviders>
|
||||
</MinuitProvider>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user