geomap.tsx 3.66 KB
Newer Older
Nicola Spanti's avatar
Nicola Spanti committed
1
2
declare const BASE_URL: string

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import React from "react";
import ReactDOM from "react-dom";

import EditorJsContainer from "react-editor-js";

import EditorJS from "@editorjs/editorjs";
import Embed from "@editorjs/embed";
import Table from "@editorjs/table";
import List from "@editorjs/list";
import Warning from "@editorjs/warning";
import Code from "@editorjs/code";
import LinkTool from "@editorjs/link";
import Image from "@editorjs/image";
import Raw from "@editorjs/raw";
import Header from "@editorjs/header";
import Quote from "@editorjs/quote";
import MarkerEdit from "@editorjs/marker";
import CheckList from "@editorjs/checklist";
import Delimiter from "@editorjs/delimiter";
import InlineCode from "@editorjs/inline-code";
import SimpleImage from "@editorjs/simple-image";

import { client } from "@logilab/cwclientlibjs";

27
28
29
30
31
import {
  ComposableMap,
  Geographies,
  Geography,
  Marker,
32
  Point,
33
34
} from "react-simple-maps";

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
export const EDITOR_JS_TOOLS = {
  embed: Embed,
  table: Table,
  marker: MarkerEdit,
  list: List,
  warning: Warning,
  code: Code,
  linkTool: LinkTool,
  image: Image,
  raw: Raw,
  header: Header,
  quote: Quote,
  checklist: CheckList,
  delimiter: Delimiter,
  inlineCode: InlineCode,
  simpleImage: SimpleImage,
};

const geoUrl =
  "https://raw.githubusercontent.com/zcreativelabs/react-simple-maps/master/topojson-maps/world-110m.json";
55
56

declare const data: {
57
58
59
60
61
62
  name: string;
  latitude: number;
  longitude: number;
  eid: number;
  description: any;
};
63
64
65
66
67
68

const MapChart = () => {
  return (
    <ComposableMap>
      <Geographies geography={geoUrl}>
        {({ geographies }) =>
69
70
71
72
73
74
75
76
          geographies.map((geo) => (
            <Geography
              key={geo.rsmKey}
              geography={geo}
              fill="#EAEAEC"
              stroke="#D6D6DA"
            />
          ))
77
78
        }
      </Geographies>
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
      <Marker coordinates={[data.longitude, data.latitude] as Point}>
        <g
          fill="none"
          stroke="#FF5533"
          strokeWidth="2"
          strokeLinecap="round"
          strokeLinejoin="round"
          transform="translate(-12, -24)"
        >
          <circle cx="12" cy="10" r="3" />
          <path d="M12 21.7C17.3 17 20 13 20 10a8 8 0 1 0-16 0c0 3 2.7 6.9 8 11.7z" />
        </g>
        <text
          textAnchor="middle"
          y={10}
          style={{ fontFamily: "system-ui", fill: "#5D5A6D" }}
        >
          {data.name}
        </text>
      </Marker>
99
100
101
102
103
    </ComposableMap>
  );
};

function App() {
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
140
141
  // XX to change with some data attribut
  const rqlClient = new client.CwRqlClient(
    new client.CwSimpleHttpClient("http://localhost:8080", true)
  );
  const instanceRef = React.useRef<EditorJS | null>(null);

  const [editorData, setEditorData] = React.useState<any | null>(null);
  async function handleSave() {
    if (instanceRef.current !== null) {
      const savedData = await instanceRef.current.save();
      rqlClient.queryRows(
        `Set M description "${JSON.stringify(savedData).replaceAll(
          '"',
          '\\"'
        )}" Where M is Museum, M eid ${data.eid}`
      );
    }
  }
  React.useEffect(() => {
    rqlClient
      .queryRows(`Any D Where M is Museum, M eid ${data.eid}, M description D`)
      .then((response) => {
        setEditorData(response[0][0]);
      });
  }, []);

  return (
    <>
      <EditorJsContainer
        tools={EDITOR_JS_TOOLS}
        data={JSON.parse(data.description)}
        instanceRef={(instance) => (instanceRef.current = instance)}
        readOnly={false}
      />
      <button onClick={handleSave}>SAVE THIS ! </button>
      <MapChart />
    </>
  );
142
143
144
}

const root = document.getElementById("awesome-map");
145
ReactDOM.render(<App />, root);