import React from 'react' import { connect } from 'react-redux' import { bindActionCreators } from 'redux' import ChangeCase from 'change-case' import isEmpty from 'lodash/isEmpty'; import Admin from '../admin' import { fetchEntities } from '../actions/index' type VoidFunc = () => void interface ConfirmDeleteProps { closeAndFetch?: VoidFunc; closeModal?: VoidFunc; delete?: VoidFunc; entityDirectory?: string; entityId?: number; entityName?: string; fetchEntities: any; issues: string[]; message: any; skipCheck?: boolean; } interface ConfirmDeleteState { fetching: boolean; status: string; issues: any[]; message: any; } class ConfirmDelete extends React.Component { constructor(props) { super(props); this.state = { fetching: !this.props.skipCheck, status: (this.props.skipCheck ? 'confirm' : 'fetching'), issues: [], message: {} }; } componentDidMount() { if (!this.props.skipCheck) { this.props.fetchEntities({ directory: `check_delete/${this.props.entityDirectory}/${this.props.entityId}` }).then(() => { let issues = this.props.issues || []; let message = this.props.message || {}; let status = (!isEmpty(message) || issues.length > 0 ? 'issues' : 'confirm'); this.setState({ fetching: false, issues, message, status }); }); } } clickConfirm() { this.props.closeAndFetch(); this.props.delete(); } render() { let entityText = ChangeCase.lowerCase(ChangeCase.sentenceCase(this.props.entityName)); switch (this.state.status) { case 'fetching': return(
{ Admin.renderSpinner() }
); case 'issues': if (this.state.message.header) { return(

{ this.state.message.header }

{ this.state.message.subheader }

); } else { return(

You cannot delete this { entityText } because it is in use by the below:

{ this.state.issues.map((issue, index) => { return(

{ issue }

); }) }
); } case 'confirm': return(

Are you sure you want to permanently delete this { entityText }?

Deleting a { entityText } will erase ALL of its information and data
Yes No
); default: return null; } } } const mapStateToProps = (reducers, props) => { return reducers.standardReducer; }; function mapDispatchToProps(dispatch) { return bindActionCreators({ fetchEntities }, dispatch); } export default connect(mapStateToProps, mapDispatchToProps)(ConfirmDelete);