diff --git a/client/App.js b/client/App.js index a0b1fad..6a497f9 100644 --- a/client/App.js +++ b/client/App.js @@ -7,6 +7,7 @@ import WeatherScreen from './Weather'; import FindRouteScreen from './FindRoute'; import DisplayRouteScreen from './DisplayRoute'; import Dashboard from './SustainabilityDashboard' +import FriendsScreen from './FriendsScreen' const Stack = createNativeStackNavigator(); @@ -20,6 +21,7 @@ export default function App() { + ); diff --git a/client/FriendsScreen.js b/client/FriendsScreen.js new file mode 100644 index 0000000..a651d02 --- /dev/null +++ b/client/FriendsScreen.js @@ -0,0 +1,391 @@ +import React, { useState, useEffect } from 'react'; +import { View, Text, TextInput, Button, FlatList, TouchableOpacity, StyleSheet, Platform } from 'react-native'; +import { GestureHandlerRootView } from 'react-native-gesture-handler'; + +export default function FriendsScreen() { + const [senderName, setSenderName] = useState('') + const [friendRequestName, setFriendName] = useState(''); + const [pendingFriends, setPendingFriends] = useState([]); + const [currentFriends, setCurrentFriends] = useState([]); + const [sentFriends, setSentFriends] = useState([]); + + let sender_name = "Conor"; //Username of the person sending the request + + // friend_list + // pending_friends + + // Function to send a friend request + const sendFriendRequest = async () => { + try{ + // send friend request name & username of the person sending friend request + const payload = { + receiver: friendRequestName, + sender: sender_name, // username of the person sending friend request + }; + + const baseUrl = Platform.OS === 'web' + ? 'http://localhost:8000' + : process.env.EXPO_PUBLIC_API_URL; + console.log(`Sending request to ${baseUrl}/send_request`); + + const response = await fetch(`${baseUrl}/send_request`,{ + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(payload), + }); + + // Check if the response is ok + if (response.ok) { + // Parse the response as JSON + const server_message = await response.json(); + console.log("Response from Server: ", server_message.message); + + alert(server_message.message); + setSentFriends([...sentFriends, friendRequestName.trim()]); + setFriendName(''); + } else { + // Log the raw response text for debugging + const responseText = await response.text(); + console.error('Failed to send friend request:', responseText); + alert('Server Error: ', responseText); + } + } catch (error) { + console.error('Error sending friend request:', error); + } + }; + + const Poll = async () => { + try{ + // send friend request name & username of the person sending friend request + //const payload = { + // sender: "Conor", // username of the person sending friend request + //}; + + const baseUrl = Platform.OS === 'web' + ? 'http://localhost:8000' + : process.env.EXPO_PUBLIC_API_URL; + console.log(`Sending request to ${baseUrl}/check_requests?sender=${sender_name}`); + + const response = await fetch(`${baseUrl}/check_requests?sender=${sender_name}`,{ + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + // Check if the response is ok + if (response.ok) { + // Parse the response as JSON + const server_message = await response.json(); + console.log("Response from Server: ", server_message.message); + + // alert(server_message.message); + setPendingFriends(server_message.pending_friends); + setCurrentFriends(server_message.friends); + setSentFriends(server_message.sent_friends); + } else { + // Log the raw response text for debugging + const responseText = await response.text(); + console.error('Failed to get friend requests:', responseText); + alert('Server Error: ', responseText); + } + } catch (error) { + console.error('Error getting pending friend requests:', error); + } + }; + + // Poll for friend requests every 5 seconds + useEffect(() => { + const intervalId = setInterval(() => { + Poll(); + }, 5000); // Poll every 5 seconds + + // Cleanup function to clear the interval when the component unmounts + return () => clearInterval(intervalId); +}, []); + + + // Function to accept a friend request + const processFriendRequest = async (friend, answer) => { + // setCurrentFriends([...currentFriends, friend]); + + try{ + // send friend request name & username of the person sending friend request + const payload = { + requester: friend, + user: sender_name, + answer: answer + }; + + const baseUrl = Platform.OS === 'web' + ? 'http://localhost:8000' + : process.env.EXPO_PUBLIC_API_URL; + console.log(`Sending request to ${baseUrl}/request_response`); + + const response = await fetch(`${baseUrl}/request_response`,{ + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(payload), + }); + + // Check if the response is ok + if (response.ok) { + // Parse the response as JSON + const server_message = await response.json(); + console.log("Response from Server: ", server_message.message); + + alert(server_message.message); + setPendingFriends(pendingFriends.filter((name) => name !== friend)); + setCurrentFriends(currentFriends.filter((name) => name !== friend)); + } else { + // Log the raw response text for debugging + const responseText = await response.text(); + console.error('Failed to accept friend request:', responseText); + alert('Server Error: ', responseText); + } + } catch (error) { + console.error('Error accepting friend request:', error); + } + }; + + + // Function to cancel a sent friend request + const cancelFriendRequest = async (friend) => { + + try{ + // send friend request name & username of the person sending friend request + const payload = { + user: sender_name, // username of the person removing friend + friend: friend // friend to be removed + }; + + const baseUrl = Platform.OS === 'web' + ? 'http://localhost:8000' + : process.env.EXPO_PUBLIC_API_URL; + console.log(`Sending request to ${baseUrl}/cancel_friend_request`); + + const response = await fetch(`${baseUrl}/cancel_friend_request`,{ + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(payload), + }); + + // Check if the response is ok + if (response.ok) { + // Parse the response as JSON + const server_message = await response.json(); + console.log("Response from Server: ", server_message.message); + + alert(server_message.message); + setSentFriends(sentFriends.filter((name) => name !== friend)); + } else { + // Log the raw response text for debugging + const responseText = await response.text(); + console.error('Failed to send friend request:', responseText); + alert('Server Error: ', responseText); + } + } catch (error) { + console.error('Error sending friend request:', error); + } + + }; + + // Function to remove an existing friend + const removeFriend = async (friend) => { + try{ + // send friend request name & username of the person sending friend request + const payload = { + user: sender_name, // username of the person removing friend + friend: friend // friend to be removed + }; + + const baseUrl = Platform.OS === 'web' + ? 'http://localhost:8000' + : process.env.EXPO_PUBLIC_API_URL; + console.log(`Sending request to ${baseUrl}/remove_friend`); + + const response = await fetch(`${baseUrl}/remove_friend`,{ + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(payload), + }); + + // Check if the response is ok + if (response.ok) { + // Parse the response as JSON + const server_message = await response.json(); + console.log("Response from Server: ", server_message.message); + + alert(server_message.message); + setCurrentFriends(currentFriends.filter((name) => name !== friend)); + } else { + // Log the raw response text for debugging + const responseText = await response.text(); + console.error('Failed to send friend request:', responseText); + alert('Server Error: ', responseText); + } + } catch (error) { + console.error('Error sending friend request:', error); + } + + }; + + return ( + + + {/* Top Section: Input & Button */} + + +