import React, { RefObject } from 'react' import { connect, ConnectedProps } from 'react-redux' import { t } from 'ttag' import ConfigurationStep from './ConfigurationStep' import SaveModal from './SaveModal' import ModalCard from '../components/ModalCard' import Dropdown from '../components/Dropdown' import Map from './Map' import type { CustomFunction } from '../reducers/customFunctions' import { setError } from '../actions/error' import { runJob } from '../actions/job' import { showSaveModal } from '../actions/saves' import { createReport, runTIFJob } from '../actions/report' import { reports } from '../config' const configurationCanRun = ({ point, variables, traits, customMode, customFunctions, }: { point: any variables: any[] traits: any[] customMode: boolean customFunctions: CustomFunction[] }) => { if (point === null || point.x === null || point.y === null) { return false } if (customMode && variables.length > 0 && variables.some(item => item.customCenter === null)) { return false } const variablesComplete = variables.length > 0 && variables.every(item => item.value !== null && item.isFetching === false) const traitsComplete = traits.length > 0 && traits.every(item => item.value !== null) const customFunctionsComplete = customFunctions.length > 0 && customFunctions.some(cf => cf.selected) && customFunctions.every(cf => (cf.selected ? cf.value && cf.transfer : true)) return variablesComplete || traitsComplete || customFunctionsComplete } const connector = connect( ({ runConfiguration, lastRun, job, auth, reportIsFetching, }: { runConfiguration: any lastRun: any job: any auth: { isLoggedIn: boolean } reportIsFetching: boolean }) => { const { isLoggedIn } = auth return { canRun: configurationCanRun(runConfiguration) && !job.isRunning, canSave: lastRun !== null, configuration: runConfiguration, job, isLoggedIn, reportIsFetching, } }, (dispatch: (event: any) => any) => ({ onRun: (configuration: any) => { const { variables, constraints } = configuration if (variables.some((item: any) => item.transfer === null)) { dispatch( setError( t`Configuration error`, t`Cannot calculate scores: one or more of your variables has no transfer limit, or a limit of 0.`, ), ) return } if (constraints.some((item: any) => Object.keys(item.values).some(key => item.values[key] === null))) { dispatch( setError( t`Configuration error`, t`Cannot calculate scores: one or more of your constraints is missing a value.`, ), ) return } dispatch(runJob(configuration)) }, onSave: (isLoggedIn: boolean) => { if (!isLoggedIn) { dispatch(setError(t`Login required`, t`Please login to save your run.`)) return } dispatch(showSaveModal()) }, onExport: (name: string) => { dispatch(createReport(name)) }, onExportTIF: () => { dispatch(runTIFJob()) }, }), ) type RunStepProps = ConnectedProps & { number: number } type RunStepState = { previewModal: boolean exportType: string | null processingCsv: boolean csvError: string | null } class RunStep extends React.Component { fileInputRef?: RefObject constructor(props: RunStepProps) { super(props) this.fileInputRef = React.createRef() this.state = { previewModal: false, exportType: null, processingCsv: false, csvError: null, } } render() { const { number, configuration, canRun, canSave, isLoggedIn, reportIsFetching, onRun, onSave, onExport, onExportTIF, } = this.props const { previewModal, processingCsv, csvError } = this.state return ( {(processingCsv || csvError) && ( <> ) } > {csvError ? (
{csvError}
) : ( <>
Uploading CSV data...
)}
)}
{reports.map(r => ( { e.preventDefault() this.setState({ previewModal: true, exportType: r.name, }) }} > {r.label} ))} { e.preventDefault() onExportTIF() }} > {t`GeoTIFF`} {previewModal ? ( { this.setState({ previewModal: false }) }} title={t`Position Map`} footer={ } >
{t`Move and zoom the map to set how it will appear in the report.`}
) : null}
) } } export default connector(RunStep)