aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/CollectionMapView.tsx
blob: b67daeb534642804381e68b7de2cad1b9361d4cd (plain)
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
import { observer } from "mobx-react";
import { makeInterface } from "../../../new_fields/Schema";
import { documentSchema } from "../../../new_fields/documentSchemas";
import React = require("react");
import { Map, Marker, MapProps, GoogleApiWrapper } from "google-maps-react";
import { NumCast, StrCast } from "../../../new_fields/Types";
import { CollectionSubView } from "./CollectionSubView";
import { Utils } from "../../../Utils";
import { Opt } from "../../../new_fields/Doc";

type MapDocument = makeInterface<[typeof documentSchema]>;
const MapDocument = makeInterface(documentSchema);

export type LocationData = google.maps.LatLngLiteral & { address?: string };

@observer
class CollectionMapView extends CollectionSubView<MapDocument, Partial<MapProps> & { google: any }>(MapDocument) {

    render() {
        const { childLayoutPairs, props } = this;
        const { Document } = props;
        const center: LocationData = { lat: NumCast(Document.mapCenterLat), lng: NumCast(Document.mapCenterLng) };
        if (!center.lat) {
            center.lat = childLayoutPairs.length ? NumCast(childLayoutPairs[0].layout.locationLat, 0) : 0;
            center.lng = childLayoutPairs.length ? NumCast(childLayoutPairs[0].layout.locationLng, 0) : 0;
        }
        return (
            <div
                className={"collectionMapView-contents"}
            >
                <Map
                    {...props}
                    zoom={NumCast(Document.zoom, 10)}
                    center={center}
                    initialCenter={center}
                >
                    {childLayoutPairs.map(({ layout }) => {
                        const location: LocationData = {
                            lat: NumCast(childLayoutPairs[0].layout.locationLat, 0),
                            lng: NumCast(childLayoutPairs[0].layout.locationLng, 0)
                        };
                        let icon: Opt<google.maps.Icon>, iconUrl: Opt<string>;
                        if ((iconUrl = StrCast(Document.mapIconUrl, null))) {
                            const iconSize = new google.maps.Size(NumCast(layout.mapIconWidth, 45), NumCast(layout.mapIconHeight, 45));
                            icon = {
                                size: iconSize,
                                scaledSize: iconSize,
                                url: iconUrl
                            };
                        }
                        return (
                            <Marker
                                key={Utils.GenerateGuid()}
                                label={StrCast(layout.title)}
                                position={location}
                                onClick={() => {
                                    Document.mapCenterLat = location.lat;
                                    Document.mapCenterLng = location.lng;
                                }}
                                icon={icon}
                            />
                        );
                    })}
                </Map>
            </div>
        );
    }

}

export default GoogleApiWrapper({ apiKey: process.env.GOOGLE_MAPS! })(CollectionMapView) as any;