MapPage.tsx 5.18 KB
Newer Older
1
2
3
4
import * as React from 'react';
import {connect} from 'react-redux';
import {withStyles} from '@material-ui/core/styles';
import {bindActionCreators} from 'redux';
5
import { loadAccessionsMapInfo } from 'accessions/actions/public';
6
7
8
import AccessionFilter from 'model/AccessionFilter';
import Loading from 'ui/common/Loading';
import AccessionMapInfo from 'model/AccessionMapInfo';
9
import PageLayout from 'ui/layout/PageLayout';
10
import ContentHeader from 'ui/common/heading/ContentHeader';
11
12
import Button from '@material-ui/core/Button';
import Tabs, {Tab} from 'ui/common/Tabs';
13
import PrettyFilters from 'ui/common/filter/PrettyFilters';
14
15
16
17
18
19


let Map;
let TileLayer;

interface IMapPageProps extends React.ClassAttributes<any> {
20
    apiUrl: string;
21
    mapInfo: AccessionMapInfo;
22
23
24
    currentTab: string;
    classes: any;
    filterCode: string;
25
    loadAccessionsMapInfo: any;
26
27
28
29
30
31
32
33
34
35
36
37
}

const styles = (theme) => ({
    leafletContainer: {
        width: '100%',
        minHeight: '500px',
        height: 'calc(100vh - 186px)',
    },
});

class BrowsePage extends React.Component<IMapPageProps, any> {
    protected static needs = [
38
39
      ({ params: { filterCode } }) => {
        return loadAccessionsMapInfo(filterCode || '');
40
41
42
43
44
45
46
47
48
49
50
51
52
      },
    ];

    constructor(props, context) {
        super(props, context);
        if (typeof window !== 'undefined') {
            Map = require('react-leaflet').Map;
            TileLayer = require('react-leaflet').TileLayer;
        }
    }


    public componentWillMount() {
53
54
55
56
57
58
59
      const { mapInfo, filterCode, loadAccessionsMapInfo } = this.props;
      // console.log(`Filter code for map ${filterCode} ?== ${mapInfo && mapInfo.filterCode}`, filterCode, mapInfo ? mapInfo.filterCode : 'No mapInfo');
      if (mapInfo && mapInfo.filterCode !== filterCode) {
        // console.log(`mapInfo.filterCode !== filterCode. updatingRoute`);
        loadAccessionsMapInfo(filterCode || '');
      } else if (!mapInfo) {
        loadAccessionsMapInfo(filterCode || '');
60
61
62
      }
    }

63
64
65
66
67
    /// Wrap loadAccessionsMapInfo dispatch and fills the current sort selection
    protected myApplyFilters = (filters: AccessionFilter) => {
      const { loadAccessionsMapInfo } = this.props;

      loadAccessionsMapInfo(filters);
68
69
70
71
72
    }

    public render() {
        const position = [30, 0];

73
74
75
76
77
        const { mapInfo, currentTab, classes, filterCode } = this.props;

        if (! mapInfo) {
          return <Loading />;
        }
78
79

        // const color = 'f00ba0';
80
        const layerUrl = `{s}/acn/tile/{z}/{x}/{y}?f=${filterCode ? filterCode : ''}`; // `&color=${color}`;
81
82
83
84
85
86
87
88

        return (
            <PageLayout>
                <ContentHeader title="Accession map" subTitle="Explore accession localities"/>
                <Tabs
                    tab={ currentTab }
                    actions={
                        <span>
Matija Obreza's avatar
Matija Obreza committed
89
90
91
92
                          <form method="post" action="/proxy/api/v1/acn/downloadKml">
                            <input type="hidden" name="f" value={ filterCode } />
                            <Button type="submit">Download KML</Button>
                          </form>
93
94
95
                        </span>
                    }
                >
96
97
98
                    <Tab name="data" to={ `/a/${filterCode || ''}` }>Accessions</Tab>
                    <Tab name="overview" to={ `/a/overview/${filterCode || ''}` }>Overview</Tab>
                    <Tab name="map" to={ `/a/map/${filterCode || '' }` }>Map</Tab>
99
100
101
                </Tabs>
                <PrettyFilters
                    prefix="accessions"
102
                    filterObj={ mapInfo && mapInfo.filter || {} }
103
104
105
                    onSubmit={ this.myApplyFilters }
                />
                <div className={ classes.leafletContainer }>
106
                { mapInfo && typeof window !== 'undefined' &&
107
108
                    <Map
                        center={ position }
109
110
                        zoom={ 3 } minZoom={ 2 } maxZoom={ 14 }
                        bounds={ mapInfo.bounds }>
111
112
113
114
115
116
117
118
119
120
                        <TileLayer
                            opacity={ 0.50 }
                            attribution={ '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' }
                            url={ 'https://tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png' }
                        />
                        <TileLayer
                            updateInterval={ 1000 }
                            updateWhenZooming={ false }
                            attribution="&amp;copy Accession localities from <a href=&quot;/&quot;>Genesys PGR</a>"
                            url={ layerUrl }
121
                            subdomains={ mapInfo.tileServers }
122
123
124
125
126
127
128
129
130
131
                        />
                    </Map>
                }
                </div>
            </PageLayout>
        );
    }
}

const mapStateToProps = (state, ownProps) => ({
132
    mapInfo: state.accessions.public.mapInfo || undefined,
133
134
135
136
137
    filterCode: ownProps.match.params.filterCode || '',
    currentTab: ownProps.match.params.tab || 'map', // current tab, or ownProps.location.pathname
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
138
    loadAccessionsMapInfo,
139
140
141
}, dispatch);

export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(BrowsePage));