import { Image, StyleSheet, Button, ActivityIndicator, TextInput, Modal, Pressable, View } from 'react-native'; import ParallaxScrollView from '@/components/ParallaxScrollView'; import { ThemedText } from '@/components/ThemedText'; import { ThemedView } from '@/components/ThemedView'; import { useEffect, useState } from 'react'; import NetInfo from '@react-native-community/netinfo'; interface Message { title: string; type: string; additionally: { payload: string; }; } interface GroupData { groupName: string; users: string[]; messages: Message[]; } export default function GroupsScreen() { const [groups, setGroups] = useState([]); const [isSyncing, setIsSyncing] = useState(false); const [isModalVisible, setIsModalVisible] = useState(false); const [newGroupName, setNewGroupName] = useState(""); const fetchGroups = async () => { try { setIsSyncing(true); const response = await fetch("http://127.0.0.1:8099/getAllGroups"); const data: GroupData[] = await response.json(); setGroups(data); } catch (error) { console.log('Fetch error:', error); } finally { setIsSyncing(false); } }; const createGroup = async () => { if (!newGroupName.trim()) return; try { const response = await fetch(`http://127.0.0.1:8099/create_group?group_name=` + newGroupName + `&username=` + "moxitech", { method: "GET", headers: { 'Content-Type': 'application/json', }, }); if (response.ok) { await fetchGroups(); // Обновляем список групп после создания новой setIsModalVisible(false); // Закрываем модальное окно setNewGroupName(""); // Сбрасываем имя новой группы } } catch (error) { console.log('Create group error:', error); } }; useEffect(() => { const syncWithServer = async () => { await fetchGroups(); }; const unsubscribe = NetInfo.addEventListener(state => { if (state.isConnected) { syncWithServer(); } }); // Первоначальная загрузка данных syncWithServer(); return () => { unsubscribe(); }; }, []); return ( }>