MyDataPage.tsx 3.93 KB
Newer Older
Maxym Borodenko's avatar
Maxym Borodenko committed
1 2 3 4 5 6 7 8 9 10 11 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
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 {setPageTitle} from 'actions/pageTitle';

import ContentHeaderWithButton from 'ui/common/heading/ContentHeaderWithButton';
import BackButton from 'ui/common/buttons/BackButton';

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;
    setPageTitle: (title: string) => void;
}

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);
        const { title, setPageTitle } = this.props;
        setPageTitle(title);
    }

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

        return (
            <div>
                <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
    filter: state.filterCode.filters && parse(ownProps.location.search).filter && state.filterCode.filters[parse(ownProps.location.search).filter] || null,
});

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

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

export {MyDataPage as default, BaseMyDataPage};