index.tsx 3.85 KB
Newer Older
1
import * as React from 'react';
Maxym Borodenko's avatar
Maxym Borodenko committed
2
3
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
4
5
import Grid from 'material-ui/Grid';

Maxym Borodenko's avatar
Maxym Borodenko committed
6
import { log } from 'utilities/debug';
Maxym Borodenko's avatar
Maxym Borodenko committed
7
import confirm from 'utilities/confirmAlert';
Valeriy Panov's avatar
Valeriy Panov committed
8

9
10
import TopSection from 'ui/common/stepper/TopSection';
import BottomSection from 'ui/common/stepper/BottomSection';
Maxym Borodenko's avatar
Maxym Borodenko committed
11
12
13
14
import { navigateTo } from 'actions/navigation';
import { setPageTitle } from 'actions/pageTitle';
import { loadDataset, publishDataset, deleteDataset } from 'actions/dataset';
import { Dataset } from 'model/dataset.model';
Maxym Borodenko's avatar
Maxym Borodenko committed
15
import renderRoutes from 'ui/renderRoutes';
Maxym Borodenko's avatar
Maxym Borodenko committed
16
import steps from './steps';
Matija Obreza's avatar
Matija Obreza committed
17

18
19
interface IDatasetProps extends React.ClassAttributes<any> {
    classes: any;
Valeriy Panov's avatar
Valeriy Panov committed
20
    location: any;
Matija Obreza's avatar
Matija Obreza committed
21
22

    uuid: string;
Maxym Borodenko's avatar
Maxym Borodenko committed
23
    pageTitle: string;
Matija Obreza's avatar
Matija Obreza committed
24
25
26
    dataset: Dataset;
    loadDataset: (uuid: string) => Promise<Dataset>;
    publishDataset: (dataset: Dataset) => void;
Maxym Borodenko's avatar
Maxym Borodenko committed
27
    deleteDataset: (dataset: Dataset) => Promise<Dataset>;
Matija Obreza's avatar
Matija Obreza committed
28
29

    navigateTo: (path: string) => void;
Maxym Borodenko's avatar
Maxym Borodenko committed
30
    setPageTitle: (title: string) => void;
Maxym Borodenko's avatar
Maxym Borodenko committed
31
    route: any;
Maxym Borodenko's avatar
Maxym Borodenko committed
32
    match: any;
33
34
35
36
}

class DatasetStepper extends React.Component<IDatasetProps, any> {

Maxym Borodenko's avatar
Maxym Borodenko committed
37
38
39
    protected static needs = [
        ({ params: { uuid } }) => loadDataset(uuid),
    ];
40

Maxym Borodenko's avatar
Maxym Borodenko committed
41
42
43
44
45
46
    public constructor(props: any) {
        super(props);
        const {pageTitle, setPageTitle} = this.props;
        setPageTitle(pageTitle);
    }

47
    public componentDidMount() {
Matija Obreza's avatar
Matija Obreza committed
48
        const {uuid, dataset, loadDataset} = this.props;
Matija Obreza's avatar
Matija Obreza committed
49
50

        if (uuid && (!dataset || dataset.uuid !== uuid)) {
Maxym Borodenko's avatar
Maxym Borodenko committed
51
52
            loadDataset(uuid);
        }
53
54
    }

Maxym Borodenko's avatar
Maxym Borodenko committed
55
    protected gotoStep = (id) => {
Matija Obreza's avatar
Matija Obreza committed
56
        const {dataset, uuid, navigateTo} = this.props;
Matija Obreza's avatar
Matija Obreza committed
57
        const path = steps.find((e) => e.id === id).link;
Valeriy Panov's avatar
Valeriy Panov committed
58
        log('Go to step', path);
59
60
61
62
63
        if (uuid || (dataset && dataset.uuid)) {
          navigateTo(`/datasets/${dataset ? dataset.uuid : uuid}/${path}`);
        } else {
          // no navigation!
        }
Matija Obreza's avatar
Matija Obreza committed
64
65
    }

Maxym Borodenko's avatar
Maxym Borodenko committed
66
67
68
69
    protected onDelete = (e) => {
        const {dataset, deleteDataset, navigateTo} = this.props;
        log('Removing dataset', dataset);
        confirm(<span>Delete <b>{ dataset.title }</b>?</span>, {
Maxym Borodenko's avatar
Maxym Borodenko committed
70
            description: `Deleting the dataset will remove all related data.`,
Maxym Borodenko's avatar
Maxym Borodenko committed
71
72
73
74
75
76
77
78
79
            confirmLabel: 'Delete',
            abortLabel: 'Cancel',
        }).then(() => {
            deleteDataset(dataset).then(() => navigateTo(`/dashboard`));
        }).catch(() => {
            // don't delete
        });
    }

80
81
    public onPublish = (e) => {
      const {dataset, publishDataset} = this.props;
Valeriy Panov's avatar
Valeriy Panov committed
82
      log('Publishing dataset', dataset);
83
84
85
      publishDataset(dataset);
    }

86
    public render() {
Maxym Borodenko's avatar
Maxym Borodenko committed
87
        const {uuid, pageTitle, route, match} = this.props;
Matija Obreza's avatar
Matija Obreza committed
88
89
        let {dataset} = this.props;

Matija Obreza's avatar
Matija Obreza committed
90
91
        const stillLoading: boolean = (! dataset || (uuid && (dataset.uuid !== uuid)));

Matija Obreza's avatar
Matija Obreza committed
92
93
94
95
96
        if (! dataset && ! uuid) {
          dataset = new Dataset();
        }

        if (! dataset) {
Valeriy Panov's avatar
Valeriy Panov committed
97
          log('No dataset.');
Matija Obreza's avatar
Matija Obreza committed
98
99
          return null;
        }
100

101
        return (
102
            <Grid container spacing={ 0 }>
103
                <TopSection pageTitle={ pageTitle } subTitle="Publish your datasets" />
Maxym Borodenko's avatar
Maxym Borodenko committed
104
105
106
                {
                    renderRoutes(route.routes, match.path, { stillLoading, onGotoStep: this.gotoStep, onDelete: this.onDelete, onPublish: this.onPublish })
                }
107
                <BottomSection/>
Valeriy Panov's avatar
Valeriy Panov committed
108
            </Grid>
109
        );
Matija Obreza's avatar
Matija Obreza committed
110
    }
111
112
113
114

}

const mapStateToProps = (state, ownProps) => ({
Maxym Borodenko's avatar
Maxym Borodenko committed
115
    pageTitle: ownProps.route.extraProps.title, // route-configured
Maxym Borodenko's avatar
Maxym Borodenko committed
116
117
    location: ownProps.location,
    uuid: ownProps.match.params.uuid,
Matija Obreza's avatar
Matija Obreza committed
118
    dataset: state.datasets.currentDataset,
119
120
121
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
Matija Obreza's avatar
Matija Obreza committed
122
    loadDataset,
123
    publishDataset,
Maxym Borodenko's avatar
Maxym Borodenko committed
124
    deleteDataset,
Matija Obreza's avatar
Matija Obreza committed
125
    navigateTo,
Maxym Borodenko's avatar
Maxym Borodenko committed
126
    setPageTitle,
127
128
129
130
}, dispatch);

export default connect(
    mapStateToProps, mapDispatchToProps,
Maxim Babichev's avatar
Maxim Babichev committed
131
)(DatasetStepper);