adding more logic client side for selection and creating and such
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useContext, useState, useCallback, useMemo } from 'react';
|
||||
import React, { useContext, useState, useCallback, useMemo, useEffect } from 'react';
|
||||
|
||||
import { Container } from '@mui/system';
|
||||
import { Avatar, Box, Fab, Grid, IconButton, Paper, Stack, Typography } from '@mui/material';
|
||||
@@ -10,10 +10,15 @@ import Conversations from './Conversations';
|
||||
import Navbar from './Navbar';
|
||||
import Conversation from '@nirvana/core/src/models/conversation.model';
|
||||
import useAuth from '../providers/AuthProvider';
|
||||
import { createOneOnOneConversation } from '../firebase/firestore';
|
||||
import {
|
||||
createOneOnOneConversation,
|
||||
getConversationsQueryLIVE,
|
||||
useGetConversationsQueryLIVE,
|
||||
} from '../firebase/firestore';
|
||||
import { Link, AudioClip, Image } from '@nirvana/core/src/models/content.model';
|
||||
import { useImmer } from 'use-immer';
|
||||
import { User } from '@nirvana/core/src/models/user.model';
|
||||
import { onSnapshot } from 'firebase/firestore';
|
||||
|
||||
type ConversationMap = {
|
||||
[conversationId: string]: Conversation;
|
||||
@@ -71,6 +76,19 @@ export function TerminalProvider({ children }: { children?: React.ReactNode }) {
|
||||
const [userMap, updateUserMap] = useImmer<UserMap>({});
|
||||
const [contentMap, updatecontentMap] = useImmer<ConversationContentMap>({});
|
||||
|
||||
const queryLiveConversations = useGetConversationsQueryLIVE();
|
||||
|
||||
useEffect(() => {
|
||||
const unsub = onSnapshot(queryLiveConversations, (querySnapshot) => {
|
||||
const convos = querySnapshot.docs.map((doc) => doc.data());
|
||||
|
||||
enqueueSnackbar('got conversations...logging', { variant: 'success' });
|
||||
console.log(convos);
|
||||
});
|
||||
|
||||
return () => unsub();
|
||||
}, [queryLiveConversations]);
|
||||
|
||||
// cache of selected conversation
|
||||
const selectedConversation: Conversation | undefined = useMemo(() => {
|
||||
// select if we do have it
|
||||
@@ -100,9 +118,9 @@ export function TerminalProvider({ children }: { children?: React.ReactNode }) {
|
||||
// if there is already a convo with exactly me and him, then select it
|
||||
const findExistingConversation = Object.values(conversationMap).find((convo) => {
|
||||
if (
|
||||
convo.membersInRoom?.length === 2 &&
|
||||
convo.membersInRoom.includes(user.uid) &&
|
||||
convo.membersInRoom.includes(otherUserId)
|
||||
convo.membersList?.length === 2 &&
|
||||
convo.membersList.includes(user.uid) &&
|
||||
convo.membersList.includes(otherUserId)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
@@ -115,7 +133,11 @@ export function TerminalProvider({ children }: { children?: React.ReactNode }) {
|
||||
}
|
||||
|
||||
// create conversation in this case
|
||||
await createOneOnOneConversation(otherUserId, user.uid);
|
||||
const newConversationId = await createOneOnOneConversation(otherUserId, user.uid);
|
||||
|
||||
enqueueSnackbar('started conversation!', { variant: 'success' });
|
||||
|
||||
setSelectedConversationId(newConversationId);
|
||||
} catch (error) {
|
||||
enqueueSnackbar('Something went wrong, please try again', { variant: 'error' });
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import { User as FirebaseUser } from 'firebase/auth';
|
||||
import { firestoreDb } from './connect';
|
||||
import { User } from '@nirvana/core/src/models/user.model';
|
||||
import Conversation from '@nirvana/core/src/models/conversation.model';
|
||||
import useAuth from '../providers/AuthProvider';
|
||||
|
||||
interface Document {
|
||||
id: string;
|
||||
@@ -127,17 +128,27 @@ export const getUserById = async (userId: string): Promise<User | undefined> =>
|
||||
};
|
||||
|
||||
// get the conversations for particular user
|
||||
export const getConversationsQueryLIVE = async (userId: string) =>
|
||||
query(db.conversations, where('membersList', 'array-contains', userId));
|
||||
export const useGetConversationsQueryLIVE = () => {
|
||||
const { user } = useAuth();
|
||||
|
||||
return query(db.conversations, where('membersList', 'array-contains', user.uid));
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param otherUserId
|
||||
* @param myUserId
|
||||
* @returns id of new conversation
|
||||
*/
|
||||
export const createOneOnOneConversation = async (
|
||||
otherUserId: string,
|
||||
myUserId: string,
|
||||
): Promise<void> => {
|
||||
): Promise<string | undefined> => {
|
||||
const newConversation = new Conversation(myUserId, [myUserId, otherUserId]);
|
||||
|
||||
try {
|
||||
await addDoc(db.conversations, newConversation);
|
||||
const newDoc = await addDoc(db.conversations, newConversation);
|
||||
return newDoc.id;
|
||||
} catch (e) {
|
||||
console.error('Error creating user: ', e);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user