From 8ab1ab824c750dd2c07e9e13ed61b8dab8919ecf Mon Sep 17 00:00:00 2001 From: Pushkar Dureja Date: Fri, 12 Mar 2021 15:02:16 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Added=20PollResponse=20Through=20Em?= =?UTF-8?q?ail?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/poll/PollTable.tsx | 6 +++++- src/components/poll/SubmitChoices.tsx | 27 +++++++++++++++++++++++++-- src/utils/api/mailer.ts | 12 +++++++++++- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/components/poll/PollTable.tsx b/src/components/poll/PollTable.tsx index 9a45dd5..ba725fa 100644 --- a/src/components/poll/PollTable.tsx +++ b/src/components/poll/PollTable.tsx @@ -98,7 +98,11 @@ const PollTable = (props: { {pollFromDB.open && loggedInUserEmailID !== pollCreatorEmailID && ( - + )} {pollFromDB.open && loggedInUserEmailID === pollCreatorEmailID && ( diff --git a/src/components/poll/SubmitChoices.tsx b/src/components/poll/SubmitChoices.tsx index dfcd1d8..08541ae 100644 --- a/src/components/poll/SubmitChoices.tsx +++ b/src/components/poll/SubmitChoices.tsx @@ -1,15 +1,28 @@ import { Button, Spinner } from "react-bootstrap"; import { useState } from "react"; import Router from "next/router"; +import { useSelector } from "react-redux"; import { markChoices } from "../../utils/api/server"; -import { Vote } from "../../models/poll"; +import { Vote, RocketMeetPollFromDB, MailerArgs } from "../../models/poll"; import ResponseMessage from "../ResponseMessage"; +import { decrypt } from "../../helpers/helpers"; +import { sendPollResponse } from "../../utils/api/mailer"; +import { RootState } from "../../store/store"; const SubmitChoices = (props: { newVote: Vote; pollID: string; + pollFromDB: RocketMeetPollFromDB; }): JSX.Element => { - const { newVote, pollID } = props; + const { newVote, pollID, pollFromDB } = props; + const pollCreatorEmailID = decrypt(pollFromDB.encryptedEmailID); + const token = useSelector((state: RootState) => state.authReducer.token); + const isLoggedIn = useSelector( + (state: RootState) => state.authReducer.isLoggedIn + ); + const senderEmailID = useSelector( + (state: RootState) => state.authReducer.username + ); const [response, setResponse] = useState({ status: false, @@ -31,6 +44,16 @@ const SubmitChoices = (props: { }; const submitChoiceResponse = await markChoices(voterArgs); if (submitChoiceResponse.statusCode === 201) { + if (isLoggedIn) { + const mailerArgs: MailerArgs = { + pollID, + senderEmailID, + pollTitle: pollFromDB.title, + receiverIDs: Array(pollCreatorEmailID), + senderName: newVote.name, + }; + sendPollResponse(mailerArgs, token); + } Router.reload(); } else if (submitChoiceResponse.statusCode === 400) { setResponse({ diff --git a/src/utils/api/mailer.ts b/src/utils/api/mailer.ts index 58f5bad..2d2b657 100644 --- a/src/utils/api/mailer.ts +++ b/src/utils/api/mailer.ts @@ -51,4 +51,14 @@ const sendFinalTime = ( return httpPost(payload, endpoint, token); }; -export { sendPollInvites, sendFinalTime }; +const sendPollResponse = ( + // Send poll creater an email, everytime someone votes on their poll + mailerArgs: MailerArgs, + token: string +): Promise => { + const payload = JSON.stringify(mailerArgs); + const endpoint = `${URL}/pollResponse`; + return httpPost(payload, endpoint, token); +}; + +export { sendPollInvites, sendFinalTime, sendPollResponse };