add user to conv fix & conv create impl
This commit is contained in:
@@ -63,6 +63,23 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
return users;
|
return users;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function createConversation(name) {
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
name
|
||||||
|
};
|
||||||
|
|
||||||
|
const res = await fetch(`${API_BASE}/chat/create_conversation`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${accessTokenSingle}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(payload),
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchConversations() {
|
async function fetchConversations() {
|
||||||
const res = await fetch(`${API_BASE}/chat/get_conv`, {
|
const res = await fetch(`${API_BASE}/chat/get_conv`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -101,7 +118,7 @@ export function HotelProvider({ accessToken, children, resClientId}) {
|
|||||||
//return res.json
|
//return res.json
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchConvUsers({ conv_id }) {
|
async function fetchConvUsers({ conv_id }) {
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
conv_id,
|
conv_id,
|
||||||
@@ -124,7 +141,6 @@ async function fetchConvUsers({ conv_id }) {
|
|||||||
//return res.json
|
//return res.json
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function sendMessage({ conv_id, message }) {
|
async function sendMessage({ conv_id, message }) {
|
||||||
if (!conv_id) {
|
if (!conv_id) {
|
||||||
console.log("conv_id null at sendMessage")
|
console.log("conv_id null at sendMessage")
|
||||||
@@ -206,6 +222,7 @@ async function fetchConvUsers({ conv_id }) {
|
|||||||
sendMessage,
|
sendMessage,
|
||||||
fetchHotelUsers,
|
fetchHotelUsers,
|
||||||
addUserToConv,
|
addUserToConv,
|
||||||
|
createConversation
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
@@ -7,13 +7,16 @@ import { useRef } from "react";
|
|||||||
import "./chatWidget.css"
|
import "./chatWidget.css"
|
||||||
|
|
||||||
export default function ChatWidget({convlist}) {
|
export default function ChatWidget({convlist}) {
|
||||||
const { fetchMessages, sendMessage,
|
const {
|
||||||
usersById, clientId, fetchConvUsers,
|
createConversation,
|
||||||
addUserToConv
|
fetchMessages, sendMessage,
|
||||||
|
usersById, clientId,
|
||||||
|
fetchConvUsers, addUserToConv
|
||||||
} = useHotel();
|
} = useHotel();
|
||||||
|
|
||||||
const [activeConvId, setActiveConvId] = useState(null);
|
const [activeConvId, setActiveConvId] = useState(null);
|
||||||
const [showAddUsers, setShowAddUsers] = useState(false);
|
const [showAddUsers, setShowAddUsers] = useState(false);
|
||||||
|
const [showCreateConv, setShowCreateConv] = useState(false);
|
||||||
|
|
||||||
const handleOpenConv = async (conv_id) => {
|
const handleOpenConv = async (conv_id) => {
|
||||||
setActiveConvId(conv_id);
|
setActiveConvId(conv_id);
|
||||||
@@ -40,7 +43,13 @@ return (
|
|||||||
>
|
>
|
||||||
Add users
|
Add users
|
||||||
</button>
|
</button>
|
||||||
</div>
|
<button
|
||||||
|
onClick={() => setShowCreateConv(v => !v)}
|
||||||
|
>
|
||||||
|
Create Conv
|
||||||
|
</button>
|
||||||
|
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{showAddUsers && activeConvId && (
|
{showAddUsers && activeConvId && (
|
||||||
@@ -67,6 +76,13 @@ return (
|
|||||||
onSend={sendMessage}
|
onSend={sendMessage}
|
||||||
disabled={!activeConvId}
|
disabled={!activeConvId}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{showCreateConv && (
|
||||||
|
<CreateConvMenu
|
||||||
|
onSend={createConversation}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -159,7 +175,28 @@ function AddUsersMenu({ convId, usersById, fetchConvUsers, onValidate, onClose }
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function CreateConvMenu({onSend}) {
|
||||||
|
|
||||||
|
const [nametext, setNameText] = useState("");
|
||||||
|
const handleSend = () =>{
|
||||||
|
if (!nametext.trim()) return;
|
||||||
|
|
||||||
|
onSend(nametext);
|
||||||
|
|
||||||
|
setText("");
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="createConvBox">
|
||||||
|
<input
|
||||||
|
value= {text}
|
||||||
|
onChange= {element => setText(element.target.value)}
|
||||||
|
placeholder="Ma Conversation"
|
||||||
|
/>
|
||||||
|
<button onClick= {handleSend}>Send</button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
function ConvCard({id, title, onOpenConv}) {
|
function ConvCard({id, title, onOpenConv}) {
|
||||||
return(
|
return(
|
||||||
|
|||||||
Reference in New Issue
Block a user