|
| 1 | +import { FC, useCallback, useState } from "react"; |
| 2 | +import Modal from "components/ui/Modal/Modal"; |
| 3 | +import Loader from "components/ui/Loader"; |
| 4 | +import { useHistory, useParams } from "react-router-dom"; |
| 5 | +import { TParams as TAreaDetailParams } from "../AreaView"; |
| 6 | +import { toastr } from "react-redux-toastr"; |
| 7 | +import { TErrorResponse } from "constants/api"; |
| 8 | +import { useAppDispatch } from "hooks/useRedux"; |
| 9 | +import { FormattedMessage, useIntl } from "react-intl"; |
| 10 | +import { areaService } from "services/area"; |
| 11 | +import { getAreas } from "modules/areas"; |
| 12 | + |
| 13 | +type TParams = TAreaDetailParams & { |
| 14 | + teamId: string; |
| 15 | +}; |
| 16 | + |
| 17 | +interface IProps {} |
| 18 | + |
| 19 | +const RemoveTeamModal: FC<IProps> = props => { |
| 20 | + const { teamId, areaId } = useParams<TParams>(); |
| 21 | + const history = useHistory(); |
| 22 | + const intl = useIntl(); |
| 23 | + const dispatch = useAppDispatch(); |
| 24 | + const [isRemoving, setIsRemoving] = useState(false); |
| 25 | + |
| 26 | + const onClose = useCallback(() => { |
| 27 | + history.push(`/areas/${areaId}`); |
| 28 | + }, [history, areaId]); |
| 29 | + |
| 30 | + const removeTeam = async () => { |
| 31 | + setIsRemoving(true); |
| 32 | + try { |
| 33 | + await areaService.unassignTeamFromArea(areaId, teamId); |
| 34 | + dispatch(getAreas()); |
| 35 | + onClose(); |
| 36 | + toastr.success(intl.formatMessage({ id: "areas.details.teams.remove.success" }), ""); |
| 37 | + } catch (e: any) { |
| 38 | + const error = JSON.parse(e.message) as TErrorResponse; |
| 39 | + toastr.error( |
| 40 | + intl.formatMessage({ id: "areas.details.teams.remove.error" }), |
| 41 | + error?.errors?.length ? error.errors[0].detail : "" |
| 42 | + ); |
| 43 | + console.error(e); |
| 44 | + } |
| 45 | + setIsRemoving(false); |
| 46 | + }; |
| 47 | + |
| 48 | + return ( |
| 49 | + <Modal |
| 50 | + isOpen |
| 51 | + dismissible={false} |
| 52 | + title={"areas.details.teams.remove.title"} |
| 53 | + onClose={onClose} |
| 54 | + actions={[ |
| 55 | + { name: "common.confirm", onClick: removeTeam }, |
| 56 | + { name: "common.cancel", variant: "secondary", onClick: onClose } |
| 57 | + ]} |
| 58 | + > |
| 59 | + <Loader isLoading={isRemoving} /> |
| 60 | + <p> |
| 61 | + <FormattedMessage id="areas.details.teams.remove.body" /> |
| 62 | + </p> |
| 63 | + </Modal> |
| 64 | + ); |
| 65 | +}; |
| 66 | + |
| 67 | +export default RemoveTeamModal; |
0 commit comments