chat retrieval is done
This commit is contained in:
parent
d1e824ab77
commit
86db5df813
@ -5,7 +5,7 @@ const LOCAL = "http://localhost:8099";
|
||||
const DEV = "https://dev.bro-js.ru";
|
||||
const SERVER = "";
|
||||
|
||||
export const BASE_API_URL = LOCAL + getConfigValue("enterfront.api") + "/enterfront";
|
||||
export const BASE_API_URL = DEV + getConfigValue("enterfront.api") + "/enterfront";
|
||||
|
||||
// fetch(`${BASE_API_URL}/books/list`)
|
||||
|
||||
|
11
src/components/home/Search.jsx
Normal file
11
src/components/home/Search.jsx
Normal file
@ -0,0 +1,11 @@
|
||||
import React from 'react';
|
||||
|
||||
const Search = (props) => {
|
||||
return (
|
||||
<a className="MyButton search-class mclaren-regular" onClick={() => {
|
||||
props.search(props.item);
|
||||
}}>Find</a>
|
||||
);
|
||||
};
|
||||
|
||||
export default Search;
|
@ -12,11 +12,23 @@
|
||||
color: orange;
|
||||
}
|
||||
|
||||
.search-class {
|
||||
margin-top: 2vw;
|
||||
margin-bottom: 4vw;
|
||||
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
|
||||
@media only screen and (max-width: 800px) {
|
||||
.homeTitle {
|
||||
font-size: 8vh;
|
||||
}
|
||||
|
||||
.search-class {
|
||||
margin-top: 3vh;
|
||||
}
|
||||
}
|
||||
|
||||
.chatIcon {
|
||||
|
@ -8,6 +8,7 @@ const InputField = (props) => {
|
||||
onChange={(e) => props.setValue(e.target.value)}
|
||||
value={props.value}
|
||||
className="Input"
|
||||
placeholder={(props.placeholder) ? props.placeholder : ''}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
@ -4,10 +4,14 @@ import ChatsList from "../components/home/ChatsList.jsx";
|
||||
import Header from "../components/home/Header.jsx";
|
||||
import {displayMessage} from "../backend/notifications/notifications";
|
||||
import {MessageType} from "../backend/notifications/message";
|
||||
import {get} from "../backend/api";
|
||||
import {get, post} from "../backend/api";
|
||||
import InputField from "../components/reg/InputField.jsx";
|
||||
import Search from "../components/home/Search.jsx";
|
||||
import {URLs} from "../__data__/urls";
|
||||
|
||||
const Home = () => {
|
||||
const [chats, setChats] = useState([])
|
||||
const [interlocutor, setInterlocutor] = useState("")
|
||||
|
||||
async function retrieveChats() {
|
||||
const username = localStorage.getItem("username");
|
||||
@ -24,6 +28,23 @@ const Home = () => {
|
||||
setChats(data.chats);
|
||||
}
|
||||
|
||||
async function createChat(alias) {
|
||||
const username = localStorage.getItem("username");
|
||||
if (!username) {
|
||||
displayMessage("You're not logged in!", MessageType.WARN);
|
||||
return;
|
||||
}
|
||||
|
||||
const {ok, data} = await post('/chat/item/' + username + '/' + alias);
|
||||
if (!ok) {
|
||||
displayMessage(data.message, MessageType.ERROR);
|
||||
} else {
|
||||
localStorage.setItem('message', 'Successfully opened chat!');
|
||||
localStorage.setItem('interlocutorId', alias);
|
||||
window.location.href = URLs.chat.url;
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {retrieveChats().then()}, [])
|
||||
|
||||
return (
|
||||
@ -33,6 +54,15 @@ const Home = () => {
|
||||
</div>
|
||||
|
||||
<HomeTitle/>
|
||||
|
||||
<InputField
|
||||
title="Create new chat"
|
||||
value={interlocutor}
|
||||
setValue={setInterlocutor}
|
||||
placeholder="Enter the username (id)"
|
||||
/>
|
||||
<Search search={createChat} item={interlocutor}/>
|
||||
|
||||
<p>Your chats</p>
|
||||
<ChatsList chats={chats} />
|
||||
</div>
|
||||
|
@ -2,7 +2,7 @@ const chatRouter = require('express').Router();
|
||||
|
||||
module.exports = chatRouter;
|
||||
|
||||
const { getChatFromDB, getUsersChats, addChatToDB } = require('../db');
|
||||
const { getChatFromDB, getUsersChats, addChatToDB, getUserFromDB } = require('../db');
|
||||
|
||||
chatRouter.get('/item/:id1/:id2', (req, res) => {
|
||||
const { id1, id2 } = req.params;
|
||||
@ -27,16 +27,20 @@ chatRouter.post('/item/:id1/:id2', (req, res) => {
|
||||
// Chat already exists
|
||||
res.status(200).send({chat});
|
||||
} else {
|
||||
// Creating new chat
|
||||
const newChat = {
|
||||
id1: id1,
|
||||
id2: id2,
|
||||
messages: []
|
||||
if (!getUserFromDB(id1) || !getUserFromDB(id2)) {
|
||||
res.status(404).send({message: 'Such interlocutor does not exist'});
|
||||
} else {
|
||||
// Creating new chat
|
||||
const newChat = {
|
||||
id1: id1,
|
||||
id2: id2,
|
||||
messages: []
|
||||
}
|
||||
|
||||
addChatToDB(newChat);
|
||||
|
||||
res.status(200).send({newChat});
|
||||
}
|
||||
|
||||
addChatToDB(newChat);
|
||||
|
||||
res.status(200).send({newChat});
|
||||
}
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user