Untitled Document-9
Untitled Document-9
//this will be the button that allows us to close the page or create group chat
useEffect(() => {
props.navigation.setOptions({
headerLeft: () => {
return (
<HeaderButtons HeaderButtonComponent={CustomHeaderButton}>
<Item title="Close" onPress={() => props.navigation.goBack()} />
</HeaderButtons>
);
},
headerRight: () => {
return (
<HeaderButtons HeaderButtonComponent={CustomHeaderButton}>
{ //Only renders if it is a group chat
isGroupChat &&
<Item title="Create" onPress={() => {}} />
}
</HeaderButtons>
);
},
headerTitle: isGroupChat ? "Add participants" : "New chat",
});
}, []);
<TextInput
placeholder="Search"
style={styles.searchBox}
onChangeText={(text) => setSearchTerm(text)}
/>
</View>
{/*Passes the data through and displays it in the search tab */}
{!isLoading && !noResultsFound && users && (
<FlatList
data={Object.keys(users)}
renderItem={(itemData) => {
const userId = itemData.item;
const userData = users[userId];
return (
<DataItem
title={`${userData.firstName} ${userData.lastName}`}
subTitle={userData.about}
image={userData.profilePicture}
onPress={() => userPressed(userId)}
/>
);
}}
/>
)}
},
inputContainer: {
width: '100%',
paddingHorizontal: 15,
paddingVertical: 15,
backgroundColor: colors.nearlyWhite,
flexDirection: 'row',
borderRadius: 2
},
textbox: {
color: colors.textColor,
width: '100%',
fontFamily: 'regular',
letterSpacing: 0.3
}
});
const navigationProps = {
newChatData: { users: chatUsers },
};
props.navigation.navigate("ChatScreen", navigationProps);
}, [props.route?.params]);
return (
<PageContainer>
<PageTitle text="Chats" />
//Find the first user that isnt the user we are logged in as
const otherUserId = chatData.users.find(
(uid) => uid !== userData.userId
);
//accesses the stored user data
const otherUser = storedUsers[otherUserId];
if (!otherUser) return;
return (
<DataItem
title={title}
subTitle={subTitle}
image={image}
onPress={() =>
props.navigation.navigate("ChatScreen", { chatId })
}
/>
);
}}
/>
</PageContainer>
);
};
//Retrieves chat messages for specific chat through an array with the Id and message
inside
const chatMessages = useSelector((state) => {
//Handles brand new chat if there is nothing in it
if (!chatId) return [];
messageList.push({
key,
...message,
});
}
return messageList;
});
return (
otherUserData && `${otherUserData.firstName} ${otherUserData.lastName}`
);
};
{
/* Function used to manage state changes and handle message sending*/
}
const sendMessage = useCallback(async () => {
try {
let id = chatId;
if (!id) {
//If there is no chatId, create the chat
id = await createChat(userData.userId, props.route.params.newChatData);
setChatId(id);
}
//Sends text message
await sendTextMessage(
chatId,
userData.userId,
messageText,
replyingTo && replyingTo.key
);
//Only clears the text box if the message sends
setMessageText("");
setReplyingTo(null);
} catch (error) {
console.log(error);
setErrorBannerText("Message has failed to send");
setTimeout(() => setErrorBannerText(""), 5000);
}
}, [messageText, chatId]);
setTempImageUri(tempUri);
} catch (error) {
console.log(error);
}
}, [tempImageUri]);
setTempImageUri(tempUri);
} catch (error) {
console.log(error);
}
}, [tempImageUri]);
try {
let id = chatId;
if (!id) {
//If there is no chatId, create the chat
id = await createChat(userData.userId, props.route.params.newChatData);
setChatId(id);
}
//Send image
await sendImage(
id,
userData.userId,
uploadUrl,
replyingTo && replyingTo.key
);
setReplyingTo(null);
return (
<SafeAreaView edges={["right", "left", "bottom"]} style={styles.container}>
<KeyboardAvoidingView
style={styles.screen}
behavior={Platform.OS === "ios" ? "padding" : undefined}
keyboardVerticalOffset={75}
>
<ImageBackground
source={backgroundImage}
style={styles.backgroundImage}
>
{/* all chats appear here- UI*/}
<PageContainer style={{ backgroundColor: "transparent" }}>
{!chatId && (
<Bubble text="This is a new chat. Say hello!" type="system" />
)}
<View style={styles.inputContainer}>
{/* Allows us to add an image from our photos*/}
<TouchableOpacity style={styles.mediaButton} onPress={pickImage}>
<Feather name="plus" size={24} color={colors.blue} />
</TouchableOpacity>
<TextInput
style={styles.textbox}
value={messageText}
onChangeText={(text) => setMessageText(text)}
onSubmitEditing={sendMessage}
/>