import React from 'react'; // material-ui import { useTheme } from '@mui/material/styles'; import { Box, Button, ButtonBase, Card, Collapse, FormHelperText, Grid, InputAdornment, Menu, MenuItem, Stack, TextField, Typography, useMediaQuery } from '@mui/material'; // third-party import * as yup from 'yup'; import uniqueId from 'lodash/uniqueId'; import { Controller, FormProvider, useForm, useFormContext } from 'react-hook-form'; import { yupResolver } from '@hookform/resolvers/yup'; // project imports import Reply from './Reply'; import Avatar from 'ui-component/extended/Avatar'; import AnimateButton from 'ui-component/extended/AnimateButton'; import { Comment as CommentProps, CommentData, PostProps, Profile } from '_mockApis/user-profile/types'; // assets import FiberManualRecordIcon from '@mui/icons-material/FiberManualRecord'; import MoreVertTwoToneIcon from '@mui/icons-material/MoreVertTwoTone'; import ThumbUpAltTwoToneIcon from '@mui/icons-material/ThumbUpAltTwoTone'; import ReplyTwoToneIcon from '@mui/icons-material/ReplyTwoTone'; import AttachmentRoundedIcon from '@mui/icons-material/AttachmentRounded'; import { FormInputProps } from 'types'; const avatarImage = require.context('assets/images/profile', true); const validationSchema = yup.object().shape({ name: yup.string().required('Reply Field is Required') }); // ==============================|| COMMENT TEXTFIELD ||============================== // const FormInput = ({ bug, label, name, required, ...others }: FormInputProps) => { const { control } = useFormContext(); let isError = false; let errorMessage = ''; if (bug && Object.prototype.hasOwnProperty.call(bug, name)) { isError = true; errorMessage = bug[name].message; } return ( <> {errorMessage && ( {errorMessage} )} ); }; export interface CommentComponentProps { comment: CommentProps; postId: string; handleReplayLikes: PostProps['handleReplayLikes']; handleCommentLikes: PostProps['handleCommentLikes']; replyAdd: PostProps['replyAdd']; user: Profile; } // ==============================|| SOCIAL PROFILE - COMMENT ||============================== // const Comment = ({ comment, handleCommentLikes, handleReplayLikes, postId, replyAdd, user }: CommentComponentProps) => { const theme = useTheme(); const matchesXS = useMediaQuery(theme.breakpoints.down('md')); const [anchorEl, setAnchorEl] = React.useState Element) | null | undefined>(null); const handleClick = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); }; const handleClose = () => { setAnchorEl(null); }; const [openReply, setOpenReply] = React.useState(false); const handleChangeReply = () => { setOpenReply((prev) => !prev); }; let repliesResult: React.ReactElement[] | React.ReactElement = <>; if (Object.keys(comment).length > 0 && comment.data?.replies && comment.data?.replies.length) { repliesResult = comment.data?.replies.map((reply, index) => ( )); } const methods = useForm({ resolver: yupResolver(validationSchema) }); const { handleSubmit, errors, reset } = methods; const onSubmit = async (reply: CommentData) => { handleChangeReply(); const replyId = uniqueId('#REPLY_'); const newReply = { id: replyId, profile: user, data: { comment: reply.name, likes: { like: false, value: 0 }, replies: [] } }; replyAdd(postId, comment.id, newReply); reset({ name: '' }); }; return ( <> {Object.keys(comment).length > 0 && ( {comment.profile.name} {' '} {comment.profile.time} Edit Delete {comment.data?.comment} )} {repliesResult} {/* comment - add new replay */}
) }} />
); }; export default Comment;