@vidro/map-handler 1.3.2 → 1.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/README.md +67 -1
  2. package/dist/map-handler.js +1 -1
  3. package/map-handler.d.ts +134 -0
  4. package/package.json +10 -4
  5. package/src/types.d.ts +63 -0
  6. package/.babelrc +0 -6
  7. package/doc/animation.png +0 -0
  8. package/doc/confirmComponent.png +0 -0
  9. package/doc/giswater.png +0 -0
  10. package/doc/giswaterInfo.png +0 -0
  11. package/doc/giswaterInfoApi.png +0 -0
  12. package/doc/giswatergeojson.png +0 -0
  13. package/doc/multiTile.png +0 -0
  14. package/doc/multiTileNoGutter.png +0 -0
  15. package/doc/togglelayergiswater.png +0 -0
  16. package/doc/vidromaps-basic.png +0 -0
  17. package/examples/full/apidemo.js +0 -387
  18. package/examples/full/cachedToken.dat +0 -1
  19. package/examples/full/cachedTokenData.dat +0 -1
  20. package/examples/full/docker/Docker_compose.yml +0 -14
  21. package/examples/full/docker/Dockerfile +0 -27
  22. package/examples/full/index.php +0 -200
  23. package/examples/full/storeToken.php +0 -6
  24. package/examples/full/tester.css +0 -74
  25. package/examples/full/tester.js +0 -658
  26. package/examples/multipleIframes/index.js +0 -82
  27. package/examples/multipleIframes/index.php +0 -52
  28. package/examples/react-next/README.md +0 -282
  29. package/examples/react-next/atoms/PrintLayoutSelector.js +0 -51
  30. package/examples/react-next/atoms/PrintPaperSizeSelector.js +0 -49
  31. package/examples/react-next/atoms/PrintScaleSelector.js +0 -61
  32. package/examples/react-next/atoms/ZoomToScaleButton.js +0 -57
  33. package/examples/react-next/components/AuthComponent.js +0 -88
  34. package/examples/react-next/components/MapButtons.js +0 -108
  35. package/examples/react-next/components/MapFilters.js +0 -120
  36. package/examples/react-next/components/MapIframe.js +0 -25
  37. package/examples/react-next/components/MapInfo.js +0 -36
  38. package/examples/react-next/components/MapLayers.js +0 -60
  39. package/examples/react-next/components/MapList.js +0 -51
  40. package/examples/react-next/components/MapPrint.js +0 -50
  41. package/examples/react-next/contexts/auth.js +0 -147
  42. package/examples/react-next/contexts/maps.js +0 -185
  43. package/examples/react-next/contexts/messages.js +0 -358
  44. package/examples/react-next/contexts/print.js +0 -125
  45. package/examples/react-next/env.sample +0 -3
  46. package/examples/react-next/eslint.config.mjs +0 -14
  47. package/examples/react-next/hooks/useMapEvents.js +0 -118
  48. package/examples/react-next/jsconfig.json +0 -7
  49. package/examples/react-next/next.config.mjs +0 -6
  50. package/examples/react-next/package.json +0 -25
  51. package/examples/react-next/pages/_app.js +0 -5
  52. package/examples/react-next/pages/index.js +0 -97
  53. package/examples/react-next/postcss.config.mjs +0 -8
  54. package/examples/react-next/public/discord.svg +0 -8
  55. package/examples/react-next/public/favicon.ico +0 -0
  56. package/examples/react-next/public/file.svg +0 -1
  57. package/examples/react-next/public/logo.png +0 -0
  58. package/examples/react-next/public/next.svg +0 -1
  59. package/examples/react-next/shared/constants.js +0 -48
  60. package/examples/react-next/shared/cookies.js +0 -23
  61. package/examples/react-next/styles/globals.css +0 -24
  62. package/examples/react-next/tailwind.config.mjs +0 -17
  63. package/examples/serverLess/dist/index.23420cfa.js +0 -2973
  64. package/examples/serverLess/dist/index.23420cfa.js.map +0 -1
  65. package/examples/serverLess/dist/index.91b6cacc.js +0 -2
  66. package/examples/serverLess/dist/index.91b6cacc.js.map +0 -1
  67. package/examples/serverLess/dist/index.html +0 -1
  68. package/examples/serverLess/index.html +0 -39
  69. package/examples/serverLess/main.js +0 -113
  70. package/examples/serverLess/package.json +0 -18
  71. package/examples/serverLess/readme.md +0 -41
  72. package/examples/simple/index.html +0 -23
  73. package/examples/simple/simple.js +0 -80
  74. package/examples/taigua/index.html +0 -55
  75. package/examples/taigua/main.js +0 -490
  76. package/examples/tester.css +0 -74
  77. package/examples/vidromap/index.js +0 -20
  78. package/examples/vidromap/index.php +0 -111
  79. package/flows.md +0 -73
  80. package/helpers.md +0 -45
  81. package/src/index.js +0 -882
  82. package/src/shared/iframe-communicator.js +0 -18
  83. package/webpack.config.js +0 -22
@@ -1,185 +0,0 @@
1
- "use client";
2
- import { createContext, useContext, useState, useEffect } from "react";
3
- import { useAuth } from "./auth";
4
- import {
5
- setMapId as setMapIdCookie,
6
- getMapId as getMapIdCookie,
7
- removeMapId as removeMapIdCookie,
8
- } from "@/shared/cookies";
9
- const MapsContext = createContext({});
10
-
11
- export const MapsProvider = ({ children }) => {
12
- const { apiUrl, token } = useAuth();
13
- const [map, setMap] = useState(null);
14
- const [mapId, setMapId] = useState(null);
15
-
16
- const [mapError, setMapError] = useState(null);
17
- const [mapReady, setMapReady] = useState(false);
18
-
19
- const [sessionToken, setSessionToken] = useState(null);
20
- const [currentMapAction, setCurrentMapAction] = useState(null);
21
- const [clickedCoordinates, setClickedCoordinates] = useState(null);
22
- const [bboxCoordinates, setBboxcoordinates] = useState(null);
23
- const [zoomLevel, setZoomLevel] = useState(null);
24
- const [mapScale, setMapScale] = useState(null);
25
- const [mapResolution, setMapResolution] = useState(null);
26
- const [srid, setSrid] = useState(null);
27
-
28
- //geolocations
29
- const [geolocalizing, setGeolocalizing] = useState(false);
30
- const [geolocalizingstatus, setGeolocalizingStatus] = useState(null);
31
- const [userGeoPosition, setUserGeoPosition] = useState(null);
32
-
33
- //measure
34
- const [measureStatus, setMeasureStatus] = useState(null);
35
-
36
- //layers
37
- const [displayedLayers, setDisplayedLayers] = useState([]);
38
- const [activeLayer, setActiveLayer] = useState(null);
39
- const [mapLayers, setMapLayers] = useState(null);
40
-
41
- const [infoLayerSource, setInfoLayerSource] = useState([]); //layer from info is received
42
-
43
- //filters
44
- const [configuredFilters, setConfiguredFilters] = useState(null); //backoffice configured filters
45
- const [mapFilters, setMapfilters] = useState(null);
46
- const [filtersApplied, setFiltersApplied] = useState(false);
47
- const [activeFilters, setActiveFilters] = useState([]);
48
-
49
- //tools
50
- const [selectedTool, setSelectedTool] = useState(null);
51
-
52
- useEffect(() => {
53
- if (!token) return;
54
- (async () => {
55
- if (!getMapIdCookie()) {
56
- console.log("No map if cookie found");
57
-
58
- return;
59
- }
60
- console.log("Map id cookie found, getting map...");
61
- try {
62
- await GetMap(getMapIdCookie());
63
- } catch (error) {
64
- console.error("Error getting map from cookie", error);
65
- return;
66
- }
67
- })();
68
- }, [token]);
69
-
70
- const GetMap = async (id) => {
71
- let url = `${apiUrl}map/${id}`;
72
-
73
- try {
74
- const response = await fetch(url, {
75
- method: "GET",
76
- headers: {
77
- "Content-Type": "application/json",
78
- Authorization: `Bearer ${token}`,
79
- },
80
- });
81
- const data = await response.json();
82
- console.log("GetMap", data);
83
- setMapId(id);
84
- setMapError(false);
85
- setSessionToken(data.message.sessionToken);
86
- setMap(
87
- `${data.message.iframe}?sessionToken=${data.message.sessionToken}`
88
- );
89
- //store map id in cookie, to avoid losing it on page refresh
90
- setMapIdCookie(id);
91
-
92
- return;
93
- } catch (error) {
94
- console.error("Error fetching map:", error);
95
- setSessionToken(null);
96
- setMap(null);
97
- setMapId(null);
98
- removeMapIdCookie();
99
-
100
- throw error;
101
- }
102
- };
103
-
104
- const GetMapInfo = async (id) => {
105
- let url = `${apiUrl}map/detail/${id}`;
106
-
107
- try {
108
- const response = await fetch(url, {
109
- method: "GET",
110
- headers: {
111
- "Content-Type": "application/json",
112
- Authorization: `Bearer ${token}`,
113
- },
114
- });
115
- const data = await response.json();
116
- console.log("GetMapInfo", data);
117
- console.log("Layers", data.message.layers);
118
- setMapLayers(data.message.layers);
119
- setConfiguredFilters(data.message.layerFilters);
120
- return;
121
- } catch (error) {
122
- console.error("Error fetching map info:", error);
123
-
124
- setMapLayers(null);
125
- throw error;
126
- }
127
- };
128
- return (
129
- <MapsContext.Provider
130
- value={{
131
- mapId,
132
- setMapId,
133
- GetMap,
134
- map,
135
- sessionToken,
136
- mapError,
137
- mapReady,
138
- setMapReady,
139
- displayedLayers,
140
- setDisplayedLayers,
141
- activeLayer,
142
- setActiveLayer,
143
- currentMapAction,
144
- setCurrentMapAction,
145
- mapFilters,
146
- setMapfilters,
147
- activeFilters,
148
- setActiveFilters,
149
- configuredFilters,
150
- infoLayerSource,
151
- setInfoLayerSource,
152
- clickedCoordinates,
153
- setClickedCoordinates,
154
- bboxCoordinates,
155
- setBboxcoordinates,
156
- srid,
157
- setSrid,
158
- geolocalizing,
159
- setGeolocalizing,
160
- geolocalizingstatus,
161
- setGeolocalizingStatus,
162
- userGeoPosition,
163
- measureStatus,
164
- setMeasureStatus,
165
- selectedTool,
166
- setSelectedTool,
167
- filtersApplied,
168
- setFiltersApplied,
169
- zoomLevel,
170
- setZoomLevel,
171
- mapScale,
172
- setMapScale,
173
- setSessionToken,
174
- mapLayers,
175
- GetMapInfo,
176
- mapResolution,
177
- setMapResolution,
178
- }}
179
- >
180
- {children}
181
- </MapsContext.Provider>
182
- );
183
- };
184
-
185
- export const useMaps = () => useContext(MapsContext);
@@ -1,358 +0,0 @@
1
- import { createContext, useContext, useEffect, useState } from "react";
2
- import { MAP_EVENTS } from "@/shared/constants";
3
-
4
- const MessageContext = createContext({});
5
-
6
- export const MessageProvider = ({ children }) => {
7
- const [events, setEvents] = useState(false);
8
- const [communicator, setCommunicator] = useState(false);
9
- const [message, setMessage] = useState(null);
10
- const [messageQueue, setMessageQueue] = useState([]);
11
-
12
- useEffect(() => {
13
- if (!communicator) return;
14
- console.log("MapHandler is ready for use");
15
- }, [communicator]);
16
-
17
- const start = async (sessionToken) => {
18
- const { Communicator } = await import("@vidro/map-handler");
19
- const _communicator = new Communicator({ sessionToken });
20
- setCommunicator(_communicator);
21
- };
22
-
23
- const reloadLayers = () => {
24
- if (!communicator) return;
25
- communicator.reloadDisplayedLayers();
26
- };
27
-
28
- const ZoomIn = () => {
29
- console.log("ZoomIn");
30
- if (!communicator) return;
31
- console.log("ZoomIn 2");
32
- communicator.ZoomIn();
33
- };
34
-
35
- const zoomToExtent = () => {
36
- if (!communicator) return;
37
- communicator.zoomToExtent();
38
- };
39
-
40
- const ZoomOut = () => {
41
- if (!communicator) return;
42
- communicator.ZoomOut();
43
- };
44
-
45
- const Clear = () => {
46
- if (!communicator) return;
47
- communicator.clear();
48
- };
49
-
50
- const Info = (type = "geojson", layer, format = "json") => {
51
- const hitTolerance = 5;
52
- communicator.infoFromCoordinates(type, layer, hitTolerance, format);
53
- };
54
-
55
- const Geolocalize = (what) => {
56
- if (!communicator) return;
57
- communicator.Geolocalize(what);
58
- };
59
-
60
- const Filters = (filters) => {
61
- if (!communicator) return;
62
- console.log("messages - Filters", filters);
63
- communicator.setFilters(filters);
64
- };
65
-
66
- const RemoveElementFromMap = (id, layer) => {
67
- communicator.RemoveGeometry(id, layer);
68
- };
69
-
70
- const CancelAddGeom = () => {
71
- if (!communicator) return;
72
- console.log("CancelAddGeom");
73
- communicator.CancelAddGeom();
74
- };
75
-
76
- const Highlight = (item, center, animate, id, style, zoom = null) => {
77
- if (!communicator) return;
78
- console.log("Highlight", { item, center, animate, id, style, zoom });
79
- let options = {
80
- geom: item.geom,
81
- zoom,
82
- center,
83
- animate,
84
- data: item,
85
- style,
86
- };
87
- communicator.Highlight(options);
88
- };
89
-
90
- const RemoveGeometriesByProperty = (layer, property, value) => {
91
- if (!communicator) return;
92
- communicator.RemoveGeometriesByProperty(layer, property, value);
93
- };
94
-
95
- const UpdateGeometriesByProperty = (layer, property, value, style) => {
96
- if (!communicator) return;
97
- console.log("UpdateGeometriesByProperty", {
98
- layer,
99
- property,
100
- value,
101
- style,
102
- });
103
- communicator.UpdateGeometriesByProperty(layer, property, value, style);
104
- };
105
-
106
- const DrawGeometries = (geoms) => {
107
- if (!communicator) return;
108
- console.log("DrawGeometries", geoms);
109
- communicator.DrawGeometries(geoms);
110
- };
111
-
112
- const DrawGeometry = (geom, featureId, style) => {
113
- if (!communicator) return;
114
- console.log("DrawGeometry", { geom, featureId, style });
115
- communicator.DrawGeometries([{ geom, style, featureId, id: featureId }]);
116
- };
117
-
118
- const ToggleLayer = (layer, properties) => {
119
- if (!communicator) return;
120
- communicator.toggleLayer(layer, properties);
121
- };
122
-
123
- const removeLayer = (name) => {
124
- if (!communicator) return;
125
- console.log("removeLayer", { name });
126
- communicator.removeLayer(name);
127
- };
128
-
129
- const startDrawPolygon = () => {
130
- const options = {
131
- texts: {
132
- start: t({
133
- id: "COMMON.COMMON.CLICK_TO_START_DRAWING",
134
- }),
135
- continue: t({
136
- id: "COMMON.COMMON.CLICK_TO_CONTINUE_DRAWING_POLYGON",
137
- }),
138
- },
139
- style: {
140
- fill: "rgb(249, 34, 34,0.3)",
141
- stroke: "rgb(249, 34, 34)",
142
- },
143
- drawOnEnd: false,
144
- showConfirm: false,
145
- };
146
- communicator.AddGeom("Polygon", options);
147
- };
148
-
149
- const BBoxForClicks = (size) => {
150
- if (!communicator) return;
151
- communicator.setBboxSize(size);
152
- };
153
-
154
- const cancelMeasure = () => {
155
- if (!communicator) return;
156
- communicator.cancelMeasure();
157
- };
158
-
159
- const initMeasure = (type, startMsg, continueMsg) => {
160
- if (!communicator) return;
161
- communicator.cancelMeasure();
162
- communicator.initMeasure(type, startMsg, continueMsg);
163
- };
164
-
165
- const zoomToCoordinates = (coordinates, zoomLevel) => {
166
- if (!communicator) return;
167
- communicator.zoomToCoordinates(coordinates[0], coordinates[1], zoomLevel);
168
- };
169
-
170
- const zoomToGeometry = (geom, limits) => {
171
- if (!communicator) return;
172
- communicator.zoomToGeometry(geom, limits);
173
- };
174
-
175
- const centerMap = (coordinates) => {
176
- if (!communicator) return;
177
- communicator.CenterMap(coordinates[0], coordinates[1]);
178
- };
179
-
180
- const drawPoint = ({ drawOnEnd = false, showConfirm = false }) => {
181
- if (!communicator) return;
182
- communicator.AddGeom("Point", { drawOnEnd, showConfirm: true });
183
- };
184
-
185
- const addIcon = (icon, coordinates) => {
186
- communicator.addIcon({ icon, coordinates });
187
- };
188
-
189
- const zoomToScale = (scale) => {
190
- const allowedScales = [
191
- "1:100",
192
- "1:200",
193
- "1:400",
194
- "1:500",
195
- "1:1000",
196
- "1:2000",
197
- "1:5000",
198
- "1:10000",
199
- "1:50000",
200
- ];
201
- if (!allowedScales.includes(scale)) {
202
- console.error(
203
- `Invalid scale: ${scale}. Allowed values are: ${allowedScales.join(
204
- ", "
205
- )}`
206
- );
207
- return;
208
- }
209
- if (!communicator) return;
210
- console.log("zoomToScale", { scale });
211
- communicator.zoomToScale(scale);
212
- };
213
-
214
- //***** PRINT ******/
215
-
216
- const startPrint = (options) => {
217
- communicator.startPrint(options);
218
- };
219
- const cancelPrint = (options) => {
220
- communicator.cancelPrint(options);
221
- };
222
-
223
- const print = (options) => {
224
- communicator.print(options);
225
- };
226
- const onMapEvent = (data) => {
227
- console.log(`onMapEvent`, { type: data.type, data });
228
- setMessageQueue((prevQueue) => [...prevQueue, data]);
229
- };
230
-
231
- useEffect(() => {
232
- if (message) return;
233
- if (messageQueue.length === 0) {
234
- return;
235
- }
236
- setMessage(messageQueue[0]);
237
- setMessageQueue((prevQueue) => {
238
- return prevQueue.slice(1);
239
- });
240
- }, [messageQueue, message]);
241
-
242
- useEffect(() => {
243
- if (!communicator) return;
244
- communicator.on(MAP_EVENTS.ZOOM_CHANGE, onMapEvent);
245
- communicator.on(MAP_EVENTS.LOADED, onMapEvent);
246
- communicator.on(MAP_EVENTS.CAPABILITIES, onMapEvent);
247
- communicator.on(MAP_EVENTS.ERROR, onMapEvent);
248
- communicator.on(MAP_EVENTS.GEOLOCATION, onMapEvent);
249
- communicator.on(MAP_EVENTS.END_MEASURE, onMapEvent);
250
- communicator.on(MAP_EVENTS.START_MEASURE, onMapEvent);
251
- communicator.on(MAP_EVENTS.UNLOADED, onMapEvent);
252
- communicator.on(MAP_EVENTS.COORDINATES, onMapEvent);
253
- communicator.on(MAP_EVENTS.CENTER_CHANGE, onMapEvent);
254
- communicator.on(MAP_EVENTS.ACTIVE_LAYER, onMapEvent);
255
- communicator.on(MAP_EVENTS.WMS_LAYERS, onMapEvent);
256
- communicator.on(MAP_EVENTS.STATUS, onMapEvent);
257
- communicator.on(MAP_EVENTS.INFO, onMapEvent);
258
- communicator.on(MAP_EVENTS.GEOM_ADDED, onMapEvent);
259
- communicator.on(MAP_EVENTS.LAYERS, onMapEvent);
260
- communicator.on(MAP_EVENTS.VERSION, onMapEvent);
261
- communicator.on(MAP_EVENTS.SCREENSHOT, onMapEvent);
262
- communicator.on(MAP_EVENTS.PRINT, onMapEvent);
263
- return () => {
264
- if (!communicator) return;
265
- communicator.off(MAP_EVENTS.ZOOM_CHANGE, onMapEvent);
266
- communicator.off(MAP_EVENTS.LOADED, onMapEvent);
267
- communicator.off(MAP_EVENTS.CAPABILITIES, onMapEvent);
268
- communicator.off(MAP_EVENTS.ERROR, onMapEvent);
269
- communicator.off(MAP_EVENTS.GEOLOCATION, onMapEvent);
270
- communicator.off(MAP_EVENTS.END_MEASURE, onMapEvent);
271
- communicator.off(MAP_EVENTS.START_MEASURE, onMapEvent);
272
- communicator.off(MAP_EVENTS.UNLOADED, onMapEvent);
273
- communicator.off(MAP_EVENTS.COORDINATES, onMapEvent);
274
- communicator.off(MAP_EVENTS.CENTER_CHANGE, onMapEvent);
275
- communicator.off(MAP_EVENTS.ACTIVE_LAYER, onMapEvent);
276
- communicator.off(MAP_EVENTS.WMS_LAYERS, onMapEvent);
277
- communicator.off(MAP_EVENTS.STATUS, onMapEvent);
278
- communicator.off(MAP_EVENTS.INFO, onMapEvent);
279
- communicator.off(MAP_EVENTS.LAYERS, onMapEvent);
280
- communicator.off(MAP_EVENTS.VERSION, onMapEvent);
281
- communicator.off(MAP_EVENTS.SCREENSHOT, onMapEvent);
282
- communicator.off(MAP_EVENTS.PRINT, onMapEvent);
283
- setCommunicator(null);
284
- };
285
- }, [communicator, events]);
286
-
287
- useEffect(() => {
288
- // Clean up
289
- return function cleanup() {
290
- if (communicator) {
291
- communicator.off(MAP_EVENTS.ZOOM_CHANGE, onMapEvent);
292
- communicator.off(MAP_EVENTS.LOADED, onMapEvent);
293
- communicator.off(MAP_EVENTS.CAPABILITIES, onMapEvent);
294
- communicator.off(MAP_EVENTS.ERROR, onMapEvent);
295
- communicator.off(MAP_EVENTS.GEOLOCATION, onMapEvent);
296
- communicator.off(MAP_EVENTS.END_MEASURE, onMapEvent);
297
- communicator.off(MAP_EVENTS.START_MEASURE, onMapEvent);
298
- communicator.off(MAP_EVENTS.UNLOADED, onMapEvent);
299
- communicator.off(MAP_EVENTS.COORDINATES, onMapEvent);
300
- communicator.off(MAP_EVENTS.CENTER_CHANGE, onMapEvent);
301
- communicator.off(MAP_EVENTS.ACTIVE_LAYER, onMapEvent);
302
- communicator.off(MAP_EVENTS.WMS_LAYERS, onMapEvent);
303
- communicator.off(MAP_EVENTS.STATUS, onMapEvent);
304
- communicator.off(MAP_EVENTS.INFO, onMapEvent);
305
- communicator.off(MAP_EVENTS.VERSION, onMapEvent);
306
- communicator.off(MAP_EVENTS.PRINT, onMapEvent);
307
- }
308
- setMessage(null);
309
- setEvents(false);
310
- };
311
- }, []);
312
-
313
- return (
314
- <MessageContext.Provider
315
- value={{
316
- communicator,
317
- start,
318
- ZoomIn,
319
- ZoomOut,
320
- zoomToExtent,
321
- DrawGeometry,
322
- DrawGeometries,
323
-
324
- startDrawPolygon,
325
- CancelAddGeom,
326
- Clear,
327
- Info,
328
- RemoveElementFromMap,
329
- Highlight,
330
- RemoveGeometriesByProperty,
331
- UpdateGeometriesByProperty,
332
- ToggleLayer,
333
- message,
334
- setMessage,
335
- Filters,
336
- reloadLayers,
337
- BBoxForClicks,
338
- cancelMeasure,
339
- initMeasure,
340
- removeLayer,
341
- Geolocalize,
342
- zoomToCoordinates,
343
- zoomToGeometry,
344
- centerMap,
345
- drawPoint,
346
- addIcon,
347
- zoomToScale,
348
- print,
349
- startPrint,
350
- cancelPrint,
351
- }}
352
- >
353
- {children}
354
- </MessageContext.Provider>
355
- );
356
- };
357
-
358
- export const useMessages = () => useContext(MessageContext);
@@ -1,125 +0,0 @@
1
- "use client";
2
- import { createContext, useContext, useState, useEffect } from "react";
3
- import { useMessages } from "./messages";
4
- import { MAP_EVENTS } from "@/shared/constants";
5
- import { useMaps } from "./maps";
6
- import { useAuth } from "./auth";
7
-
8
- const PrintsContext = createContext({});
9
-
10
- export const PrintsProvider = ({ children }) => {
11
- const [paperSize, setPaperSize] = useState("A4");
12
- const [paperLayout, setPaperLayout] = useState("landscape");
13
- const [printEnabled, setPrintEnabled] = useState(null);
14
- const { mapId, displayedLayers, mapScale } = useMaps();
15
- const { apiUrl, token } = useAuth();
16
- const { startPrint: startPrintMessages, setMessage, message } = useMessages();
17
-
18
- useEffect(() => {
19
- if (!message) return;
20
-
21
- switch (message.type) {
22
- case MAP_EVENTS.PRINT:
23
- console.log("useMapEvents print event", message, message.content);
24
- if (message.content?.print === true) {
25
- setPrintEnabled(true);
26
- } else if (message.content?.print === false) {
27
- setPrintEnabled(false);
28
- }
29
- if (message.content?.file) {
30
- sendPrintToApi(message.content.file);
31
- setPrintEnabled(false);
32
- }
33
- setMessage(null);
34
- break;
35
- }
36
- }, [message]);
37
-
38
- const sendPrintToApi = async (file) => {
39
- const data = {
40
- scale: "1:100",
41
- template: "basic",
42
- content: file,
43
- format: paperSize,
44
- layout: paperLayout,
45
- displayedLayers,
46
- };
47
- try {
48
- // Create FormData object for the request payload
49
- const bodyFormData = new FormData();
50
- for (const [key, value] of Object.entries(data)) {
51
- bodyFormData.append(key, value);
52
- }
53
-
54
- // Set headers dynamically, including Authorization if available
55
- const headers = {};
56
-
57
- if (token) {
58
- headers.Authorization = `Bearer ${token}`;
59
- }
60
-
61
- // Send POST request using fetch
62
- const response = await fetch(`${apiUrl}/print/${mapId}`, {
63
- method: "POST",
64
- headers, // No need to set "Content-Type" for FormData (browser sets it automatically)
65
- body: bodyFormData,
66
- });
67
-
68
- // Check if the request was successful
69
- if (!response.ok) {
70
- throw new Error(`Failed to fetch: ${response.statusText}`);
71
- }
72
-
73
- // Get the response as a Blob (PDF)
74
- const pdfBlob = await response.blob();
75
-
76
- // Create a download link
77
- const link = document.createElement("a");
78
- link.href = URL.createObjectURL(pdfBlob);
79
- link.download = "myprint.pdf"; // Set filename
80
- document.body.appendChild(link);
81
- link.click(); // Trigger download
82
- document.body.removeChild(link);
83
-
84
- return true;
85
- } catch (error) {
86
- console.error("Error sending print request:", error);
87
- return false;
88
- }
89
- };
90
-
91
- useEffect(() => {
92
- if (!paperLayout) return;
93
- console.log("set paperLayout", paperLayout);
94
- }, [paperLayout]);
95
-
96
- useEffect(() => {
97
- if (!paperSize) return;
98
- console.log("set paperSize", paperSize);
99
- }, [paperSize]);
100
-
101
- const startPrint = () => {
102
- console.log("Start print");
103
- startPrintMessages({
104
- paperLayout: paperLayout ? paperLayout : "landscape",
105
- paperSize: paperSize ? paperSize : "A4",
106
- });
107
- };
108
-
109
- return (
110
- <PrintsContext.Provider
111
- value={{
112
- paperLayout,
113
- setPaperLayout,
114
- paperSize,
115
- setPaperSize,
116
- startPrint,
117
- printEnabled,
118
- }}
119
- >
120
- {children}
121
- </PrintsContext.Provider>
122
- );
123
- };
124
-
125
- export const usePrint = () => useContext(PrintsContext);
@@ -1,3 +0,0 @@
1
- NEXT_PUBLIC_APIURL="https://your api url"
2
- NEXT_PUBLIC_USER="your user"
3
- NEXT_PUBLIC_PWD="your password"
@@ -1,14 +0,0 @@
1
- import { dirname } from "path";
2
- import { fileURLToPath } from "url";
3
- import { FlatCompat } from "@eslint/eslintrc";
4
-
5
- const __filename = fileURLToPath(import.meta.url);
6
- const __dirname = dirname(__filename);
7
-
8
- const compat = new FlatCompat({
9
- baseDirectory: __dirname,
10
- });
11
-
12
- const eslintConfig = [...compat.extends("next/core-web-vitals")];
13
-
14
- export default eslintConfig;