diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/(tabs)/alerts/[id].tsx | 2 | ||||
| -rw-r--r-- | app/(tabs)/alerts/index.tsx | 28 | ||||
| -rw-r--r-- | app/(tabs)/index.tsx | 6 | 
3 files changed, 24 insertions, 12 deletions
diff --git a/app/(tabs)/alerts/[id].tsx b/app/(tabs)/alerts/[id].tsx index 9dba92a..92856b3 100644 --- a/app/(tabs)/alerts/[id].tsx +++ b/app/(tabs)/alerts/[id].tsx @@ -158,7 +158,7 @@ export default function AlertIdScreen() {    const theme = useColorScheme() ?? 'light';    return ( -    <ParallaxScrollView> +    <ParallaxScrollView token={token} userId={userId}>        {alert === null ? (          <ThemedView>            <ThemedText>Loading...</ThemedText> diff --git a/app/(tabs)/alerts/index.tsx b/app/(tabs)/alerts/index.tsx index b4aa18d..9ba2cce 100644 --- a/app/(tabs)/alerts/index.tsx +++ b/app/(tabs)/alerts/index.tsx @@ -29,8 +29,8 @@ export default function AlertsScreen() {    const [alerts, setAlerts] = useState<AlertData[]>([]);    const [refreshing, setRefreshing] = useState(false); -  const fetchAlerts = async () => { -    if (!token || !userId) return; +  const fetchAlerts = async (currentToken: string, currentUserId: string) => { +    if (!currentToken || !currentUserId) return;      try {        const response = await fetch( @@ -38,7 +38,7 @@ export default function AlertsScreen() {          {            method: 'POST',            headers: { -            Authorization: `Bearer ${token}`, +            Authorization: `Bearer ${currentToken}`,              'Content-Type': 'application/json',            },            body: JSON.stringify({ @@ -52,7 +52,6 @@ export default function AlertsScreen() {        if (data.errors) {          Alert.alert('Error', 'Error fetching data');        } else if (data.data.alerts) { -        console.log(`Found ${data.data.alerts.length} alerts`);          setAlerts(data.data.alerts);        }      } catch (err) { @@ -74,6 +73,7 @@ export default function AlertsScreen() {      setUserId(storedUserId || '');      if (!storedToken || !storedUserId) { +      setAlerts([]);        Alert.alert(          'Login required',          'You must log in to the system if you want to see alerts list', @@ -85,19 +85,28 @@ export default function AlertsScreen() {          ]        );      } + +    // Fetch alerts only after token and userId are set +    return { storedToken, storedUserId };    };    useFocusEffect(      useCallback(() => { -      checkAuth(); -      fetchAlerts(); +      const init = async () => { +        const { storedToken, storedUserId } = await checkAuth(); +        if (storedToken && storedUserId) { +          fetchAlerts(storedToken, storedUserId); +        } +      }; + +      init();      }, [])    );    const onRefresh = useCallback(() => {      setRefreshing(true); -    fetchAlerts().finally(() => setRefreshing(false)); -  }, []); +    fetchAlerts(token, userId).finally(() => setRefreshing(false)); +  }, [token, userId]);    const formatDate = (timestamp: string) => {      const date = new Date(parseInt(timestamp) * 1000); @@ -141,7 +150,7 @@ export default function AlertsScreen() {    return (      <FlatList        ListHeaderComponent={ -        <ParallaxScrollView> +        <ParallaxScrollView token={token} userId={userId}>            <ThemedView style={styles.header}>              <ThemedText type="subtitle">Alerts</ThemedText>              <ThemedText type="default"> @@ -175,6 +184,7 @@ const styles = StyleSheet.create({    },    alertBox: {      padding: 16, +    paddingBottom: 14,      borderRadius: 8,      shadowColor: '#000',      shadowOffset: { width: 0, height: 2 }, diff --git a/app/(tabs)/index.tsx b/app/(tabs)/index.tsx index 94e85d6..3303b28 100644 --- a/app/(tabs)/index.tsx +++ b/app/(tabs)/index.tsx @@ -95,6 +95,7 @@ export default function HomeScreen() {      }      try { +      console.log(`${process.env.EXPO_PUBLIC_API_URL}/graphql`)        const response = await fetch(`${process.env.EXPO_PUBLIC_API_URL}/graphql`, {          method: 'POST',          headers: { @@ -170,7 +171,6 @@ export default function HomeScreen() {    const handleLogout = async () => {      await removeToken(); -    setToken('');    };    const removeToken = async () => { @@ -181,6 +181,8 @@ export default function HomeScreen() {        await AsyncStorage.removeItem('token');        await AsyncStorage.removeItem('userId');      } +    setToken(''); +    setUserId('');    };    const fetchMapData = async () => { @@ -241,7 +243,7 @@ export default function HomeScreen() {    }, [region]);    return ( -    <ParallaxScrollView> +    <ParallaxScrollView token={token} userId={userId}>        {token && userId ? (          <>            <ThemedView>  |