Skip to content

Commit

Permalink
Merge pull request #108 from PushkarDureja/pollresponse
Browse files Browse the repository at this point in the history
✨ Added PollResponse Through Email
  • Loading branch information
anandbaburajan authored Mar 17, 2021
2 parents b9691bd + 8ab1ab8 commit 389b391
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/components/poll/PollTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ const PollTable = (props: {
</tbody>
</Table>
{pollFromDB.open && loggedInUserEmailID !== pollCreatorEmailID && (
<SubmitChoices newVote={newVote} pollID={pollID} />
<SubmitChoices
newVote={newVote}
pollID={pollID}
pollFromDB={pollFromDB}
/>
)}
{pollFromDB.open && loggedInUserEmailID === pollCreatorEmailID && (
<SubmitFinalChoice finalChoice={finalChoice} pollID={pollID} />
Expand Down
27 changes: 25 additions & 2 deletions src/components/poll/SubmitChoices.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<string>(pollCreatorEmailID),
senderName: newVote.name,
};
sendPollResponse(mailerArgs, token);
}
Router.reload();
} else if (submitChoiceResponse.statusCode === 400) {
setResponse({
Expand Down
12 changes: 11 additions & 1 deletion src/utils/api/mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MailerResponse> => {
const payload = JSON.stringify(mailerArgs);
const endpoint = `${URL}/pollResponse`;
return httpPost(payload, endpoint, token);
};

export { sendPollInvites, sendFinalTime, sendPollResponse };

0 comments on commit 389b391

Please sign in to comment.