From 88746aa9be5e1423452085379927454df96849cc Mon Sep 17 00:00:00 2001 From: "Daniel T. Rodrigues" Date: Mon, 14 Jun 2021 23:43:55 -0300 Subject: [PATCH 1/3] add comments editing --- src/components/Comment/Comment.tsx | 127 ++++++++++++++++-- src/components/Comment/CommentHeader.tsx | 45 +++---- src/models/howto.models.tsx | 1 + .../Howto/HowToComments/HowToComments.tsx | 3 +- src/pages/Howto/Content/Howto/Howto.tsx | 5 +- src/stores/Howto/howto.store.tsx | 1 + 6 files changed, 143 insertions(+), 39 deletions(-) diff --git a/src/components/Comment/Comment.tsx b/src/components/Comment/Comment.tsx index 8e9248fe2c..0ba21cba6b 100644 --- a/src/components/Comment/Comment.tsx +++ b/src/components/Comment/Comment.tsx @@ -1,13 +1,124 @@ -import React from 'react' -import { Box } from 'rebass' +import React, { useState } from 'react' +import { FaTrash, FaRegEdit } from 'react-icons/fa' +import { Flex } from 'rebass' +import { useCommonStores } from 'src' import { IComment } from 'src/models' +import { hasAdminRights } from 'src/utils/helpers' import { CommentHeader } from './CommentHeader' +import { Text } from 'src/components/Text' +import { Modal } from '../Modal/Modal' +import { TextAreaField } from '../Form/Fields' +import { Field, Form } from 'react-final-form' +import { Button } from 'src/components/Button' export interface IProps extends IComment {} -export const Comment = (props: IProps) => ( - - -

{props.text}

-
-) +export const Comment: React.FC = ({ + _creatorId, + text, + _id, + ...props +}) => { + const { stores } = useCommonStores() + const user = stores.userStore.activeUser + + const [showEditModal, setShowEditModal] = useState(false) + + return ( + + +

{text}

+ + {user && (user._id === _creatorId || hasAdminRights(user)) && ( + + {user._id === _creatorId && ( + setShowEditModal(true)} + > + edit + + )} + { + const confirmation = window.confirm( + 'Are you sure you want to delete this comment?', + ) + if (confirmation) { + await stores.howtoStore.deleteComment(_id) + } + }} + > + delete + + + )} + + {showEditModal && ( + +
{ + console.log(values) + }} + initialValues={{ + comment: text, + }} + render={({ handleSubmit, values }) => ( + + + Edit comment + + + + + + + + )} + /> + + )} + + ) +} diff --git a/src/components/Comment/CommentHeader.tsx b/src/components/Comment/CommentHeader.tsx index 96b2050e72..10bf5dfd8c 100644 --- a/src/components/Comment/CommentHeader.tsx +++ b/src/components/Comment/CommentHeader.tsx @@ -1,23 +1,15 @@ -import React from 'react' -import { FaTrash } from 'react-icons/fa' import { Box, Flex, Text } from 'rebass' -import { useCommonStores } from 'src' import { FlagIconHowTos } from 'src/components/Icons/FlagIcon/FlagIcon' import { IComment } from 'src/models' -import { hasAdminRights } from 'src/utils/helpers' -export interface IProps extends Omit {} +export interface IProps extends Omit {} export const CommentHeader = ({ creatorName, creatorCountry, _created, - _creatorId, - _id, + _edited, }: IProps) => { - const { stores } = useCommonStores() - const user = stores.userStore.activeUser - return ( @@ -26,21 +18,24 @@ export const CommentHeader = ({ {creatorName} - - - {new Date(_created).toLocaleDateString('en-GB').replaceAll('/', '-')} - - {user && (user._id === _creatorId || hasAdminRights(user)) && ( - await stores.howtoStore.deleteComment(_id)} - > + + {_edited ? ( + <> + + (Edited) + + + {new Date(_edited) + .toLocaleDateString('en-GB') + .replaceAll('/', '-')} + + + ) : ( + + {new Date(_created) + .toLocaleDateString('en-GB') + .replaceAll('/', '-')} + )} diff --git a/src/models/howto.models.tsx b/src/models/howto.models.tsx index b2e0a337f2..6409a87d88 100644 --- a/src/models/howto.models.tsx +++ b/src/models/howto.models.tsx @@ -9,6 +9,7 @@ import { IUploadedFileMeta } from 'src/stores/storage' export interface IComment { _id: string _created: string + _edited?: string _creatorId: string creatorName: string creatorCountry?: string | null diff --git a/src/pages/Howto/Content/Howto/HowToComments/HowToComments.tsx b/src/pages/Howto/Content/Howto/HowToComments/HowToComments.tsx index d8dcf65771..664a7ba72e 100644 --- a/src/pages/Howto/Content/Howto/HowToComments/HowToComments.tsx +++ b/src/pages/Howto/Content/Howto/HowToComments/HowToComments.tsx @@ -9,12 +9,11 @@ import { IComment } from 'src/models' const MAX_COMMENTS = 5 interface IProps { - userName?: string comments?: IComment[] } // TODO: Expect the comments as a prop from the HowTo -export const HowToComments = ({ userName, comments }: IProps) => { +export const HowToComments = ({ comments }: IProps) => { const { stores } = useCommonStores() const [moreComments, setMoreComments] = useState(1) diff --git a/src/pages/Howto/Content/Howto/Howto.tsx b/src/pages/Howto/Content/Howto/Howto.tsx index fdcb1639b6..48f28fb73c 100644 --- a/src/pages/Howto/Content/Howto/Howto.tsx +++ b/src/pages/Howto/Content/Howto/Howto.tsx @@ -142,10 +142,7 @@ export class Howto extends React.Component< ))} - + diff --git a/src/stores/Howto/howto.store.tsx b/src/stores/Howto/howto.store.tsx index baca3fb25d..fc5253fd61 100644 --- a/src/stores/Howto/howto.store.tsx +++ b/src/stores/Howto/howto.store.tsx @@ -187,6 +187,7 @@ export class HowtoStore extends ModuleStore { ) if (commentIndex !== -1) { comments[commentIndex].text = newText.slice(0, 400).trim() + comments[commentIndex]._edited = new Date().toISOString() const updatedHowto: IHowto = { ...toJS(howto), From 3f7c99a37544e4fc49d7f0081ae9bb10e3558c47 Mon Sep 17 00:00:00 2001 From: "Daniel T. Rodrigues" Date: Wed, 16 Jun 2021 20:12:23 -0300 Subject: [PATCH 2/3] increase edit comment modal size --- src/components/Comment/Comment.tsx | 2 +- src/components/Modal/Modal.tsx | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/components/Comment/Comment.tsx b/src/components/Comment/Comment.tsx index 0ba21cba6b..3647a22034 100644 --- a/src/components/Comment/Comment.tsx +++ b/src/components/Comment/Comment.tsx @@ -71,7 +71,7 @@ export const Comment: React.FC = ({ )} {showEditModal && ( - + { console.log(values) diff --git a/src/components/Modal/Modal.tsx b/src/components/Modal/Modal.tsx index ad28c856bd..8eb2a62376 100644 --- a/src/components/Modal/Modal.tsx +++ b/src/components/Modal/Modal.tsx @@ -8,6 +8,7 @@ interface IProps { // provide onDidDismiss function to enable backdrop click dismiss onDidDismiss: (data?: any) => void height?: number + width?: number } interface IState { isOpen: boolean @@ -54,12 +55,12 @@ export class Modal extends React.Component { render() { const isOpen = this.state - const { height, children } = this.props + const { height, width, children } = this.props return ( isOpen && ( this.dismiss()} /> - + {children} From 8701a23172b851bc399f8dc3f00de34f63a51b82 Mon Sep 17 00:00:00 2001 From: danitrod Date: Mon, 28 Jun 2021 20:42:17 -0300 Subject: [PATCH 3/3] fix comments new-line wrapping --- src/components/Comment/Comment.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/Comment/Comment.tsx b/src/components/Comment/Comment.tsx index 3647a22034..13a8e229f7 100644 --- a/src/components/Comment/Comment.tsx +++ b/src/components/Comment/Comment.tsx @@ -34,7 +34,9 @@ export const Comment: React.FC = ({ style={{ borderRadius: '5px' }} > -

{text}

+ + {text} + {user && (user._id === _creatorId || hasAdminRights(user)) && (