Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add comments editing #1175

Merged
merged 4 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 121 additions & 8 deletions src/components/Comment/Comment.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,126 @@
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) => (
<Box p="3" bg={'white'} width="100%" mb={4} style={{ borderRadius: '5px' }}>
<CommentHeader {...props} />
<p>{props.text}</p>
</Box>
)
export const Comment: React.FC<IProps> = ({
_creatorId,
text,
_id,
...props
}) => {
const { stores } = useCommonStores()
const user = stores.userStore.activeUser

const [showEditModal, setShowEditModal] = useState(false)

return (
<Flex
flexDirection="column"
p="3"
bg={'white'}
width="100%"
mb={4}
style={{ borderRadius: '5px' }}
>
<CommentHeader {...props} />
<Text my={2} style={{ whiteSpace: 'pre-wrap' }}>
{text}
</Text>

{user && (user._id === _creatorId || hasAdminRights(user)) && (
<Flex ml="auto">
{user._id === _creatorId && (
<Text
style={{
cursor: 'pointer',
}}
mr={2}
fontSize="12px"
onClick={async () => setShowEditModal(true)}
>
edit <FaRegEdit />
</Text>
)}
<Text
style={{
cursor: 'pointer',
alignItems: 'center',
}}
fontSize="12px"
onClick={async () => {
const confirmation = window.confirm(
'Are you sure you want to delete this comment?',
)
if (confirmation) {
await stores.howtoStore.deleteComment(_id)
}
}}
>
delete <FaTrash color="red" />
</Text>
</Flex>
)}

{showEditModal && (
<Modal width={600}>
<Form
onSubmit={values => {
console.log(values)
}}
initialValues={{
comment: text,
}}
render={({ handleSubmit, values }) => (
<Flex
as="form"
flexDirection="column"
p={2}
onSubmit={handleSubmit}
>
<Text
as="label"
large
htmlFor="comment"
style={{ marginBottom: '6px' }}
>
Edit comment
</Text>
<Field name="comment" id="comment" component={TextAreaField} />
<Flex mt={4} ml="auto">
<Button
small
mr={4}
variant="secondary"
onClick={() => setShowEditModal(false)}
>
Cancel
</Button>
<Button
small
onClick={async () => {
await stores.howtoStore.editComment(_id, values.comment)
setShowEditModal(false)
}}
>
Edit
</Button>
</Flex>
</Flex>
)}
/>
</Modal>
)}
</Flex>
)
}
46 changes: 20 additions & 26 deletions src/components/Comment/CommentHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
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'
import { Link } from 'src/components/Links'


export interface IProps extends Omit<IComment, 'text'> { }
interface IProps extends Omit<IComment, 'text' | '_id' | '_creatorId'> {}

export const CommentHeader = ({
creatorName,
creatorCountry,
_created,
_creatorId,
_id,
_edited,
}: IProps) => {
const { stores } = useCommonStores()
const user = stores.userStore.activeUser

return (
<Flex justifyContent="space-between" alignItems="baseline">
<Box>
Expand All @@ -36,21 +27,24 @@ export const CommentHeader = ({
</Link>
</span>
</Box>
<Flex>
<Text fontSize={1}>
{new Date(_created).toLocaleDateString('en-GB').replaceAll('/', '-')}
</Text>
{user && (user._id === _creatorId || hasAdminRights(user)) && (
<FaTrash
color="red"
fontSize="12px"
style={{
marginLeft: '8px',
cursor: 'pointer',
alignItems: 'center',
}}
onClick={async () => await stores.howtoStore.deleteComment(_id)}
></FaTrash>
<Flex alignItems="center">
{_edited ? (
<>
<Text color="#777" fontSize={0} mr={2}>
(Edited)
</Text>
<Text fontSize={1}>
{new Date(_edited)
.toLocaleDateString('en-GB')
.replaceAll('/', '-')}
</Text>
</>
) : (
<Text fontSize={1}>
{new Date(_created)
.toLocaleDateString('en-GB')
.replaceAll('/', '-')}
</Text>
)}
</Flex>
</Flex>
Expand Down
5 changes: 3 additions & 2 deletions src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -54,12 +55,12 @@ export class Modal extends React.Component<IProps, IState> {

render() {
const isOpen = this.state
const { height, children } = this.props
const { height, width, children } = this.props
return (
isOpen && (
<Portal id="portal">
<ModalBackdrop id="ModalBackdrop" onClick={() => this.dismiss()} />
<ModalContent id="ModalContent" style={height ? { height } : {}}>
<ModalContent id="ModalContent" height={height} width={width}>
{children}
</ModalContent>
</Portal>
Expand Down
1 change: 1 addition & 0 deletions src/models/howto.models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import styled from 'styled-components'
const MAX_COMMENTS = 5

interface IProps {
userName?: string
comments?: IComment[]
}

Expand Down
5 changes: 1 addition & 4 deletions src/pages/Howto/Content/Howto/Howto.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,7 @@ export class Howto extends React.Component<
))}
</Box>
<AuthWrapper roleRequired="beta-tester">
<HowToComments
userName={loggedInUser?.userName}
comments={activeHowto.comments}
/>
<HowToComments comments={activeHowto.comments} />
</AuthWrapper>
<MoreBox py={20} mt={20}>
<Text bold txtcenter fontSize={[4, 4, 5]}>
Expand Down
1 change: 1 addition & 0 deletions src/stores/Howto/howto.store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,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),
Expand Down