From 0930332430de0b9f123ecb556949c8db54a92359 Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Sun, 20 Nov 2022 05:59:45 +0300
Subject: Fix comment/edit/pm
---
vnext/src/ui/Chat.js | 19 +++++++++----------
vnext/src/ui/Post.js | 23 +++++++++++++----------
vnext/src/ui/Thread.js | 27 +++++++++++++++++----------
3 files changed, 39 insertions(+), 30 deletions(-)
(limited to 'vnext/src/ui')
diff --git a/vnext/src/ui/Chat.js b/vnext/src/ui/Chat.js
index c1151daf..392b12a0 100644
--- a/vnext/src/ui/Chat.js
+++ b/vnext/src/ui/Chat.js
@@ -26,16 +26,15 @@ import { Helmet } from 'react-helmet';
*/
export default function Chat(props) {
const [visitor] = useVisitor();
- const [chats, setChats] = useState([]);
+ const [messages, setMessages] = useState([]);
const params = useParams();
let loadChat = useCallback((uname) => {
const { hash } = visitor;
- setChats([]);
if (hash && uname) {
getChat(uname)
.then(response => {
- setChats(response.data);
+ setMessages(response.data);
}).catch(console.log);
}
}, [visitor]);
@@ -43,17 +42,17 @@ export default function Chat(props) {
let onMessage = useCallback((json) => {
const msg = JSON.parse(json.data);
if (msg.user.uname === params.user) {
- setChats((oldChat) => {
+ setMessages((oldChat) => {
return [msg, ...oldChat];
});
}
}, [params.user]);
- let onSend = (template) => {
- pm(template.to.uname, template.body)
- .then(() => {
- loadChat(params.user);
- }).catch(console.log);
+ let onSend = async ({ body }) => {
+ let result = false;
+ let res = await pm(params.user, body).catch(console.error);
+ result = res.status == 200;
+ return result;
};
useEffect(() => {
if (props.connection.addEventListener) {
@@ -78,7 +77,7 @@ export default function Chat(props) {
{
- chats.map((chat) =>
+ messages.map((chat) =>
)
}
diff --git a/vnext/src/ui/Post.js b/vnext/src/ui/Post.js
index 8fc753b4..45eceb35 100644
--- a/vnext/src/ui/Post.js
+++ b/vnext/src/ui/Post.js
@@ -18,16 +18,19 @@ export default function Post() {
let draftMessage = (location.state || {}).data || {};
let [draft, setDraft] = useState(draftMessage.body);
let [params] = useSearchParams();
- let postMessage = (template) => {
- const { attach, body } = template;
- const postAction = draftMessage.mid ? update(draftMessage.mid, 0, body) : post(body, attach);
- postAction
- .then(response => {
- if (response.status === 200) {
- const msg = response.data.newMessage;
- navigate(`/${visitor.uname}/${msg.mid}`);
- }
- }).catch(console.log);
+ let postMessage = async ({ attach, body }) => {
+ try {
+ const res = draftMessage.mid ? await update(draftMessage.mid, 0, body) : await post(body, attach);
+ let result = res.status == 200;
+ if (result) {
+ const msg = res.data.newMessage;
+ navigate(`/${visitor.uname}/${msg.mid}`);
+ }
+ return result;
+ } catch (e) {
+ console.log(e);
+ }
+ return false;
};
let appendTag = (tag) => {
setDraft(prevDraft => {
diff --git a/vnext/src/ui/Thread.js b/vnext/src/ui/Thread.js
index cfb67bb1..de1d74b2 100644
--- a/vnext/src/ui/Thread.js
+++ b/vnext/src/ui/Thread.js
@@ -56,15 +56,19 @@ export default function Thread(props) {
});
}, [hash, message.mid, mid]);
- let postComment = (template) => {
- const { body, attach } = template;
- let commentAction = editing.rid ? update(mid, editing.rid, body) : comment(mid, active, body, attach);
- commentAction.then((res) => {
- console.log(res);
- setEditing(emptyMessage);
- loadReplies();
- })
- .catch(console.log);
+ let postComment = async ({ body, attach }) => {
+ try {
+ let res = editing.rid ? await update(mid, editing.rid, body)
+ : await comment(mid, active, body, attach);
+ let result = res.status == 200;
+ if (result) {
+ setEditing(emptyMessage);
+ }
+ return result;
+ } catch (e) {
+ console.error(e);
+ }
+ return false;
};
let startEditing = (reply) => {
@@ -83,6 +87,9 @@ export default function Thread(props) {
setReplies(oldReplies => {
return [...oldReplies, msg];
});
+ setActive(prev => {
+ return prev + 1;
+ });
}
};
if (props.connection.addEventListener && message.mid) {
@@ -119,7 +126,7 @@ export default function Thread(props) {
)) : (
- <>
+ <>
{
Array(loaders).fill().map((it, i) => )
}
--
cgit v1.2.3