creating conversation sort of working but not fully
This commit is contained in:
@@ -38,27 +38,15 @@ const createConversation = async (req: Request, res: Response, next: NextFunctio
|
||||
const createRequest = req.body as CreateConversationRequest;
|
||||
const userInfo = res.locals.userInfo as JwtClaims;
|
||||
|
||||
console.log('other members', createRequest.otherMemberIds);
|
||||
|
||||
if (!createRequest.otherMemberIds || createRequest.otherMemberIds.length === 0) {
|
||||
if (!createRequest.otherUsers) {
|
||||
return next(new Error('must provide who you want to talk to'));
|
||||
}
|
||||
|
||||
// process of creating other conversation user members with their user objects
|
||||
// for cached data purposes
|
||||
const otherUserMembers = await UserService.getUsersByIds(createRequest.otherMemberIds);
|
||||
if (!otherUserMembers) {
|
||||
return next(new Error('unable to find other conversation members'));
|
||||
}
|
||||
const conversationUserMembers: ConversationUserMember[] = [];
|
||||
|
||||
createRequest.otherMemberIds.forEach(async (memberId) => {
|
||||
const userObject = otherUserMembers.find((userObj) => userObj._id?.equals(memberId));
|
||||
|
||||
if (!userObject) {
|
||||
return next(new Error('unable to find a user that was passed in'));
|
||||
}
|
||||
|
||||
createRequest.otherUsers.forEach((userObject) => {
|
||||
const newConversationMember = new ConversationMember(MemberRole.regular, MemberState.inbox);
|
||||
|
||||
conversationUserMembers.push({
|
||||
@@ -89,6 +77,8 @@ const createConversation = async (req: Request, res: Response, next: NextFunctio
|
||||
return next(Error('unable to create a conversation'));
|
||||
}
|
||||
|
||||
// todo: send updates to other users if they are connected through sockets
|
||||
|
||||
const responseObj = new NirvanaResponse<CreateConversationResponse>(
|
||||
new CreateConversationResponse(insertResult.insertedId),
|
||||
undefined,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import Conversation from '@nirvana/core/models/conversation.model';
|
||||
import { ObjectId } from 'mongodb';
|
||||
import User from '../models/user.model';
|
||||
|
||||
export default class CreateConversationRequest {
|
||||
constructor(public otherMemberIds: ObjectId[], public conversationName?: string) {}
|
||||
constructor(public otherUsers: User[], public conversationName?: string) {}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios';
|
||||
|
||||
import CreateConversationRequest from '../../../core/requests/CreateConversationRequest.request';
|
||||
import CreateConversationResponse from '../../../core/responses/CreateConversationResponse.response';
|
||||
import LoginResponse from '@nirvana/core/responses/login.response';
|
||||
import NirvanaResponse from '../../../core/responses/nirvanaResponse';
|
||||
import UserDetailsResponse from '@nirvana/core/responses/userDetails.response';
|
||||
import UserSearchResponse from '@nirvana/core/responses/userSearch.response';
|
||||
|
||||
@@ -71,3 +74,9 @@ export async function getUserDetails(): Promise<UserDetailsResponse> {
|
||||
export async function userSearch(searchQuery: string): Promise<UserSearchResponse> {
|
||||
return await NirvanaApi.fetch(`/search/users?query=${searchQuery}`, 'GET', true);
|
||||
}
|
||||
|
||||
export async function createConversation(
|
||||
req: CreateConversationRequest,
|
||||
): Promise<NirvanaResponse<CreateConversationResponse>> {
|
||||
return await NirvanaApi.fetch(`/conversations`, 'POST', true, req);
|
||||
}
|
||||
|
||||
@@ -20,11 +20,13 @@ import React, { HTMLAttributes, useCallback, useEffect, useState } from 'react';
|
||||
import { useDebounce, useKeyPressEvent, useToggle } from 'react-use';
|
||||
|
||||
import CircularProgress from '@mui/material/CircularProgress';
|
||||
import CreateConversationRequest from '../../../core/requests/CreateConversationRequest.request';
|
||||
import { FiX } from 'react-icons/fi';
|
||||
import { NirvanaRules } from '../util/rules';
|
||||
import User from '@nirvana/core/models/user.model';
|
||||
import UserDetailRow from '../subcomponents/UserDetailRow';
|
||||
import { blueGrey } from '@mui/material/colors';
|
||||
import { createConversation } from '../api/NirvanaApi';
|
||||
import toast from 'react-hot-toast';
|
||||
import useAuth from '../providers/AuthProvider';
|
||||
import useRouter from '../providers/RouterProvider';
|
||||
@@ -92,12 +94,23 @@ export default function NewConversationDialog() {
|
||||
|
||||
setIsSubmitting(true);
|
||||
|
||||
// await handleSubmit(selectedUsers, conversationName);
|
||||
toast('submitting');
|
||||
try {
|
||||
// if it's just one other person, then call quick dial master function which would do all of the checking
|
||||
|
||||
// clear form for next time
|
||||
setSelectedUsers([]);
|
||||
setConversationName('');
|
||||
// if more than one person, then just create conversation
|
||||
|
||||
const createResult = await createConversation(
|
||||
new CreateConversationRequest(selectedUsers, conversationName),
|
||||
);
|
||||
|
||||
// clear form for next time
|
||||
setSelectedUsers([]);
|
||||
setConversationName('');
|
||||
|
||||
// todo: select conversation Id that was created
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
setIsSubmitting(false);
|
||||
}, [selectedUsers, setConversationName, conversationName, setSelectedUsers, setIsSubmitting]);
|
||||
|
||||
Reference in New Issue
Block a user