import React, { useState } from "react"; import { Col, Row, CloseButton, Button } from "react-bootstrap"; import StorageImage from "./StorageImage"; import CircularImage from "./CircularImage"; import UploadPhoto from "./UploadPhoto"; import DisplayComment from "./DisplayComment"; import StyledTextarea from "./StyledTextarea"; import { MdPhotoCamera } from "react-icons/md"; import { MdSend } from "react-icons/md"; import { addPhoto, handleTextareaChange, delPhoto, handleKeyPress, } from "./helper"; import "./Comments.css"; import { useSelector } from "react-redux"; import { updatePost } from "../backend/backend"; const Comments = (props) => { const { post } = props; const user = useSelector((state) => state.currentUser); const userID = useSelector((state) => state.user.id); const WELCOME_TEXT = "Write a comment ..."; const INIT_COMMENT = { userID: userID, text: "", isPhoto: false, photoURL: "", }; const [comment, setComment] = useState(INIT_COMMENT); const [show, setShow] = useState(false); function handleChange(e) { handleTextareaChange({ e: e, state: comment, setState: setComment, }); } function addPhotoToComment(file) { addPhoto({ state: comment, setState: setComment, file: file, userID: userID, }); } function deletePhoto() { delPhoto({ state: comment, setState: setComment, user: user, userID: userID, }); } function saveComment() { if (comment.text === "" && !comment.isPhoto) return; const newPost = { ...post, comments: [], }; const postID = post.postID; if (post.comments) newPost.comments = [...post.comments]; newPost.comments.push(comment); updatePost(newPost, postID); setComment(INIT_COMMENT); } return (
{post.comments && post.comments.map((comment, index) => ( ))} handleKeyPress(e, saveComment)} welcomeText={WELCOME_TEXT} value={comment.text} className="w-100 mt-2" /> {comment.isPhoto && (
)}
); }; export default Comments;