diff options
author | Vitaly Takmazov | 2022-11-20 05:59:45 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2023-01-13 10:37:59 +0300 |
commit | 0930332430de0b9f123ecb556949c8db54a92359 (patch) | |
tree | 61eca561df695a48b8ba2d1cd47744e98f14b61d /vnext | |
parent | d3cfc10a2087579f44b3bf91576c2f4ca518326a (diff) |
Fix comment/edit/pm
Diffstat (limited to 'vnext')
-rw-r--r-- | vnext/src/App.js | 2 | ||||
-rw-r--r-- | vnext/src/ui/Chat.js | 19 | ||||
-rw-r--r-- | vnext/src/ui/Post.js | 23 | ||||
-rw-r--r-- | vnext/src/ui/Thread.js | 27 |
4 files changed, 41 insertions, 30 deletions
diff --git a/vnext/src/App.js b/vnext/src/App.js index 69ceb456..60d5212d 100644 --- a/vnext/src/App.js +++ b/vnext/src/App.js @@ -19,6 +19,7 @@ import { useCookies } from 'react-cookie'; import { me, trends } from './api'; import { useVisitor } from './ui/VisitorContext'; import Avatar from './ui/Avatar'; +import { Toaster } from 'react-hot-toast'; /** * @@ -114,6 +115,7 @@ export default function App({ footer }) { return ( <> <Header /> + <Toaster /> { <aside id="sidebar"> <div id="sidebar_wrapper"> 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) { <div className="chatroom"> <ul className="Chat_messages"> { - chats.map((chat) => + messages.map((chat) => <PM key={dayjs.utc(chat.timestamp).valueOf()} chat={chat} {...props} /> ) } 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) { <Comment msg={msg} draft={msg.rid === editing.replyto ? editing.body : ''} active={active} setActive={setActive} onStartEditing={startEditing} postComment={postComment} /> </li> )) : ( - <> + <> { Array(loaders).fill().map((it, i) => <Spinner key={i} />) } |