MyDataPage.tsx 3.85 KB
Newer Older
Maxym Borodenko's avatar
Maxym Borodenko committed
1 2 3 4 5 6 7 8 9 10
import * as React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';

import { parse } from 'query-string';

import { filterCodeToUrl } from 'actions/filterCode';

import ContentHeaderWithButton from 'ui/common/heading/ContentHeaderWithButton';
import BackButton from 'ui/common/buttons/BackButton';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
11
import PageTitle from 'ui/common/PageTitle';
Maxym Borodenko's avatar
Maxym Borodenko committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

interface IDataPublishedContainerProps extends React.ClassAttributes<any> {
    title: string;
    tab?: string;
    pageCurrent: number;
    pageSize: number;
    pageSort?: string;
    pageDir?: string;
    filterCode?: string;
    filter: any;
    preFilter?: object;
    basePath: string;
    filterCodeToUrl: any;
}

class BaseMyDataPage<T> extends React.Component<T & IDataPublishedContainerProps & any, any> {

    // SSR only
    protected static needs = [
    ];

    constructor(props) {
        super(props);
        this.state = {tab: '', filterCode: props.filterCode};
    }

    public componentDidMount() {
        // noop
    }

    public componentWillMount() {
        // noop
    }

    public componentWillUnmount() {
        // noop
    }

    public componentWillReceiveProps(nextProps) {
        // const {filterCodeToUrl} = this.props;
        // const paged = this.getPaged(nextProps);
        //
        // if (paged) {
        //     filterCodeToUrl(paged.filterCode);
        // }
    }

    public componentWillUpdate(nextProps, nextState) {
        // noop
    }

    public componentDidUpdate() {
        // noop
    }

    protected getPaged(props) {
        // switch (props.tab) {
        //     case 'descriptorlists': return props.descriptorLists;
        //     case 'descriptors': return props.descriptors;
        //     case 'datasets':
        // default: return props.datasets;
    // }
    }

    protected onPaginationChange = (page, results, sortBy, dir) => {
        const { history, location } = this.props;
        const params = new URLSearchParams(location.search);
        params.set('p', page);
        params.set('l', results);
        if (sortBy) {
            params.set('s', sortBy);
        } else {
            params.delete('s');
        }
        if (dir) {
            params.set('d', dir);
        } else {
            params.delete('d');
        }

        location.search = params.toString();
        history.push(location);
    }
}

class DP extends BaseMyDataPage<any> {

    constructor(props) {
        super(props);
    }

    public render() {
        const {title} = this.props;

        return (
            <div>
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
108
                <PageTitle title={ title }/>
Maxym Borodenko's avatar
Maxym Borodenko committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
                <ContentHeaderWithButton title={ title } buttons={ <BackButton defaultTarget="/dashboard" defaultBackText="BACK TO DASHBOARD" preferDefaultTarget="true" /> }/>
            </div>
        );
    }
}

const mapStateToProps = (state, ownProps) => ({
    title: ownProps.route.extraProps.title || 'Data', // route-configured
    basePath: ownProps.route.extraProps.basePath, // route-configured
    preFilter: ownProps.route.extraProps.filter || {}, // route-configured
    tab: ownProps.match.params.tab || 'datasets', // current tab, or ownProps.location.pathname
    pageCurrent: +parse(ownProps.location.search).p || 0, // current page
    pageSize: +parse(ownProps.location.search).l || 20, // page size
    pageSort: parse(ownProps.location.search).s, // page sort
    pageDir: parse(ownProps.location.search).d, // page sort direction
    filterCode: parse(ownProps.location.search).filter, // filter code
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
125
    filter: state.filterCode.filters && parse(ownProps.location.search).filter && state.filterCode.filters[parse(ownProps.location.search).filter as string] || null,
Maxym Borodenko's avatar
Maxym Borodenko committed
126 127 128 129 130 131 132 133 134 135 136
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
    filterCodeToUrl,
}, dispatch);

const MyDataPage = connect(
    mapStateToProps, mapDispatchToProps,
)(DP);

export {MyDataPage as default, BaseMyDataPage};