From 10c81f1ccd538a77d5801ff39bc48274bbea470c Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 22 Jan 2023 22:55:18 +0300 Subject: Message: subscribe/unsubscribe --- vnext/src/ui/Message.js | 42 ++++++++++++++++++++++++++++++++---------- vnext/src/ui/Thread.js | 24 ++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 12 deletions(-) (limited to 'vnext/src/ui') diff --git a/vnext/src/ui/Message.js b/vnext/src/ui/Message.js index d45395bb..3bb59a4a 100644 --- a/vnext/src/ui/Message.js +++ b/vnext/src/ui/Message.js @@ -14,9 +14,16 @@ import { UserLink } from './UserInfo'; import { format, embedUrls } from '../utils/embed'; import { useVisitor } from './VisitorContext'; +/** + * @callback ToggleSubscriptionCallback + * @param { import('../client').Message } message + */ + /** * @typedef {object} MessageProps * @property { import('../client').Message } data data + * @property {boolean} isThread + * @property {ToggleSubscriptionCallback} onToggleSubscription */ /** @@ -24,7 +31,7 @@ import { useVisitor } from './VisitorContext'; * * @param {React.PropsWithChildren<{}> & MessageProps} props props */ -export default function Message({ data, children }) { +export default function Message({ data, isThread = false, onToggleSubscription, children }) { const [visitor] = useVisitor(); const isCode = (data.tags || []).indexOf('code') >= 0; const likesSummary = data.likes ? `${data.likes}` : 'Recommend'; @@ -68,7 +75,7 @@ export default function Message({ data, children }) {  ·  Edit + }} state={{ data: data }}>Edit } @@ -109,16 +116,31 @@ export default function Message({ data, children }) { {likesSummary} )} - {data.user && canComment && ( - - - {commentsSummary} - - )} + { + data.user && canComment && (( + isThread ? ( + { onToggleSubscription(data); }}> + { + data.subscribed ? (<> + + Subscribed + ) : (<> + + Subscribe + )} + + ) : ( + + + {commentsSummary} + + ) + )) + } {children} - + ); } diff --git a/vnext/src/ui/Thread.js b/vnext/src/ui/Thread.js index de1d74b2..2919e830 100644 --- a/vnext/src/ui/Thread.js +++ b/vnext/src/ui/Thread.js @@ -6,7 +6,7 @@ import Message from './Message'; import MessageInput from './MessageInput'; import Spinner from './Spinner'; -import { getMessages, comment, update } from '../api'; +import { getMessages, comment, update, post } from '../api'; import { useVisitor } from './VisitorContext'; import { Helmet } from 'react-helmet'; /** @@ -104,6 +104,26 @@ export default function Thread(props) { const loaders = Math.min(message.replies || 0, 10); const pageTitle = `${params.user} ${message && message.tags || 'thread'}`; + /** @type { import('./Message').ToggleSubscriptionCallback } */ + const handleSubsciptionToggle = (message) => { + if (message.subscribed) { + if (confirm('Unsubscribe?')) { + post(`U #${message.mid}`).then((response) => { + if (response.status === 200) { + setMessage({...message, subscribed: false}); + } + }).catch(console.error); + } + } else { + if (confirm('Subscribe?')) { + post(`S #${message.mid}`).then((response) => { + if (response.status === 200) { + setMessage({...message, subscribed: true}); + } + }).catch(console.error); + } + } + }; return ( <> @@ -111,7 +131,7 @@ export default function Thread(props) { { message.mid ? ( - + {active === (message.rid || 0) && Write a comment...} ) : ( -- cgit v1.2.3