From fd7cb24b141e6a63c53cdac64d626185151f9572 Mon Sep 17 00:00:00 2001 From: debsouryadatta Date: Mon, 31 Jul 2023 19:57:16 +0530 Subject: [PATCH] Liking polls functionality added, closes #288 Signed-off-by: debsouryadatta --- src/components/poll.jsx | 218 ++++++++++++++++++++++++---------------- 1 file changed, 129 insertions(+), 89 deletions(-) diff --git a/src/components/poll.jsx b/src/components/poll.jsx index db59e26..aecb853 100644 --- a/src/components/poll.jsx +++ b/src/components/poll.jsx @@ -6,7 +6,7 @@ import { Text } from "@chakra-ui/react"; import { Heading } from "@chakra-ui/react"; import { Button } from "@chakra-ui/react"; import { Stack, Flex, Center, } from "@chakra-ui/react" -import { deleteDoc } from '../lib/db'; +import { db, deleteDoc } from '../lib/db'; import { useToast } from "@chakra-ui/react"; import Pollpopup from './pollPopup'; @@ -38,7 +38,12 @@ import { TwitterShareButton, WhatsappIcon, WhatsappShareButton, - } from "react-share"; +} from "react-share"; +import { faThumbsUp } from '@fortawesome/free-regular-svg-icons'; +import { useEffect } from 'react'; +import { useState } from 'react'; +import { AiFillLike, AiOutlineLike } from 'react-icons/ai'; + export default function Poll(props) { const { user, loadingUser } = useAuth(); @@ -48,12 +53,26 @@ export default function Poll(props) { const [hasVoted, setVoted] = React.useState(''); const [copyIcon, setCopyIcon] = React.useState(faCopy); const [OpenShare, setOpenShare] = React.useState(false); + const [likesArr, setLikesArr] = useState([]) const toast = useToast(); - function upVote(){ - if (hasVoted==='down'){ - setVotes(votes+2); + useEffect(() => { + const checkLikes = () => { + if (props.data.likesArr) { + setLikesArr(props.data.likesArr) + } else { + setLikesArr([]) + } + } + + checkLikes(); + }, []) + + + function upVote() { + if (hasVoted === 'down') { + setVotes(votes + 2); setVoted('up'); } else if (hasVoted === '') { @@ -111,100 +130,121 @@ export default function Poll(props) { await navigator.clipboard.writeText(loc.host + "/poll" + "/" + docId); } + const likesHandler = async () => { + if (!likesArr.includes(user.email)) { + setLikesArr([...likesArr, user.email]); + // const documentRef = doc(db, "polls", props.data.id); + const documentRef = db.collection("polls").doc(props.data.id); + const response = await documentRef.update({ + likesArr: [...likesArr, user.email] + }); + } + } + + return ( <> - - - - {props.pollvoting && - - - {votes}{/*props.upvotes*/} - - - } -
- - {props.name} - {props.description} - - -
-
- {(props.flag === "discover") ? : - <> - {user?.uid === props.polls[0].author.id && - } - } -
- - {showModal && } -
- setOpenShare(false)}> + + + + {props.pollvoting && + + + {votes}{/*props.upvotes*/} + + + } +
+
+ + {props.name} + {props.description} + +
+ + + + +
+
+ {(props.flag === "discover") ? : + <> + {user?.uid === props.polls[0].author.id && + } + } +
+ + {showModal && } +
+ setOpenShare(false)}> Share To - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + +