2

I have been stunned on this problem for a bit now, so I've come to ask for help. I am creating a private messaging app with a group chat feature. I am using firebase 7.14.0 (yes I know that's old) when you click on a contact in my app you can create a groupchat which will create another chat with just you in it (I know its not optimal I just want to get functionality working first) then in that chat you can add people to the chat.

Here is the code I need help with.

import React, { useEffect, useState, useCallback } from 'react';
import firebase from "../firebase"
import { NavLink } from 'react-router-dom';
import { Avatar } from '@material-ui/core';
import './Gccontact.css';

const Gccontact = props => {
    const [contact, setContact] = useState()
    const [conversation, setConversation] = useState()

    const getContact = useCallback(async () => {
        const id = props.contact
        const db = firebase.db
        const unsubscribe = db.collection("users").doc(id).onSnapshot(snapshot => {
            setContact(snapshot.data())
        })
        return unsubscribe
    },[props.contact])

    

    useEffect(() => {
        getContact()
 
    }, [props])

    const addtogroup = useCallback(async uid => {
        const members = [contact.uid];
        await firebase.db.collection("conversations").doc('x8DGIeBhW96ziGh0MEGf').update({ members });
    }, []);

   /* db.collection("users").doc(currentUser.uid).update({
        status
    }) */

    return (
      
            <li className="contact">
                {contact && <div className="wrap" onClick={addtogroup}>
                    <span className={`contact-status ${contact.status}`}></span>
                    <div className="img-container">
                        <Avatar src={contact.profilePicture} alt={contact.name + " Profile Picture"} />
                    </div>
                    <div style={{display: "inline-block"}} className="meta">
                        <p className="display-name">{contact.name}</p>
                        {props.recent && <p className="preview">
                            {props.recent?.sender === firebase?.auth?.currentUser?.uid && <span>You: </span>} 
                            {props.recent?.attachments?.length === 0 ? props.recent?.body : "Picture"}
                        </p>}
                    </div>
                </div>}
            </li> 
        
    );
}

export default Gccontact;

This is the contact file, when you click on the contact I want it to add them to the groupchat. When I try to add the members to group im getting an error saying contact.uid is undefined, so I tried contact.name and it still didn't work. As far as the

await firebase.db.collection("conversations").doc('x8DGIeBhW96ziGh0MEGf').update({ members });

goes I don't know how to get each individuals documents id, so I have a set one just to test. and with .update I noticed it gets rid of all the members and just adds that one that a defined.

Osanda Gamage
  • 446
  • 2
  • 6
  • 16
Charlie
  • 115
  • 1
  • 8
  • There's a related [post](https://stackoverflow.com/questions/60463030/create-private-chat-group-chat-on-different-condition-using-firebase-firestore) where it has guides on how you can achieve Chat application in Firestore. Check the comment of Alex Mamo. – RJC Jan 31 '22 at 08:21

0 Answers0