|
|
@@ -18,7 +18,7 @@ import { useEffect, useState } from 'react';
|
|
|
import { setupInterceptors } from 'src/utils/request';
|
|
|
import { ErrorModal, WarningModal } from 'src/components';
|
|
|
import React from 'react';
|
|
|
-import { Linking, Platform } from 'react-native';
|
|
|
+import { AppState, Linking, Platform } from 'react-native';
|
|
|
import { API_HOST, API_URL, APP_VERSION } from 'src/constants';
|
|
|
import axios from 'axios';
|
|
|
import { API } from 'src/types';
|
|
|
@@ -29,6 +29,13 @@ import {
|
|
|
startAutoSyncListener,
|
|
|
stopAutoSyncListener
|
|
|
} from 'src/watermelondb/features/chat/networkSync';
|
|
|
+import { fetchActivePoll, PostGetActivePoll } from '@api/app';
|
|
|
+
|
|
|
+const POLL_LAST_SHOWN_KEY = 'poll_popup_last_shown';
|
|
|
+const getTodayKey = () => {
|
|
|
+ const d = new Date();
|
|
|
+ return `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`;
|
|
|
+};
|
|
|
|
|
|
const IOS_STORE_URL = 'https://apps.apple.com/app/id6502843543';
|
|
|
const ANDROID_STORE_URL =
|
|
|
@@ -136,6 +143,7 @@ const InnerApp = () => {
|
|
|
const navigation = React.useRef<any>(null);
|
|
|
const [isUpdateAvailable, setIsUpdateAvailable] = useState(false);
|
|
|
const [latestVersion, setLatestVersion] = useState<string | null>(null);
|
|
|
+ const [activePoll, setActivePoll] = useState<PostGetActivePoll | null>(null);
|
|
|
|
|
|
useEffect(() => {
|
|
|
setupGlobalErrorHandler(navigation);
|
|
|
@@ -183,6 +191,30 @@ const InnerApp = () => {
|
|
|
return () => stopAutoSyncListener();
|
|
|
}, [token]);
|
|
|
|
|
|
+ useEffect(() => {
|
|
|
+ const checkActivePoll = async () => {
|
|
|
+ if (storage.get(POLL_LAST_SHOWN_KEY, StoreType.STRING) === getTodayKey()) return;
|
|
|
+
|
|
|
+ const data = await fetchActivePoll(token);
|
|
|
+ if (data?.result === 'OK' && data.active === 1 && data.url) {
|
|
|
+ storage.set(POLL_LAST_SHOWN_KEY, getTodayKey());
|
|
|
+ setActivePoll(data);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ checkActivePoll();
|
|
|
+
|
|
|
+ const subscription = AppState.addEventListener('change', (state) => {
|
|
|
+ if (state === 'active') checkActivePoll();
|
|
|
+ });
|
|
|
+ return () => subscription.remove();
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ const handlePollPress = () => {
|
|
|
+ if (!activePoll?.url) return;
|
|
|
+ Linking.openURL(activePoll.url).catch((err) => console.error('Failed to open poll URL:', err));
|
|
|
+ };
|
|
|
+
|
|
|
const handleUpdatePress = () => {
|
|
|
const storeUrl = Platform.OS === 'ios' ? IOS_STORE_URL : ANDROID_STORE_URL;
|
|
|
Linking.openURL(storeUrl).catch((err) => console.error('Failed to open store URL:', err));
|
|
|
@@ -215,6 +247,18 @@ const InnerApp = () => {
|
|
|
setIsUpdateAvailable(false);
|
|
|
}}
|
|
|
/>
|
|
|
+ <WarningModal
|
|
|
+ isVisible={!!activePoll && !isUpdateAvailable}
|
|
|
+ type="poll"
|
|
|
+ title={activePoll?.title || 'We need your opinion!'}
|
|
|
+ message={
|
|
|
+ activePoll?.message ||
|
|
|
+ 'A new NomadMania poll is live. It only takes a minute - tell us what you think!'
|
|
|
+ }
|
|
|
+ buttonTitle={activePoll?.button_text || undefined}
|
|
|
+ action={handlePollPress}
|
|
|
+ onClose={() => setActivePoll(null)}
|
|
|
+ />
|
|
|
</NavigationContainer>
|
|
|
</RegionProvider>
|
|
|
</ConnectionProvider>
|