Refactor: Reorganize state management into atoms and selectors directories
Moved all Recoil atoms to a new 'atoms' directory and selectors to a 'selectors' directory to clarify the project structure and improve maintainability. This change separates concerns by clearly distinguishing between simple state (atoms) and derived state (selectors), facilitating better understanding and scalability of the application's state management.
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
//components/DataSheet.js
|
//components/DataSheet.js
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil";
|
import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil";
|
||||||
import { gisStationsStaticDistrictState } from "../store/gisStationState";
|
import { gisStationsStaticDistrictState } from "../store/atoms/gisStationState";
|
||||||
import { gisSystemStaticState } from "../store/gisSystemState";
|
import { gisSystemStaticState } from "../store/atoms/gisSystemState";
|
||||||
import { mapLayersState } from "../store/mapLayersState";
|
import { mapLayersState } from "../store/atoms/mapLayersState";
|
||||||
import { selectedAreaState } from "../store/selectedAreaState";
|
import { selectedAreaState } from "../store/atoms/selectedAreaState";
|
||||||
import { zoomTriggerState } from "../store/zoomTriggerState";
|
import { zoomTriggerState } from "../store/atoms/zoomTriggerState";
|
||||||
|
|
||||||
function DataSheet() {
|
function DataSheet() {
|
||||||
const setSelectedArea = useSetRecoilState(selectedAreaState);
|
const setSelectedArea = useSetRecoilState(selectedAreaState);
|
||||||
|
|||||||
@@ -11,12 +11,12 @@ import "leaflet.smooth_marker_bouncing";
|
|||||||
import OverlappingMarkerSpiderfier from "overlapping-marker-spiderfier-leaflet";
|
import OverlappingMarkerSpiderfier from "overlapping-marker-spiderfier-leaflet";
|
||||||
import DataSheet from "./DataSheet.js";
|
import DataSheet from "./DataSheet.js";
|
||||||
import { useRecoilState, useRecoilValue, RecoilRoot } from "recoil";
|
import { useRecoilState, useRecoilValue, RecoilRoot } from "recoil";
|
||||||
import { gisStationsStaticDistrictState } from "../store/gisStationState.js";
|
import { gisStationsStaticDistrictState } from "../store/atoms/gisStationState.js";
|
||||||
import { gisSystemStaticState } from "../store/gisSystemState.js";
|
import { gisSystemStaticState } from "../store/atoms/gisSystemState.js";
|
||||||
import { mapLayersState } from "../store/mapLayersState.js";
|
import { mapLayersState } from "../store/atoms/mapLayersState.js";
|
||||||
import { selectedAreaState } from "../store/selectedAreaState.js";
|
import { selectedAreaState } from "../store/atoms/selectedAreaState.js";
|
||||||
import { zoomTriggerState } from "../store/zoomTriggerState.js";
|
import { zoomTriggerState } from "../store/atoms/zoomTriggerState.js";
|
||||||
import { poiTypState } from "../store/poiTypState.js";
|
import { poiTypState } from "../store/atoms/poiTypState.js";
|
||||||
import ShowAddStationPopup from "./ShowAddStationPopup";
|
import ShowAddStationPopup from "./ShowAddStationPopup";
|
||||||
//import { createRoot } from "react-dom/client";
|
//import { createRoot } from "react-dom/client";
|
||||||
|
|
||||||
@@ -415,7 +415,11 @@ const MapComponent = ({ locations, onLocationUpdate }) => {
|
|||||||
// Create a root container for the React component inside the popup
|
// Create a root container for the React component inside the popup
|
||||||
const root = ReactDOM.createRoot(container);
|
const root = ReactDOM.createRoot(container);
|
||||||
|
|
||||||
root.render(<ShowAddStationPopup map={initMap} latlng={e.latlng} />);
|
root.render(
|
||||||
|
<RecoilRoot>
|
||||||
|
<ShowAddStationPopup map={initMap} latlng={e.latlng} />
|
||||||
|
</RecoilRoot>
|
||||||
|
);
|
||||||
|
|
||||||
// Create and configure the popup
|
// Create and configure the popup
|
||||||
L.popup().setLatLng(e.latlng).setContent(container).openOn(initMap);
|
L.popup().setLatLng(e.latlng).setContent(container).openOn(initMap);
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
|
// components/ShowAddStationPopup.js
|
||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import ReactDOM from "react-dom";
|
import ReactDOM from "react-dom";
|
||||||
|
import { useRecoilValue } from "recoil";
|
||||||
|
import { loadDataStore } from "../store/selectors/loadDataStore";
|
||||||
const ShowAddStationPopup = ({ map, latlng }) => {
|
const ShowAddStationPopup = ({ map, latlng }) => {
|
||||||
|
const loadData = useRecoilValue(loadDataStore);
|
||||||
const [poiTypData2, setPoiTypData2] = useState(); // Recoil State verwenden
|
const [poiTypData2, setPoiTypData2] = useState(); // Recoil State verwenden
|
||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
const [poiTypeId, setPoiTypeId] = useState(""); // Initialize as string
|
const [poiTypeId, setPoiTypeId] = useState(""); // Initialize as string
|
||||||
@@ -29,7 +32,7 @@ const ShowAddStationPopup = ({ map, latlng }) => {
|
|||||||
fetchPoiTypData2();
|
fetchPoiTypData2();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleSubmit = (event) => {
|
const handleSubmit = async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const formData = {
|
const formData = {
|
||||||
name, // Name der Station
|
name, // Name der Station
|
||||||
@@ -46,6 +49,7 @@ const ShowAddStationPopup = ({ map, latlng }) => {
|
|||||||
.then((response) => response.json())
|
.then((response) => response.json())
|
||||||
.then((data) => console.log(data)) // Handle the response data
|
.then((data) => console.log(data)) // Handle the response data
|
||||||
.catch((error) => console.error(error)); // Handle any errors
|
.catch((error) => console.error(error)); // Handle any errors
|
||||||
|
await loadData();
|
||||||
|
|
||||||
// Check if map is not undefined and call closePopup
|
// Check if map is not undefined and call closePopup
|
||||||
if (map && typeof map.closePopup === "function") {
|
if (map && typeof map.closePopup === "function") {
|
||||||
|
|||||||
98
pages/index copy.js
Normal file
98
pages/index copy.js
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
// pages/index.js
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import dynamic from "next/dynamic";
|
||||||
|
import { useSetRecoilState } from "recoil";
|
||||||
|
import { loadDataStore } from "../store/selectors/loadDataStore";
|
||||||
|
const MapComponentWithNoSSR = dynamic(
|
||||||
|
() => import("../components/MapComponent"),
|
||||||
|
{ ssr: false }
|
||||||
|
);
|
||||||
|
|
||||||
|
export default function Home() {
|
||||||
|
const setLoadData = useSetRecoilState(loadDataStore);
|
||||||
|
const [mParam, setMParam] = useState([""]);
|
||||||
|
const [uParam, setUParam] = useState([""]);
|
||||||
|
|
||||||
|
const [locations, setLocations] = useState([]);
|
||||||
|
const [formData, setFormData] = useState({
|
||||||
|
name: "",
|
||||||
|
longitude: "",
|
||||||
|
latitude: "",
|
||||||
|
type: "",
|
||||||
|
});
|
||||||
|
|
||||||
|
const loadData = async () => {
|
||||||
|
const response = await fetch("/api/readLocations");
|
||||||
|
const data = await response.json();
|
||||||
|
setLocations(data);
|
||||||
|
};
|
||||||
|
useEffect(() => {
|
||||||
|
setLoadData(async () => {
|
||||||
|
const response = await fetch("/api/readLocations");
|
||||||
|
const data = await response.json();
|
||||||
|
setLocations(data); // Überlegungen für setLocations beachten
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Funktion, um URL-Parameter zu holen
|
||||||
|
function getURLParameter(name) {
|
||||||
|
// Nutze URLSearchParams, eine Web API für die Arbeit mit Query-Strings
|
||||||
|
const params = new URLSearchParams(window.location.search);
|
||||||
|
return params.get(name); // Holt den Wert des Parameternamens
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hole die Parameter 'm' und 'u'
|
||||||
|
setMParam(getURLParameter("m"));
|
||||||
|
setUParam(getURLParameter("u"));
|
||||||
|
|
||||||
|
// Logge die Werte in der Konsole
|
||||||
|
console.log(`Parameter m: ${mParam}, Parameter u: ${uParam}`);
|
||||||
|
loadData();
|
||||||
|
}, []);
|
||||||
|
const handleAddLocation = async (name, type, lat, lng) => {
|
||||||
|
const response = await fetch("/api/addLocation", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({
|
||||||
|
name,
|
||||||
|
type,
|
||||||
|
latitude: lat,
|
||||||
|
longitude: lng,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
console.log("Standort erfolgreich hinzugefügt");
|
||||||
|
setFormData({ name: "", longitude: "", latitude: "", type: "" }); // Formular zurücksetzen
|
||||||
|
loadData(); // Daten erneut laden
|
||||||
|
} else {
|
||||||
|
console.error("Fehler beim Hinzufügen des Standorts");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleLocationUpdate = (id, newLatitude, newLongitude) => {
|
||||||
|
setLocations((prevLocations) => {
|
||||||
|
return prevLocations.map((location) => {
|
||||||
|
if (location.idPoi === id) {
|
||||||
|
return {
|
||||||
|
...location,
|
||||||
|
// Hier musst du ggf. die Formatierung anpassen, je nachdem wie du die Koordinaten speicherst
|
||||||
|
position: `POINT(${newLongitude} ${newLatitude})`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return location;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{/* Ihr Formular */}
|
||||||
|
<MapComponentWithNoSSR
|
||||||
|
locations={locations}
|
||||||
|
onAddLocation={handleAddLocation}
|
||||||
|
onLocationUpdate={handleLocationUpdate}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,12 +1,15 @@
|
|||||||
// pages/index.js
|
// pages/index.js
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
|
import { useSetRecoilState } from "recoil";
|
||||||
|
import { loadDataStore } from "../store/selectors/loadDataStore";
|
||||||
const MapComponentWithNoSSR = dynamic(
|
const MapComponentWithNoSSR = dynamic(
|
||||||
() => import("../components/MapComponent"),
|
() => import("../components/MapComponent"),
|
||||||
{ ssr: false }
|
{ ssr: false }
|
||||||
);
|
);
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
|
const setLoadData = useSetRecoilState(loadDataStore);
|
||||||
const [mParam, setMParam] = useState([""]);
|
const [mParam, setMParam] = useState([""]);
|
||||||
const [uParam, setUParam] = useState([""]);
|
const [uParam, setUParam] = useState([""]);
|
||||||
|
|
||||||
@@ -23,6 +26,13 @@ export default function Home() {
|
|||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setLocations(data);
|
setLocations(data);
|
||||||
};
|
};
|
||||||
|
useEffect(() => {
|
||||||
|
setLoadData(async () => {
|
||||||
|
const response = await fetch("/api/readLocations");
|
||||||
|
const data = await response.json();
|
||||||
|
setLocations(data); // Überlegungen für setLocations beachten
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Funktion, um URL-Parameter zu holen
|
// Funktion, um URL-Parameter zu holen
|
||||||
@@ -60,27 +70,7 @@ export default function Home() {
|
|||||||
console.error("Fehler beim Hinzufügen des Standorts");
|
console.error("Fehler beim Hinzufügen des Standorts");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const handleSubmit = async (event) => {
|
|
||||||
event.preventDefault();
|
|
||||||
const response = await fetch("/api/addLocation", {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify(formData),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
console.log("Erfolg");
|
|
||||||
setFormData({ name: "", longitude: "", latitude: "", type: "" }); // Formular zurücksetzen
|
|
||||||
loadData(); // Daten erneut laden
|
|
||||||
} else {
|
|
||||||
console.error("Fehler beim Speichern der Daten");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleChange = (event) => {
|
|
||||||
const { name, value } = event.target;
|
|
||||||
setFormData((prevState) => ({ ...prevState, [name]: value }));
|
|
||||||
};
|
|
||||||
const handleLocationUpdate = (id, newLatitude, newLongitude) => {
|
const handleLocationUpdate = (id, newLatitude, newLongitude) => {
|
||||||
setLocations((prevLocations) => {
|
setLocations((prevLocations) => {
|
||||||
return prevLocations.map((location) => {
|
return prevLocations.map((location) => {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// Pfad: store/gisStationState.js
|
// Pfad: store/atoms/gisStationState.js
|
||||||
import { atom } from "recoil";
|
import { atom } from "recoil";
|
||||||
|
|
||||||
export const gisStationsStaticDistrictState = atom({
|
export const gisStationsStaticDistrictState = atom({
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
// Pfad: store/gisStationState.js
|
// Pfad: store/atoms/gisStationState.js
|
||||||
import { atom } from "recoil";
|
import { atom } from "recoil";
|
||||||
|
|
||||||
export const gisSystemStaticState = atom({
|
export const gisSystemStaticState = atom({
|
||||||
Reference in New Issue
Block a user