From bb708526497163f19dfc11ed90394e2e71d2ff22 Mon Sep 17 00:00:00 2001 From: GabrielTrettel Date: Mon, 5 Apr 2021 15:20:48 -0300 Subject: [PATCH] Refactoring MapMarkers to a new component --- src/app/components/MapMarkerList.js | 48 +++++++++++++++++++++++++++++ src/app/hooks/selectFromDB.js | 4 +++ src/app/screens/MapFeedScreen.js | 11 ++----- 3 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 src/app/components/MapMarkerList.js diff --git a/src/app/components/MapMarkerList.js b/src/app/components/MapMarkerList.js new file mode 100644 index 0000000..e59bfa4 --- /dev/null +++ b/src/app/components/MapMarkerList.js @@ -0,0 +1,48 @@ +import { StyleSheet, View } from "react-native"; +import React from "react"; +import useMarkers from "../hooks/selectFromDB"; +import MapMarker from "../components/MapMarker"; + +function isRequestedValue( + item, + renderRain, + renderFlood, + renderRiver, + renderPluviometer +) { + return ( + (item.name == "pluviometer" && renderPluviometer) || + (item.name == "rain" && renderRain) || + (item.name == "river" && renderRiver) || + (item.name == "flood" && renderFlood) + ); +} + +function MapMarkerList({ + reload = null, + renderRain = true, + renderFlood = true, + renderRiver = true, + renderPluviometer = true, +}) { + const markers = useMarkers(reload); + return ( + <> + {[...markers.markers] + .filter((item) => + isRequestedValue( + item, + renderRain, + renderFlood, + renderRiver, + renderPluviometer + ) + ) + .map(({ ID, ...val }) => { + return ; + })} + + ); +} + +export { MapMarkerList }; diff --git a/src/app/hooks/selectFromDB.js b/src/app/hooks/selectFromDB.js index 9e2f2ec..08d905c 100644 --- a/src/app/hooks/selectFromDB.js +++ b/src/app/hooks/selectFromDB.js @@ -53,6 +53,7 @@ function partsePluviometer(row) { // displacement += offset; const i = { ID: ++ID, + name: "pluviometer", title: "Pluviometro", coordinate: { latitude: row["Latitude"] + displacement, @@ -77,6 +78,7 @@ function parseFloodZones(row) { // displacement += offset; return { ID: ++ID, + name: "flood", title: row["Passable"] == 1 ? "Transponível" : "Intransponível", coordinate: { latitude: row["Latitude"], @@ -103,6 +105,7 @@ function parseRiverLevel(row) { const riverIdx = row["RiverIdx"]; return { ID: ++ID, + name: "river", title: "Rio " + riverLevel[riverIdx], coordinate: { latitude: row["Latitude"], @@ -135,6 +138,7 @@ function parseRainLevel(row) { return { ID: ++ID, + name: "rain", title: rainLevel[rainIdx], coordinate: { latitude: row["Latitude"], diff --git a/src/app/screens/MapFeedScreen.js b/src/app/screens/MapFeedScreen.js index a5cb1a2..fc77022 100644 --- a/src/app/screens/MapFeedScreen.js +++ b/src/app/screens/MapFeedScreen.js @@ -4,18 +4,15 @@ import MapView from "react-native-maps"; import colors from "../config/colors"; import { screen_width, screen_height } from "../config/dimensions"; -import useMarkers from "../hooks/selectFromDB"; -import MapMarker from "../components/MapMarker"; import attachFocusToQuery from "../hooks/useFocus"; import { CurrentLocationContext } from "../context/CurrentLocationContext"; import HeaderBarMenu from "../components/HeaderBarMenu"; +import { MapMarkerList } from "../components/MapMarkerList"; function MapFeedScreen(props) { const context = useContext(CurrentLocationContext); const location = context.currentCoordinates; - - const hasToQuery = attachFocusToQuery(); - const markers = useMarkers(hasToQuery); + const focusChanged = attachFocusToQuery(); const default_location = { latitude: -12.901799, @@ -44,9 +41,7 @@ function MapFeedScreen(props) { ...lat_long_delta, }} > - {[...markers.markers].map(({ ID, ...val }) => { - return ; - })} + );