2024-09-10 11:03:30 -05:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2024-09-10 13:30:33 -05:00
|
|
|
import {
|
|
|
|
StyleSheet,
|
|
|
|
TextInput,
|
|
|
|
TouchableOpacity,
|
|
|
|
Alert,
|
|
|
|
Keyboard,
|
|
|
|
TouchableWithoutFeedback,
|
|
|
|
KeyboardAvoidingView,
|
2024-09-25 13:16:33 -05:00
|
|
|
Platform,
|
|
|
|
SafeAreaView, // SafeAreaView import for handling safe areas
|
|
|
|
View
|
2024-09-10 13:30:33 -05:00
|
|
|
} from 'react-native';
|
2024-09-09 22:57:42 -05:00
|
|
|
import { ThemedText } from '@/components/ThemedText';
|
2024-09-10 15:33:34 -05:00
|
|
|
import { LinearGradient } from 'expo-linear-gradient';
|
2024-09-10 11:03:30 -05:00
|
|
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
|
|
import axios from 'axios';
|
|
|
|
|
2024-09-25 13:16:33 -05:00
|
|
|
// Environment variables
|
2024-09-10 13:30:33 -05:00
|
|
|
const API_KEY = process.env.EXPO_PUBLIC_API_KEY;
|
|
|
|
const BASE_URL = process.env.EXPO_PUBLIC_BASE_URL;
|
2024-09-10 11:03:30 -05:00
|
|
|
|
|
|
|
export default function SendMessageScreen() {
|
|
|
|
const [message, setMessage] = useState('');
|
2024-09-25 13:16:33 -05:00
|
|
|
const [userId, setUserId] = useState<number | null>(null); // Use number type for userId
|
|
|
|
const [inputHeight, setInputHeight] = useState(120); // Set initial height for TextInput
|
2024-09-10 16:57:14 -05:00
|
|
|
|
2024-09-25 13:16:33 -05:00
|
|
|
// Update TextInput height dynamically
|
2024-09-10 16:57:14 -05:00
|
|
|
const updateInputHeight = (height: number) => {
|
2024-09-25 13:16:33 -05:00
|
|
|
setInputHeight(Math.max(120, height)); // Ensure it doesn't shrink below 120px
|
2024-09-10 16:57:14 -05:00
|
|
|
};
|
2024-09-10 11:03:30 -05:00
|
|
|
|
2024-09-25 13:16:33 -05:00
|
|
|
// On component mount, get the user ID from AsyncStorage
|
2024-09-10 11:03:30 -05:00
|
|
|
useEffect(() => {
|
2024-09-25 13:16:33 -05:00
|
|
|
const getUserId = async () => {
|
|
|
|
try {
|
|
|
|
const storedUser = await AsyncStorage.getItem('@user');
|
|
|
|
if (storedUser) {
|
|
|
|
const user = JSON.parse(storedUser);
|
|
|
|
setUserId(user.id);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to get user ID:', error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-10 11:03:30 -05:00
|
|
|
getUserId();
|
|
|
|
}, []);
|
|
|
|
|
2024-09-25 13:16:33 -05:00
|
|
|
// Function to send a message
|
2024-09-10 11:03:30 -05:00
|
|
|
const sendMessage = async () => {
|
2024-09-25 13:16:33 -05:00
|
|
|
// Input validation
|
2024-09-10 11:03:30 -05:00
|
|
|
if (!message.trim()) {
|
|
|
|
Alert.alert('Error', 'Please enter a message');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!userId) {
|
|
|
|
Alert.alert('Error', 'User not found. Please select a user first.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await axios.post(`${BASE_URL}/setMessage`, null, {
|
|
|
|
params: { apiKey: API_KEY, userId, message }
|
|
|
|
});
|
|
|
|
Alert.alert('Success', 'Message sent successfully');
|
2024-09-25 13:16:33 -05:00
|
|
|
setMessage(''); // Clear the message input on success
|
|
|
|
Keyboard.dismiss(); // Dismiss the keyboard
|
2024-09-10 11:03:30 -05:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to send message:', error);
|
|
|
|
Alert.alert('Error', 'Failed to send message. Please try again.');
|
|
|
|
}
|
|
|
|
};
|
2024-09-09 22:57:42 -05:00
|
|
|
|
|
|
|
return (
|
2024-09-25 13:16:33 -05:00
|
|
|
// Ensure keyboard behaves correctly on both iOS and Android
|
2024-09-10 13:30:33 -05:00
|
|
|
<KeyboardAvoidingView
|
2024-09-25 13:16:33 -05:00
|
|
|
behavior={Platform.OS === "ios" ? "padding" : "height"}
|
|
|
|
style={{ flex: 1 }}
|
2024-09-10 13:30:33 -05:00
|
|
|
>
|
2024-09-25 13:16:33 -05:00
|
|
|
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
|
|
|
|
{/* SafeAreaView for notch and layout safety */}
|
|
|
|
<SafeAreaView style={styles.safeContainer}>
|
|
|
|
<LinearGradient
|
|
|
|
colors={['#F67C0A', '#F60AD3']}
|
|
|
|
style={styles.container}
|
|
|
|
start={{ x: 0, y: 0 }}
|
|
|
|
end={{ x: 1, y: 1 }}
|
|
|
|
>
|
|
|
|
<View style={styles.chatContainer}>
|
|
|
|
<TextInput
|
|
|
|
style={[styles.input, { height: inputHeight }]} // Dynamic height
|
|
|
|
value={message}
|
|
|
|
onChangeText={setMessage}
|
|
|
|
placeholder="Write a message"
|
|
|
|
placeholderTextColor="#FFFFFF"
|
|
|
|
multiline // Allows multiple lines
|
|
|
|
numberOfLines={4}
|
|
|
|
textAlignVertical="top" // Align text at the top
|
|
|
|
onContentSizeChange={(event) =>
|
|
|
|
updateInputHeight(event.nativeEvent.contentSize.height)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{/* Send Message Button */}
|
|
|
|
<TouchableOpacity style={styles.button} onPress={sendMessage}>
|
|
|
|
<ThemedText style={styles.buttonText}>Send Message</ThemedText>
|
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
</LinearGradient>
|
|
|
|
</SafeAreaView>
|
2024-09-10 13:30:33 -05:00
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
</KeyboardAvoidingView>
|
2024-09-10 11:03:30 -05:00
|
|
|
);
|
2024-09-09 22:57:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2024-09-25 13:16:33 -05:00
|
|
|
safeContainer: {
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: 'transparent', // Ensure full-screen safe area
|
|
|
|
},
|
2024-09-09 22:57:42 -05:00
|
|
|
container: {
|
|
|
|
flex: 1,
|
2024-09-25 11:53:19 -05:00
|
|
|
backgroundColor: 'transparent',
|
2024-09-25 13:16:33 -05:00
|
|
|
},
|
|
|
|
chatContainer: {
|
|
|
|
flex: 1,
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
paddingHorizontal: 20, // Ensures padding around the input and button
|
2024-09-09 22:57:42 -05:00
|
|
|
},
|
2024-09-10 11:03:30 -05:00
|
|
|
input: {
|
|
|
|
width: '100%',
|
2024-09-25 13:16:33 -05:00
|
|
|
minHeight: 150, // Minimum height for the TextInput
|
2024-09-10 16:57:14 -05:00
|
|
|
borderColor: 'transparent',
|
2024-09-10 11:03:30 -05:00
|
|
|
borderWidth: 1,
|
2024-09-10 15:33:34 -05:00
|
|
|
borderRadius: 10,
|
2024-09-10 11:03:30 -05:00
|
|
|
padding: 10,
|
2024-09-25 13:16:33 -05:00
|
|
|
marginBottom: 10,
|
|
|
|
fontSize: 32, // Adjust font size to shrink for smaller devices
|
|
|
|
lineHeight: 40, // Adjust line height for better readability
|
2024-09-10 16:57:14 -05:00
|
|
|
textAlign: 'center',
|
|
|
|
color: '#FFFFFF',
|
|
|
|
fontWeight: 'bold',
|
2024-09-25 13:16:33 -05:00
|
|
|
//backgroundColor: 'rgba(0, 0, 0, 0.1)', // Subtle background for text input
|
|
|
|
backgroundColor: 'transparent', // Transparent background
|
2024-09-10 11:03:30 -05:00
|
|
|
},
|
|
|
|
button: {
|
2024-09-10 15:33:34 -05:00
|
|
|
backgroundColor: '#730FF8',
|
2024-09-25 13:16:33 -05:00
|
|
|
paddingVertical: 12,
|
|
|
|
paddingHorizontal: 20,
|
2024-09-10 15:33:34 -05:00
|
|
|
borderRadius: 10,
|
2024-09-10 11:03:30 -05:00
|
|
|
},
|
|
|
|
buttonText: {
|
|
|
|
color: 'white',
|
2024-09-25 13:16:33 -05:00
|
|
|
fontSize: 18,
|
2024-09-10 11:03:30 -05:00
|
|
|
fontWeight: 'bold',
|
|
|
|
},
|
2024-09-25 13:16:33 -05:00
|
|
|
});
|