|
| 1 | +import React, { useState } from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import { withRouter } from 'react-router-dom' |
| 4 | +import Dropdown from 'rc-dropdown' |
| 5 | +import styles from './Cancel-DropDown.module.scss' |
| 6 | +import { CANCEL_REASONS } from '../../../config/constants' |
| 7 | +import cn from 'classnames' |
| 8 | +import 'rc-dropdown/assets/index.css' |
| 9 | +import { PrimaryButton } from '../../Buttons' |
| 10 | +import ConfirmationModal from '../../Modal/ConfirmationModal' |
| 11 | +import { patchChallenge } from '../../../services/challenges' |
| 12 | +import _ from 'lodash' |
| 13 | + |
| 14 | +const theme = { |
| 15 | + container: styles.modalContainer |
| 16 | +} |
| 17 | +const CancelDropDown = ({ challenge, history }) => { |
| 18 | + const [cancelReason, setCancelReason] = useState('') |
| 19 | + const [showModal, setShowModal] = useState(false) |
| 20 | + |
| 21 | + const onSelect = v => { |
| 22 | + setCancelReason(v) |
| 23 | + setShowModal(true) |
| 24 | + } |
| 25 | + const onConfirm = async () => { |
| 26 | + await patchChallenge(challenge.id, { |
| 27 | + status: cancelReason |
| 28 | + }) |
| 29 | + |
| 30 | + history.push(`/projects/${challenge.projectId}/challenges`) |
| 31 | + } |
| 32 | + |
| 33 | + const menu = ( |
| 34 | + <div className={cn(styles['menus'])}> |
| 35 | + {_.map(CANCEL_REASONS, r => { |
| 36 | + return ( |
| 37 | + <div |
| 38 | + className={styles.menu} |
| 39 | + onClick={() => { |
| 40 | + onSelect(r) |
| 41 | + }} |
| 42 | + > |
| 43 | + {r} |
| 44 | + </div> |
| 45 | + ) |
| 46 | + })} |
| 47 | + </div> |
| 48 | + ) |
| 49 | + |
| 50 | + return ( |
| 51 | + <> |
| 52 | + <Dropdown trigger={['click']} overlay={menu} animation='slide-up'> |
| 53 | + <PrimaryButton text={'Cancel'} type={'info'} /> |
| 54 | + </Dropdown> |
| 55 | + {showModal && ( |
| 56 | + <ConfirmationModal |
| 57 | + // title='Reminder' |
| 58 | + message={'Do you want to cancel the challenge ?'} |
| 59 | + theme={theme} |
| 60 | + cancelText='Cancel' |
| 61 | + confirmText='Continue' |
| 62 | + onCancel={() => { |
| 63 | + setShowModal(false) |
| 64 | + }} |
| 65 | + onConfirm={onConfirm} |
| 66 | + /> |
| 67 | + )} |
| 68 | + </> |
| 69 | + ) |
| 70 | +} |
| 71 | + |
| 72 | +CancelDropDown.defaultProps = {} |
| 73 | + |
| 74 | +CancelDropDown.propTypes = { |
| 75 | + challenge: PropTypes.shape().isRequired, |
| 76 | + history: PropTypes.shape() |
| 77 | +} |
| 78 | + |
| 79 | +export default withRouter(CancelDropDown) |
0 commit comments