Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chat ui first version #14

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"expo-location": "~16.1.0",
"expo-status-bar": "~1.6.0",
"firebase": "^10.7.1",
"moment": "^2.30.1",
"nativewind": "^2.0.11",
"prettier": "^3.2.4",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.72.6",
Expand Down
220 changes: 135 additions & 85 deletions client/screens/Chat.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,148 @@
import React, {
useState,
useEffect,
useLayoutEffect,
useCallback
} from 'react';
import { TouchableOpacity, Text } from 'react-native';
import { GiftedChat } from 'react-native-gifted-chat';
import {
collection,
addDoc,
orderBy,
query,
onSnapshot
} from 'firebase/firestore';
import { signOut } from 'firebase/auth';
import { auth, database } from '../firebaseConfig';
import { useNavigation } from '@react-navigation/native';
import { AntDesign } from '@expo/vector-icons';
useState,
useEffect,
useLayoutEffect,
useCallback,
} from "react";
import { TouchableOpacity, Text, View, StyleSheet } from "react-native";
import {
GiftedChat,
Bubble,
InputToolbar,
Send,
} from "react-native-gifted-chat";
import {
collection,
addDoc,
orderBy,
query,
onSnapshot,
} from "firebase/firestore";
import { signOut } from "firebase/auth";
import { auth, database } from "../firebaseConfig";
import { useNavigation } from "@react-navigation/native";
import { AntDesign } from "@expo/vector-icons";
import moment from "moment";


export default function Chat() {

const [messages, setMessages] = useState([]);
const navigation = useNavigation();
export default function Chat() {
const [messages, setMessages] = useState([]);
const navigation = useNavigation();

const onSignOut = () => {
signOut(auth).catch(error => console.log('Error logging out: ', error));
};
signOut(auth).catch((error) => console.log("Error logging out: ", error));
};

useLayoutEffect(() => {
navigation.setOptions({
headerRight: () => (
<TouchableOpacity
style={{
marginRight: 10
}}
onPress={onSignOut}
>
<AntDesign name="logout" size={24} color='#FAFAFA' style={{marginRight: 10}}/>
</TouchableOpacity>
)
});
}, [navigation]);
useLayoutEffect(() => {
navigation.setOptions({
headerRight: () => (
<View style={{ flexDirection: "row" }}>
<TouchableOpacity onPress={onSignOut}>
<AntDesign
name="logout"
size={24}
color="red"
style={{ marginRight: 20 }}
/>
</TouchableOpacity>
</View>
),
});
}, [navigation]);

useLayoutEffect(() => {
useLayoutEffect(() => {
const collectionRef = collection(database, "chats");
const q = query(collectionRef, orderBy("createdAt", "desc"));

const collectionRef = collection(database, 'chats');
const q = query(collectionRef, orderBy('createdAt', 'desc'));

const unsubscribe = onSnapshot(q, querySnapshot => {
console.log('querySnapshot unsusbscribe');
setMessages(
querySnapshot.docs.map(doc => ({
_id: doc.data()._id,
createdAt: doc.data().createdAt.toDate(),
text: doc.data().text,
user: doc.data().user
}))
);
});
const unsubscribe = onSnapshot(q, (querySnapshot) => {
console.log("querySnapshot unsubscribe");
setMessages(
querySnapshot.docs.map((doc) => ({
_id: doc.data()._id,
createdAt: doc.data().createdAt.toDate(),
text: doc.data().text,
user: doc.data().user,
}))
);
});
return unsubscribe;
}, []);
}, []);

const onSend = useCallback((messages = []) => {
setMessages(previousMessages =>
GiftedChat.append(previousMessages, messages)
);
const onSend = useCallback((messages = []) => {
setMessages((previousMessages) =>
GiftedChat.append(previousMessages, messages)
);

const { _id, createdAt, text, user } = messages[0];
addDoc(collection(database, 'chats'), {
_id,
createdAt,
text,
user
});
}, []);
const { _id, createdAt, text, user } = messages[0];
addDoc(collection(database, "chats"), {
_id,
createdAt,
text,
user,
});
}, []);

return (
<GiftedChat
messages={messages}
showAvatarForEveryMessage={false}
showUserAvatar={false}
onSend={messages => onSend(messages)}
messagesContainerStyle={{
backgroundColor: '#fff'
}}
textInputStyle={{
backgroundColor: '#fff',
borderRadius: 20,
}}
user={{
_id: auth?.currentUser?.email,
avatar: 'https://i.pravatar.cc/700'
}}
const renderBubble = (props) => {
const formattedTime = moment(props.currentMessage.createdAt).format(
"h:mm A"
);
return (
<View
style={[
styles.bubble,
props.position === "right" && styles.rightBubble,
]}
>
<Text>{props.currentMessage.text}</Text>
<Text style={styles.timeText}>{formattedTime}</Text>
</View>
);
};

return (
<GiftedChat
messages={messages}
showAvatarForEveryMessage={false}
showUserAvatar={false}
onSend={(messages) => onSend(messages)}
messagesContainerStyle={{
backgroundColor: "#EEE7E1",
borderRadius: 20,
}}
textInputStyle={{
backgroundColor: "white",
borderRadius: 20,
paddingHorizontal: 12,
marginTop: 6,
borderWidth: 0.5,
borderColor: "grey",
}}
renderInputToolbar={(props) => (
<InputToolbar
containerStyle={{ backgroundColor: "lightgrey" }}
{...props}
/>
);
)}
renderBubble={renderBubble}
user={{
_id: auth?.currentUser?.email,
avatar: "https://i.pravatar.cc/700",
}}
/>
);
}

const styles = StyleSheet.create({
bubble: {
padding: 15,
borderRadius: 15,
backgroundColor: "#f0f0f0",
},
rightBubble: {
backgroundColor: "#EDFFE0",
},
timeText: {
fontSize: 10,
color: "#888",
marginTop: 3,
},
});
Loading