|
| 1 | +/** |
| 2 | + * A report popup used to report issues with teams or team members |
| 3 | + */ |
| 4 | + |
| 5 | +import React, { useCallback, useState } from "react"; |
| 6 | +import { useSelector, useDispatch } from "react-redux"; |
| 7 | +import { toastr } from "react-redux-toastr"; |
| 8 | +import { closeReport } from "./actions"; |
| 9 | +import BaseModal from "components/BaseModal"; |
| 10 | +import TextArea from "components/TextArea"; |
| 11 | +import Button from "../Button"; |
| 12 | +import { postReport } from "services/teams"; |
| 13 | +import CenteredSpinner from "components/CenteredSpinner"; |
| 14 | + |
| 15 | +function ReportPopup() { |
| 16 | + const { isOpen, teamName, teamId, memberHandle } = useSelector( |
| 17 | + (state) => state.reportPopup |
| 18 | + ); |
| 19 | + |
| 20 | + const dispatch = useDispatch(); |
| 21 | + const [textVal, setTextVal] = useState(""); |
| 22 | + const [isLoading, setIsLoading] = useState(false); |
| 23 | + |
| 24 | + const submitReport = () => { |
| 25 | + setIsLoading(true); |
| 26 | + |
| 27 | + postReport(teamName, teamId, textVal, memberHandle) |
| 28 | + .then(() => { |
| 29 | + setIsLoading(false); |
| 30 | + closeModal(); |
| 31 | + toastr.success("Report submitted successfully"); |
| 32 | + }) |
| 33 | + .catch((err) => { |
| 34 | + setIsLoading(false); |
| 35 | + |
| 36 | + // Response interceptor passes only error body |
| 37 | + // use this to identify server-side errors |
| 38 | + if (err instanceof Error) { |
| 39 | + toastr.error("Report failed"); |
| 40 | + } else { |
| 41 | + toastr.error("Report failed", err.message); |
| 42 | + } |
| 43 | + }); |
| 44 | + }; |
| 45 | + |
| 46 | + const button = ( |
| 47 | + <Button |
| 48 | + onClick={() => submitReport()} |
| 49 | + size="medium" |
| 50 | + isSubmit |
| 51 | + disabled={textVal.trim().length < 1 || isLoading} |
| 52 | + > |
| 53 | + Submit |
| 54 | + </Button> |
| 55 | + ); |
| 56 | + |
| 57 | + const closeModal = useCallback(() => { |
| 58 | + dispatch(closeReport()); |
| 59 | + setTextVal(""); |
| 60 | + }, [dispatch]); |
| 61 | + |
| 62 | + return ( |
| 63 | + <BaseModal |
| 64 | + open={isOpen} |
| 65 | + onClose={closeModal} |
| 66 | + title={`Issue Report - ${teamName}${ |
| 67 | + memberHandle ? " - " + memberHandle : "" |
| 68 | + }`} |
| 69 | + button={button} |
| 70 | + disabled={isLoading} |
| 71 | + > |
| 72 | + {isLoading ? ( |
| 73 | + <CenteredSpinner /> |
| 74 | + ) : ( |
| 75 | + <TextArea |
| 76 | + value={textVal} |
| 77 | + onChange={setTextVal} |
| 78 | + placeholder="Describe your issue" |
| 79 | + /> |
| 80 | + )} |
| 81 | + </BaseModal> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +export default ReportPopup; |
0 commit comments