|
@@ -1,7 +1,6 @@
|
|
import React, { useState } from 'react';
|
|
import React, { useState } from 'react';
|
|
-import { View, Image, Linking, Text, Switch, Platform } from 'react-native';
|
|
|
|
|
|
+import { View, Image, Linking, Text } from 'react-native';
|
|
import { CommonActions, useNavigation } from '@react-navigation/native';
|
|
import { CommonActions, useNavigation } from '@react-navigation/native';
|
|
-import * as Notifications from 'expo-notifications';
|
|
|
|
|
|
|
|
import { WarningModal } from '../WarningModal';
|
|
import { WarningModal } from '../WarningModal';
|
|
import { MenuButton } from '../MenuButton';
|
|
import { MenuButton } from '../MenuButton';
|
|
@@ -16,11 +15,10 @@ import DocumentIcon from '../../../assets/icons/document.svg';
|
|
import ExitIcon from '../../../assets/icons/exit.svg';
|
|
import ExitIcon from '../../../assets/icons/exit.svg';
|
|
import UserXMark from '../../../assets/icons/user-xmark.svg';
|
|
import UserXMark from '../../../assets/icons/user-xmark.svg';
|
|
import InfoIcon from 'assets/icons/info-solid.svg';
|
|
import InfoIcon from 'assets/icons/info-solid.svg';
|
|
|
|
+import BellIcon from 'assets/icons/notifications/bell-solid.svg';
|
|
|
|
|
|
import { APP_VERSION, FASTEST_MAP_HOST } from 'src/constants';
|
|
import { APP_VERSION, FASTEST_MAP_HOST } from 'src/constants';
|
|
import { useNotification } from 'src/contexts/NotificationContext';
|
|
import { useNotification } from 'src/contexts/NotificationContext';
|
|
-import { usePostSaveNotificationTokenMutation } from '@api/user';
|
|
|
|
-import { usePushNotification } from 'src/contexts/PushNotificationContext';
|
|
|
|
|
|
|
|
export const MenuDrawer = (props: any) => {
|
|
export const MenuDrawer = (props: any) => {
|
|
const { mutate: deleteUser } = useDeleteUserMutation();
|
|
const { mutate: deleteUser } = useDeleteUserMutation();
|
|
@@ -33,9 +31,6 @@ export const MenuDrawer = (props: any) => {
|
|
action: () => {}
|
|
action: () => {}
|
|
});
|
|
});
|
|
const { updateNotificationStatus } = useNotification();
|
|
const { updateNotificationStatus } = useNotification();
|
|
- const { mutateAsync: saveNotificationToken } = usePostSaveNotificationTokenMutation();
|
|
|
|
- const [shouldOpenWarningModal, setShouldOpenWarningModal] = useState(false);
|
|
|
|
- const { isSubscribed, toggleSubscription } = usePushNotification();
|
|
|
|
|
|
|
|
const openModal = (type: string, message: string, action: any) => {
|
|
const openModal = (type: string, message: string, action: any) => {
|
|
setModalInfo({
|
|
setModalInfo({
|
|
@@ -69,72 +64,6 @@ export const MenuDrawer = (props: any) => {
|
|
deleteUser({ token }, { onSuccess: handleLogout });
|
|
deleteUser({ token }, { onSuccess: handleLogout });
|
|
};
|
|
};
|
|
|
|
|
|
- const handleSubscribe = async () => {
|
|
|
|
- const deviceData = await registerForPushNotificationsAsync();
|
|
|
|
-
|
|
|
|
- if (deviceData?.notificationToken) {
|
|
|
|
- toggleSubscription();
|
|
|
|
- await saveNotificationToken({
|
|
|
|
- token,
|
|
|
|
- platform: deviceData.platform,
|
|
|
|
- n_token: deviceData.notificationToken
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- const toggleSwitch = async () => {
|
|
|
|
- if (isSubscribed) {
|
|
|
|
- toggleSubscription();
|
|
|
|
- } else {
|
|
|
|
- const { status } = await Notifications.getPermissionsAsync();
|
|
|
|
- if (status !== 'granted') {
|
|
|
|
- setModalInfo({
|
|
|
|
- visible: true,
|
|
|
|
- type: 'success',
|
|
|
|
- message:
|
|
|
|
- 'To use this feature we need your permission to access your notifications. If you press OK your system will ask you to confirm permission to receive notifications from NomadMania.',
|
|
|
|
- action: () => setShouldOpenWarningModal(true)
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- handleSubscribe();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- async function registerForPushNotificationsAsync() {
|
|
|
|
- const { status: existingStatus } = await Notifications.getPermissionsAsync();
|
|
|
|
- let finalStatus = existingStatus;
|
|
|
|
- if (existingStatus !== 'granted') {
|
|
|
|
- const { status } = await Notifications.requestPermissionsAsync();
|
|
|
|
- finalStatus = status;
|
|
|
|
- }
|
|
|
|
- if (finalStatus !== 'granted') {
|
|
|
|
- setModalInfo({
|
|
|
|
- visible: true,
|
|
|
|
- type: 'success',
|
|
|
|
- message:
|
|
|
|
- 'NomadMania app needs notification permissions to function properly. Open settings?',
|
|
|
|
- action: () =>
|
|
|
|
- Platform.OS === 'ios' ? Linking.openURL('app-settings:') : Linking.openSettings()
|
|
|
|
- });
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- const deviceData = await Notifications.getDevicePushTokenAsync();
|
|
|
|
- console.log('deviceData', deviceData);
|
|
|
|
-
|
|
|
|
- if (Platform.OS === 'android') {
|
|
|
|
- Notifications.setNotificationChannelAsync('default', {
|
|
|
|
- name: 'default',
|
|
|
|
- importance: Notifications.AndroidImportance.MAX,
|
|
|
|
- vibrationPattern: [0, 250, 250, 250],
|
|
|
|
- lightColor: '#FF231F7C'
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- storage.set('deviceToken', deviceData.data);
|
|
|
|
-
|
|
|
|
- return { notificationToken: deviceData.data ?? '', platform: deviceData.type ?? '' };
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
return (
|
|
return (
|
|
<>
|
|
<>
|
|
<View style={styles.container}>
|
|
<View style={styles.container}>
|
|
@@ -160,26 +89,19 @@ export const MenuDrawer = (props: any) => {
|
|
red={false}
|
|
red={false}
|
|
buttonFn={() => Linking.openURL('https://nomadmania.com/terms/')}
|
|
buttonFn={() => Linking.openURL('https://nomadmania.com/terms/')}
|
|
/>
|
|
/>
|
|
- <View
|
|
|
|
- style={{
|
|
|
|
- display: 'flex',
|
|
|
|
- flexDirection: 'row',
|
|
|
|
- justifyContent: 'space-between',
|
|
|
|
- marginTop: 20,
|
|
|
|
- alignItems: 'center'
|
|
|
|
- }}
|
|
|
|
- >
|
|
|
|
- <Text style={{ color: Colors.DARK_BLUE, fontSize: 16, fontWeight: 'bold' }}>
|
|
|
|
- Notifications
|
|
|
|
- </Text>
|
|
|
|
- <Switch
|
|
|
|
- trackColor={{ false: Colors.LIGHT_GRAY, true: Colors.DARK_BLUE }}
|
|
|
|
- thumbColor={Colors.WHITE}
|
|
|
|
- onValueChange={toggleSwitch}
|
|
|
|
- value={isSubscribed}
|
|
|
|
- style={{ transform: 'scale(0.8)' }}
|
|
|
|
|
|
+ {token && (
|
|
|
|
+ <MenuButton
|
|
|
|
+ label="Notifications"
|
|
|
|
+ icon={<BellIcon fill={Colors.DARK_BLUE} width={20} height={20} />}
|
|
|
|
+ red={false}
|
|
|
|
+ buttonFn={() =>
|
|
|
|
+ // todo: add types
|
|
|
|
+ navigation.navigate(NAVIGATION_PAGES.MENU_DRAWER, {
|
|
|
|
+ screen: NAVIGATION_PAGES.NOTIFICATIONS
|
|
|
|
+ })
|
|
|
|
+ }
|
|
/>
|
|
/>
|
|
- </View>
|
|
|
|
|
|
+ )}
|
|
</View>
|
|
</View>
|
|
|
|
|
|
<View style={styles.bottomMenu}>
|
|
<View style={styles.bottomMenu}>
|
|
@@ -227,12 +149,6 @@ export const MenuDrawer = (props: any) => {
|
|
modalInfo.action();
|
|
modalInfo.action();
|
|
closeModal();
|
|
closeModal();
|
|
}}
|
|
}}
|
|
- onModalHide={() => {
|
|
|
|
- if (shouldOpenWarningModal) {
|
|
|
|
- setShouldOpenWarningModal(false);
|
|
|
|
- handleSubscribe();
|
|
|
|
- }
|
|
|
|
- }}
|
|
|
|
/>
|
|
/>
|
|
</>
|
|
</>
|
|
);
|
|
);
|