From 1d104aa6e64079e0eee5f26d133b8f5cefb015c6 Mon Sep 17 00:00:00 2001 From: Michael Baghel Date: Fri, 26 Feb 2021 12:56:19 +0400 Subject: [PATCH 1/2] Refactored Report Popup to more general EmailPopup Replaced email logic for extension request with Email Popup --- config/dev.js | 5 -- config/prod.js | 5 -- src/components/EmailPopup/actions/index.js | 21 +++++ .../EmailPopup/hooks/useEmailPopup.js | 20 +++++ .../{ReportPopup => EmailPopup}/index.jsx | 36 ++++---- .../reducers/index.js | 15 ++-- src/components/ReportPopup/actions/index.js | 22 ----- .../ReportPopup/hooks/useReportPopup.js | 20 ----- src/constants/index.js | 6 +- src/reducers/index.js | 4 +- .../components/TeamMembers/index.jsx | 35 ++++++-- .../components/TeamSummary/index.jsx | 11 ++- src/routes/MyTeamsDetails/index.jsx | 4 +- .../MyTeamsList/components/TeamCard/index.jsx | 11 ++- src/routes/MyTeamsList/index.jsx | 4 +- src/services/teams.js | 23 +---- src/utils/format.js | 84 +++++++++++++++++-- 17 files changed, 201 insertions(+), 125 deletions(-) create mode 100644 src/components/EmailPopup/actions/index.js create mode 100644 src/components/EmailPopup/hooks/useEmailPopup.js rename src/components/{ReportPopup => EmailPopup}/index.jsx (62%) rename src/components/{ReportPopup => EmailPopup}/reducers/index.js (65%) delete mode 100644 src/components/ReportPopup/actions/index.js delete mode 100644 src/components/ReportPopup/hooks/useReportPopup.js diff --git a/config/dev.js b/config/dev.js index 54870756..2cba0084 100644 --- a/config/dev.js +++ b/config/dev.js @@ -9,11 +9,6 @@ module.exports = { */ CONNECT_WEBSITE_URL: "https://connect.topcoder-dev.com", - /** - * Email to request extension - */ - EMAIL_REQUEST_EXTENSION: "customersuccess@topcoder-dev.com", - API: { V5: "https://api.topcoder-dev.com/v5", V3: "https://api.topcoder-dev.com/v3", diff --git a/config/prod.js b/config/prod.js index e2069074..21cdc373 100644 --- a/config/prod.js +++ b/config/prod.js @@ -9,11 +9,6 @@ module.exports = { */ CONNECT_WEBSITE_URL: "https://connect.topcoder.com", - /** - * Email to request extension - */ - EMAIL_REQUEST_EXTENSION: "customersuccess@topcoder.com", - API: { V5: "https://api.topcoder.com/v5", V3: "https://api.topcoder.com/v3", diff --git a/src/components/EmailPopup/actions/index.js b/src/components/EmailPopup/actions/index.js new file mode 100644 index 00000000..5c5ba629 --- /dev/null +++ b/src/components/EmailPopup/actions/index.js @@ -0,0 +1,21 @@ +/** + * Report popup actions + */ +import { ACTION_TYPE } from "constants"; + +/** + * Action to create an email popup and open it + * @param {Object} popupOptions Options to customize popup appearance and behaviour + * @param {Object} data Data to send to server with email text + */ +export const openEmailPopup = (popupOptions, data) => ({ + type: ACTION_TYPE.OPEN_EMAIL_POPUP, + payload: { popupOptions, data }, +}); + +/** + * Action to close an email popup + */ +export const closeEmailPopup = () => ({ + type: ACTION_TYPE.CLOSE_EMAIL_POPUP, +}); diff --git a/src/components/EmailPopup/hooks/useEmailPopup.js b/src/components/EmailPopup/hooks/useEmailPopup.js new file mode 100644 index 00000000..4f80a7f4 --- /dev/null +++ b/src/components/EmailPopup/hooks/useEmailPopup.js @@ -0,0 +1,20 @@ +/** + * Use email popup hook + */ + +import { useDispatch } from "react-redux"; +import { openEmailPopup } from "../actions"; + +/** + * Hook to allow email popup to be opened by any other component + * (as long as it is mounted somewhere in the tree) + * + * @returns func A wrapper around the open report dispatch + */ +export const useEmailPopup = () => { + const dispatch = useDispatch(); + + return (popupOptions, data) => { + dispatch(openEmailPopup(popupOptions, data)); + }; +}; diff --git a/src/components/ReportPopup/index.jsx b/src/components/EmailPopup/index.jsx similarity index 62% rename from src/components/ReportPopup/index.jsx rename to src/components/EmailPopup/index.jsx index c897bebc..492f8660 100644 --- a/src/components/ReportPopup/index.jsx +++ b/src/components/EmailPopup/index.jsx @@ -1,45 +1,49 @@ /** - * A report popup used to report issues with teams or team members + * An email popup used to format and send emails for reporting issues + * and requesting extensions */ import React, { useCallback, useState } from "react"; +import _ from "lodash"; import { useSelector, useDispatch } from "react-redux"; import { toastr } from "react-redux-toastr"; -import { closeReport } from "./actions"; +import { closeEmailPopup } from "./actions"; import BaseModal from "components/BaseModal"; import TextArea from "components/TextArea"; import Button from "../Button"; -import { postReport } from "services/teams"; +import { postEmail } from "services/teams"; import CenteredSpinner from "components/CenteredSpinner"; -function ReportPopup() { - const { isOpen, teamName, teamId, memberHandle } = useSelector( - (state) => state.reportPopup +function EmailPopup() { + const { isOpen, popupOptions, data } = useSelector( + (state) => state.emailPopup ); const dispatch = useDispatch(); const [textVal, setTextVal] = useState(""); const [isLoading, setIsLoading] = useState(false); - const submitReport = () => { + const submitEmail = () => { setIsLoading(true); - postReport(teamName, teamId, textVal, memberHandle) + _.set(data, popupOptions.textDataField, textVal); + + postEmail(data) .then(() => { setIsLoading(false); closeModal(); - toastr.success("Report submitted successfully"); + toastr.success("Email submitted successfully"); }) .catch((err) => { setIsLoading(false); - toastr.error("Report failed", err.message); + toastr.error("Email failed to send", err.message); }); }; const button = (