MapPage.tsx 5.36 KB
Newer Older
1
import * as React from 'react';
2
3
4
5
import { connect } from 'react-redux';
import { translate } from 'react-i18next';
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
import Button from '@material-ui/core/Button';
13
import Tabs, { Tab } from 'ui/common/Tabs';
14
import PrettyFilters from 'ui/common/filter/PrettyFilters';
15
16
17
18
19


let Map;
let TileLayer;

20
21
22
23
interface IMapPageProps {
    classes?: any;
    t?: any;

24
    apiUrl: string;
25
    mapInfo: AccessionMapInfo;
26
27
    currentTab: string;
    filterCode: string;
28
    loadAccessionsMapInfo: any;
29
30
31
32
33
34
35
36
37
38
39
40
}

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

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

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


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

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

      loadAccessionsMapInfo(filters);
71
72
73
74
75
    }

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

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

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

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

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

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

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

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