import { useMutation } from "@apollo/react-hooks"; import AsyncStorage from "@react-native-async-storage/async-storage"; import { useNavigation, useTheme } from "@react-navigation/native"; import * as Device from "expo-device"; import * as Localization from "expo-localization"; import * as Notifications from "expo-notifications"; import gql from "graphql-tag"; import React, { useContext, useEffect, useRef, useState } from "react"; import { AppState, Linking, Platform, StyleSheet, TouchableOpacity, View, } from "react-native"; import { Modalize } from "react-native-modalize"; import i18n from "../../../i18n"; import { moderateScale } from "../../utils/scaling"; import { updateUser, profile, pushToken, updateNotificationStatus, } from "../../apollo/server"; import { CustomIcon, FlashMessage, Spinner, TextDefault, WrapperView, } from "../../components"; import SwitchBtn from "../../components/FdSwitch/SwitchBtn"; import UserContext from "../../context/User"; import { alignment } from "../../utils/alignment"; import { ICONS_NAME } from "../../utils/constant"; import { scale } from "../../utils/scaling"; import SettingModal from "./components/SettingModal"; import useStyle from "./styles"; const languageTypes = [ { value: "English", code: "en", index: 0 }, { value: "français", code: "fr", index: 1 }, { value: "ភាសាខ្មែរ", code: "km", index: 2 }, { value: "中文", code: "zh", index: 3 }, { value: "Deutsche", code: "de", index: 4 }, { value: "Arabic", code: "ar", index: 5 }, ]; const PUSH_TOKEN = gql` ${pushToken} `; const UPDATE_NOTIFICATION_TOKEN = gql` ${updateNotificationStatus} `; const PROFILE = gql` ${profile} `; const UPDATEUSER = gql` ${updateUser} `; function Settings() { const styles = useStyle(); const { colors } = useTheme(); const navigation = useNavigation(); const { profile, logout } = useContext(UserContext); const [languageName, languageNameSetter] = useState("English"); const [offerNotification, offerNotificationSetter] = useState( profile.is_offer_notification ); const [orderNotification, orderNotificationSetter] = useState( profile.is_order_notification ); const [activeRadio, activeRadioSetter] = useState(languageTypes[0].index); const [appState, setAppState] = useState(AppState.currentState); const [uploadToken] = useMutation(PUSH_TOKEN); const [updateUserInfo] = useMutation(UPDATEUSER); const [mutate, { loading }] = useMutation(UPDATE_NOTIFICATION_TOKEN, { onCompleted, onError, refetchQueries: [{ query: PROFILE }], }); const modalizeRef = useRef(null); const modalizeRef1 = useRef(null); useEffect(() => { navigation.setOptions({ headerTitle: i18n.t("titleSettings"), headerRight: null, }); selectLanguage(); checkPermission(); }, [navigation]); const _handleAppStateChange = async (nextAppState) => { if (nextAppState === "active") { let token = null; const permission = await getPermission(); if (permission === "granted") { if (!profile.notificationToken) { token = (await Notifications.getExpoPushTokenAsync()).data; uploadToken({ variables: { token } }); } offerNotificationSetter(profile.is_offer_notification); orderNotificationSetter(profile.is_order_notification); } else { offerNotificationSetter(false); orderNotificationSetter(false); } } setAppState(nextAppState); }; useEffect(() => { const subscription = AppState.addEventListener( "change", _handleAppStateChange ); return () => { subscription.remove("change", _handleAppStateChange); }; }, []); async function checkPermission() { const permission = await getPermission(); if (permission !== "granted") { offerNotificationSetter(false); orderNotificationSetter(false); } else { offerNotificationSetter(profile.is_offer_notification); orderNotificationSetter(profile.is_order_notification); } } async function getPermission() { const { status } = await Notifications.getPermissionsAsync(); return status; } async function selectLanguage() { const lang = await AsyncStorage.getItem("enatega-language"); if (lang) { const defLang = languageTypes.findIndex((el) => el.code === lang); const langName = languageTypes[defLang].value; activeRadioSetter(defLang); languageNameSetter(langName); } } const onSelectedLanguage = async (active) => { const languageInd = active; if (Platform.OS === "android") { const localization = await Localization.getLocalizationAsync(); localization.locale = languageTypes[languageInd].code; await AsyncStorage.setItem( "enatega-language", languageTypes[languageInd].code ); i18n.locale = languageTypes[languageInd].code; const lang = await AsyncStorage.getItem("enatega-language"); console.log(lang); const defLang = languageTypes.findIndex((el) => el.code === lang); const langName = languageTypes[defLang].value; activeRadioSetter(defLang); languageNameSetter(langName); } }; const onClose = () => { modalizeRef.current.close(); }; function onCompleted() { FlashMessage({ message: i18n.t("notificationUpdate"), }); } function onError(error) { try { FlashMessage({ message: error.networkError.result.errors[0].message, }); } catch (err) {} } async function updateNotificationStatus(notificationCheck) { let orderNotify, offerNotify; if (!Device.isDevice) { FlashMessage({ message: "Notification do not work on simulator", }); return; } const permission = await getPermission(); if (!profile.notificationToken || permission !== "granted") { Linking.openSettings(); } if (notificationCheck === "offer") { offerNotificationSetter(!offerNotification); orderNotify = orderNotification; offerNotify = !offerNotification; } if (notificationCheck === "order") { orderNotificationSetter(!orderNotification); orderNotify = !orderNotification; offerNotify = offerNotification; } mutate({ variables: { offerNotification: offerNotify, orderNotification: orderNotify, }, }); } async function updateUserInformation() { console.log("profile data", profile); updateUserInfo({ variables: { name: profile.name, phone: profile.phone, is_active: false, }, }); } return ( {loading && ( )} {Platform.OS === "android" && ( {i18n.t("language")} ({languageName}) modalizeRef.current.open("top")} style={styles.button} > )} { updateNotificationStatus("offer"); }} style={[styles.notificationContainer, styles.shadow]} > {" "} {i18n.t("receiveOffers")}{" "} { updateNotificationStatus("offer"); }} /> { updateNotificationStatus("order"); }} style={[styles.notificationContainer, styles.shadow]} > {" "} {i18n.t("getUpdatesText")}{" "} { updateNotificationStatus("order"); }} /> modalizeRef1.current.open("top")} style={[styles.notificationContainer, styles.shadow]} > {" "} {i18n.t("delAcc")}{" "} {i18n.t("rightsReserved")} {/* Modal for language Changes */} {/* Modal for Delete Account */} {i18n.t("delAccText")} { await updateUserInformation(); logout(); navigation.reset({ routes: [{ name: "Menu" }], }); }} > Delete Account onClose()} > {i18n.t("cancel")} ); } export default Settings;