add author edits logic and initial UI

This commit is contained in:
Tom
2023-05-22 18:56:34 +02:00
parent 2d3fcc330a
commit f6f59f684d
5 changed files with 182 additions and 4 deletions
+67
View File
@@ -0,0 +1,67 @@
import React, { useEffect, useRef, useState } from 'react';
import { StyledModal } from './styled/ReplyModal.styled';
import useGeneralStore from '../hooks/stores/useGeneralStore';
import Modal from 'react-modal';
import Draggable from 'react-draggable';
const EditModal = ({ isOpen, closeModal, originalCommentContent }) => {
const {
selectedStyle,
} = useGeneralStore(state => state);
const nodeRef = useRef(null);
const commentRef = useRef();
const [isMobile, setIsMobile] = useState(window.innerWidth <= 480);
useEffect(() => {
const handleResize = () => setIsMobile(window.innerWidth <= 480);
window.addEventListener('resize', handleResize);
// Cleanup function
return () => {
window.removeEventListener('resize', handleResize);
};
}, [setIsMobile]);
return (
<StyledModal
isOpen={isOpen}
onRequestClose={closeModal}
contentLabel="Edit Comment"
shouldCloseOnEsc={false}
shouldCloseOnOverlayClick={isMobile}
selectedStyle={selectedStyle}
style={isMobile ? ({ overlay: { backgroundColor: "rgba(0,0,0,.25)" }}) : ({ overlay: { backgroundColor: "rgba(0,0,0,0)" }})}>
<Draggable handle=".modal-header" nodeRef={nodeRef} disabled={isMobile}>
<div className="modal-content" ref={nodeRef}>
<div className="modal-header">
Edit Comment
<button className="icon" onClick={() => closeModal()} title="close" />
</div>
<div id="form">
<div className="textarea-wrapper">
<textarea className="textarea"
rows="4"
placeholder="Comment"
defaultValue={originalCommentContent}
wrap="soft"
ref={commentRef} />
</div>
<div>
<button id="next">Save</button>
</div>
</div>
</div>
</Draggable>
</StyledModal>
);
};
Modal.setAppElement('#root');
export default EditModal;
+17
View File
@@ -3204,4 +3204,21 @@ export const Footer = styled.div`
return '';
}
}}
`;
export const AuthorDeleteAlert = styled.div`
.author-delete-alert {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 20px;
background: #fff;
border-radius: 5px;
box-shadow: 0 0 10px 0 rgba(0,0,0,.25);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
`;
+92 -4
View File
@@ -1,13 +1,15 @@
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Helmet } from 'react-helmet-async';
import { Link, useNavigate, useParams } from 'react-router-dom';
import { confirmAlert } from 'react-confirm-alert';
import { Tooltip } from 'react-tooltip';
import { useAccount, useAccountComments, useComment, usePublishComment, useSubplebbit } from '@plebbit/plebbit-react-hooks';
import { useAccount, useAccountComments, useComment, usePublishComment, usePublishCommentEdit, useSubplebbit } from '@plebbit/plebbit-react-hooks';
import { flattenCommentsPages } from '@plebbit/plebbit-react-hooks/dist/lib/utils'
import { debounce } from 'lodash';
import useGeneralStore from '../../hooks/stores/useGeneralStore';
import { Container, NavBar, Header, Break, PostForm, PostFormTable, PostMenu } from '../styled/Board.styled';
import { ReplyFormLink, TopBar, BottomBar, BoardForm, Footer } from '../styled/Thread.styled';
import { ReplyFormLink, TopBar, BottomBar, BoardForm, Footer, AuthorDeleteAlert } from '../styled/Thread.styled';
import EditModal from '../EditModal';
import ImageBanner from '../ImageBanner';
import ModerationModal from '../ModerationModal';
import OfflineIndicator from '../OfflineIndicator';
@@ -63,15 +65,20 @@ const Thread = () => {
const replyMenuRefs = useRef({});
const [triggerPublishComment, setTriggerPublishComment] = useState(false);
const [triggerPublishCommentEdit, setTriggerPublishCommentEdit] = useState(false);
const [deletePost, setDeletePost] = useState(false);
const [errorMessage, setErrorMessage] = useState(null);
const [successMessage, setSuccessMessage] = useState(null);
const [isReplyOpen, setIsReplyOpen] = useState(false);
const [isEditModalOpen, setIsEditModalOpen] = useState(false);
const [originalCommentContent, setOriginalCommentContent] = useState(null);
const [prevScrollPos, setPrevScrollPos] = useState(0);
const [visible, setVisible] = useState(true);
const [rotatedStates, setRotatedStates] = useState({});
const [isImageSearchOpen, setIsImageSearchOpen] = useState(false);
const [isModerator, setIsModerator] = useState(false);
const [commentCid, setCommentCid] = useState(null);
const [editedComment, setEditedComment] = useState(null);
useError(errorMessage, [errorMessage]);
useSuccess(successMessage, [successMessage]);
@@ -330,6 +337,82 @@ const Thread = () => {
};
const [publishCommentEditOptions, setPublishCommentEditOptions] = useState({
commentCid: commentCid,
subplebbitAddress: selectedAddress,
onChallenge,
onChallengeVerification,
onError: (error) => {
setErrorMessage(error);
},
});
const {error, publishCommentEdit } = usePublishCommentEdit(publishCommentEditOptions);
useEffect(() => {
if (error) {
setErrorMessage(error);
}
}, [error]);
const handleAuthorDeleteClick = (commentCid) => {
handleOptionClick(commentCid);
confirmAlert({
customUI: ({ onClose }) => {
return (
<AuthorDeleteAlert selectedStyle={selectedStyle}>
<div className='author-delete-alert'>
<p>Are you sure you want to delete this post?</p>
<button onClick={onClose}>No</button>
<button
onClick={() => {
setCommentCid(commentCid);
setPublishCommentEditOptions(prevOptions => ({
...prevOptions,
deleted: true,
}));
setTriggerPublishCommentEdit(true);
onClose();
}}
>
Yes
</button>
</div>
</AuthorDeleteAlert>
);
}
});
};
const handleAuthorEditClick = (comment) => {
handleOptionClick(comment.cid);
setCommentCid(comment.cid);
setOriginalCommentContent(comment.content);
setIsEditModalOpen(true);
}
useEffect(() => {
setPublishCommentEditOptions((prevOptions) => ({
...prevOptions,
commentCid: commentCid,
}));
}, [commentCid]);
useEffect(() => {
if (publishCommentEditOptions && triggerPublishCommentEdit) {
(async () => {
await publishCommentEdit();
setTriggerPublishCommentEdit(false);
})();
}
}, [publishCommentEditOptions, triggerPublishCommentEdit, publishCommentEdit]);
// mobile navbar board select functionality
const handleSelectChange = (event) => {
const selected = event.target.value;
@@ -374,6 +457,11 @@ const Thread = () => {
isOpen={isModerationOpen}
closeModal={() => setIsModerationOpen(false)}
deletePost={deletePost} />
<EditModal
selectedStyle={selectedStyle}
isOpen={isEditModalOpen}
closeModal={() => setIsEditModalOpen(false)}
originalCommentContent={originalCommentContent} />
<NavBar selectedStyle={selectedStyle}>
<>
<span className="boardList">
@@ -714,8 +802,8 @@ const Thread = () => {
<li onClick={() => handleOptionClick(comment.cid)}>Hide thread</li>
{comment?.author?.shortAddress === account?.author?.shortAddress ? (
<>
<li onClick={() => handleOptionClick(comment.cid)}>Edit post</li>
<li onClick={() => handleOptionClick(comment.cid)}>Delete post</li>
<li onClick={() => handleAuthorEditClick(comment)}>Edit post</li>
<li onClick={() => handleAuthorDeleteClick(comment.cid)}>Delete post</li>
</>
) : null}
{isModerator ? (