import React from 'react'; import { connect } from 'react-redux'; import { Link } from 'react-router-dom'; import { Button, Divider, List, message, Popconfirm, Spin } from 'antd'; import Placeholder from 'components/Placeholder'; import { getIsVerified } from 'modules/auth/selectors'; import Loader from 'components/Loader'; import { CCRDraft, CCRSTATUS } from 'types'; import { createCCRDraft, deleteCCRDraft, fetchAndCreateCCRDrafts, } from 'modules/ccr/actions'; import { AppState } from 'store/reducers'; import './style.less'; interface StateProps { drafts: AppState['ccr']['drafts']; isFetchingDrafts: AppState['ccr']['isFetchingDrafts']; fetchDraftsError: AppState['ccr']['fetchDraftsError']; isCreatingDraft: AppState['ccr']['isCreatingDraft']; createDraftError: AppState['ccr']['createDraftError']; isDeletingDraft: AppState['ccr']['isDeletingDraft']; deleteDraftError: AppState['ccr']['deleteDraftError']; isVerified: ReturnType; } interface DispatchProps { createCCRDraft: typeof createCCRDraft; deleteCCRDraft: typeof deleteCCRDraft; fetchAndCreateCCRDrafts: typeof fetchAndCreateCCRDrafts; } interface OwnProps { createIfNone?: boolean; } type Props = StateProps & DispatchProps & OwnProps; interface State { deletingId: number | null; } class CCRDraftList extends React.Component { state: State = { deletingId: null, }; componentDidMount() { this.props.fetchAndCreateCCRDrafts(); } componentDidUpdate(prevProps: Props) { const { isDeletingDraft, deleteDraftError, createDraftError } = this.props; if (prevProps.isDeletingDraft && !isDeletingDraft) { this.setState({ deletingId: null }); } if (deleteDraftError && prevProps.deleteDraftError !== deleteDraftError) { message.error(deleteDraftError, 3); } if (createDraftError && prevProps.createDraftError !== createDraftError) { message.error(createDraftError, 3); } } render() { const { drafts, isCreatingDraft, isFetchingDrafts, isVerified } = this.props; const { deletingId } = this.state; if (!isVerified) { return (
); } if (!drafts || isCreatingDraft) { return ; } let draftsEl; if (drafts.length) { draftsEl = ( { const actions = [ Edit , this.deleteDraft(d.ccrId)} > Delete , ]; return ( {d.title || Untitled Request} {d.status === CCRSTATUS.REJECTED && (changes requested)} } description={d.brief || No description} /> ); }} /> ); } else { draftsEl = ( ); } return (

Your Request Drafts

{draftsEl} or
); } private createDraft = () => { this.props.createCCRDraft(); }; private deleteDraft = (ccrId: number) => { this.props.deleteCCRDraft(ccrId); this.setState({ deletingId: ccrId }); }; } export default connect( state => ({ drafts: state.ccr.drafts, isFetchingDrafts: state.ccr.isFetchingDrafts, fetchDraftsError: state.ccr.fetchDraftsError, isCreatingDraft: state.ccr.isCreatingDraft, createDraftError: state.ccr.createDraftError, isDeletingDraft: state.ccr.isDeletingDraft, deleteDraftError: state.ccr.deleteDraftError, isVerified: getIsVerified(state), }), { createCCRDraft, deleteCCRDraft, fetchAndCreateCCRDrafts, }, )(CCRDraftList);