index.tsx 3.43 KB
Newer Older
1
2
3
4
import * as React from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';

5
6
import {Location} from 'model/location.model';
import {Dataset} from 'model/dataset.model';
7

Matija Obreza's avatar
Matija Obreza committed
8
9
10
import {createLocationRequest, deleteLocationRequest, updateLocationRequest} from 'actions/dataset';

import TimingAndLocationForm from './TimingAndLocationForm';
Maxym Borodenko's avatar
Maxym Borodenko committed
11
import steps from '../../steps';
12
import NavigationWrapper from 'ui/common/stepper/NavigationWrapper';
Maxym Borodenko's avatar
Maxym Borodenko committed
13
14
15
import {getFormValues, change} from 'redux-form';
import {TIMING_AND_LOCATION_FORM} from 'constants/datasets';
import * as _ from 'lodash';
Matija Obreza's avatar
Matija Obreza committed
16

17
interface ITimingAndLocationStepProps extends React.ClassAttributes<any> {
18
    dataset: Dataset;
19
    createLocationRequest: (datasetUUID: string) => Promise<Location>;
20
    updateLocationRequest: (datasetUUID: string, location: Location) => Promise<Location>;
21
    deleteLocationRequest: (datasetUUID: string, location: Location) => Promise<Location>;
Maxym Borodenko's avatar
Maxym Borodenko committed
22
23
24
25
26
27
28
    stillLoading: boolean;
    onDelete: () => void;
    onPublish: () => void;
    onGotoStep: (id: number) => () => void;
    location: any;
    formValues: any;
    change: any;
29
30
31
32
}

class TimingAndLocationStep extends React.Component<ITimingAndLocationStepProps, any> {

Maxym Borodenko's avatar
Maxym Borodenko committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    protected gotoStep = (id) => () => {
        this.updateIfNeed();
        this.props.onGotoStep(id);
    }

    protected createLocation = (datasetUUID: string) => {
        this.updateIfNeed();
        this.props.createLocationRequest(datasetUUID);
    }

    protected changeFieldValue = (field, value) => {
        this.props.change(TIMING_AND_LOCATION_FORM, field, value);
    }

    private updateIfNeed = () => {
        const { dataset, formValues : { locations }, updateLocationRequest } = this.props;
        if (dataset) {
            dataset.locations.map((oldLocation) => {
                const updatedLocation = locations.find((l) => l.uuid === oldLocation.uuid);
                if (!_.isEqual({...updatedLocation}, {...oldLocation})) {
                    updateLocationRequest(dataset.uuid, {...oldLocation, ...updatedLocation});
                }
            });
        }
    }

59
    public render() {
60
        const {dataset, deleteLocationRequest, stillLoading, onPublish, onDelete, location} = this.props;
61
62

        return (
63
            <NavigationWrapper location={ location } stillLoading={ stillLoading }
Maxym Borodenko's avatar
Maxym Borodenko committed
64
65
66
67
68
69
70
71
72
                disabledNext={ false } disabled={ false } steps={ steps }
                gotoStep={ this.gotoStep } onDelete={ onDelete } onPublish={ onPublish }>
                <TimingAndLocationForm
                    dataset={ dataset }
                    initialValues={ dataset }
                    createLocation={ this.createLocation }
                    deleteLocation={ deleteLocationRequest }
                    changeFieldValue={ this.changeFieldValue }
                />
73
            </NavigationWrapper>
74
        );
Matija Obreza's avatar
Matija Obreza committed
75
    }
76
77
78
}

const mapStateToProps = (state, ownProps) => ({
Matija Obreza's avatar
Matija Obreza committed
79
    dataset: state.datasets.currentDataset,
Maxym Borodenko's avatar
Maxym Borodenko committed
80
81
82
83
84
85
    stillLoading: ownProps.stillLoading,
    location: ownProps.location,
    onDelete: ownProps.onDelete,
    onGotoStep: ownProps.onGotoStep,
    onPublish: ownProps.onPublish,
    formValues: getFormValues(TIMING_AND_LOCATION_FORM)(state),
86
87
88
89
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
    createLocationRequest,
90
    updateLocationRequest,
91
    deleteLocationRequest,
Maxym Borodenko's avatar
Maxym Borodenko committed
92
    change,
93
94
95
96
97
}, dispatch);

export default connect(
    mapStateToProps, mapDispatchToProps,
)(TimingAndLocationStep);