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


let Map;
let TileLayer;

interface IMapPageProps extends React.ClassAttributes<any> {
21
    apiUrl: string;
22
    mapInfo: AccessionMapInfo;
23
24
25
    currentTab: string;
    classes: any;
    filterCode: string;
26
    loadAccessionsMapInfo: any;
Viacheslav Pavlov's avatar
i18n    
Viacheslav Pavlov committed
27
    t: any;
28
29
30
31
32
33
34
35
36
37
38
39
}

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

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

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


    public componentWillMount() {
55
56
57
58
59
60
61
      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 || '');
62
63
64
      }
    }

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

      loadAccessionsMapInfo(filters);
70
71
72
73
74
    }

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

Viacheslav Pavlov's avatar
i18n    
Viacheslav Pavlov committed
75
        const { mapInfo, currentTab, classes, filterCode, t } = this.props;
76
77
78
79

        if (! mapInfo) {
          return <Loading />;
        }
80
81

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

        return (
Oleksii Savran's avatar
Oleksii Savran committed
85
            <PageLayout withFooter>
Matija Obreza's avatar
Matija Obreza committed
86
              <ContentHeader title={ t('accessions.public.p.browse.title') } subTitle={ t('accessions.public.p.browse.subTitle') } />
87
88
89
90
                <Tabs
                    tab={ currentTab }
                    actions={
                        <span>
Matija Obreza's avatar
Matija Obreza committed
91
92
93
94
                          <form method="post" action="/proxy/api/v1/acn/downloadKml">
                            <input type="hidden" name="f" value={ filterCode } />
                            <Button type="submit">Download KML</Button>
                          </form>
95
96
97
                        </span>
                    }
                >
Viacheslav Pavlov's avatar
i18n    
Viacheslav Pavlov committed
98
99
100
                  <Tab name="data" to={ `/a/${filterCode || ''}` }>{ t('accessions.tab.data') }</Tab>
                  <Tab name="overview" to={ `/a/overview/${filterCode || ''}` }>{ t('accessions.tab.overview') }</Tab>
                  <Tab name="map" to={ `/a/map/${filterCode || '' }` }>{ t('accessions.tab.map') }</Tab>
101
102
103
                </Tabs>
                <PrettyFilters
                    prefix="accessions"
104
                    filterObj={ mapInfo && mapInfo.filter || {} }
105
106
107
                    onSubmit={ this.myApplyFilters }
                />
                <div className={ classes.leafletContainer }>
108
                { mapInfo && typeof window !== 'undefined' &&
109
110
                    <Map
                        center={ position }
111
112
                        zoom={ 3 } minZoom={ 2 } maxZoom={ 14 }
                        bounds={ mapInfo.bounds }>
113
114
115
116
117
118
119
120
121
122
                        <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 }
123
                            subdomains={ mapInfo.tileServers }
124
125
126
127
128
129
130
131
132
133
                        />
                    </Map>
                }
                </div>
            </PageLayout>
        );
    }
}

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

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

Viacheslav Pavlov's avatar
i18n    
Viacheslav Pavlov committed
143
export default translate()(connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(BrowsePage)));