import React, { useEffect, useState } from 'react'; import { Image, SafeAreaView, ScrollView, StatusBar, StyleSheet, Switch, View, Dimensions } from 'react-native'; import Mapbox, { Callout, Camera, MapView, PointAnnotation } from "@rnmapbox/maps"; import { useSelector } from 'react-redux'; import { useIsFocused } from '@react-navigation/core'; Mapbox.setAccessToken( "pk.eyJ1IjoibWFhcnRlbnZyOTgiLCJhIjoiY2x6ZDFqMGp1MGVyejJrczhqcXpvYm9iYiJ9.XvYcL62dWiJQiFmG6mOoug" ); import { ThemedText } from '@/lib/components/ThemedText'; import { ThemedView } from '@/lib/components/ThemedView'; import { Colors } from '@/lib/constants/Colors'; import { useColorScheme } from '@/lib/hooks/useColorScheme'; import List from '@/lib/components/List'; import { Request } from '@/lib/services/request'; export default function MapScreen() { const colorScheme = useColorScheme() ?? 'light'; const isFocused = useIsFocused(); const session = useSelector((state: any) => state.data.session); const [ types, setTypes ] = useState( [] ); const [ coordinates, setCoordinates ] = useState( [] ); const [ markers, setMarkers ] = useState( [] ); // Load session useEffect( () => { setCoordinates([session.coordinates.longitude, session.coordinates.latitude]); }, [session, isFocused]); // Load markers and types useEffect( () => { Mapbox.setTelemetryEnabled( false ); Request.get( 'locations' ).then( (response) => { const { locations, types } = response; // Set types const typesList: any[] = []; types.forEach( (type: any) => { typesList.push( { name: type.name, image: type.image, isEnabled: false, type: type.config_name, } ); } ) setTypes( typesList ); // Set markers setMarkers( locations ); } ) }, [] ); // Enable/disable type const toggleSwitch = (index: any) => { const newData = types.map( (item: any, key: any) => key === index ? { ...item, isEnabled: !item.isEnabled } : item ); setTypes( newData ); }; // Get all types that are active const getActiveTypes = (): Array => { const list: any[] = []; types.forEach( (type: any) => { if (type.isEnabled) { list.push( type.type ); } } ); return list; } // Get all markers that needs to be visible const activeMarkers = () => { return markers.filter( (marker: any) => { return getActiveTypes().includes( marker.waste_type ); } ); } return ( Afvalcontainers in de buurt {activeMarkers().map( (marker: any, index: any) => ( {marker.description + ' - ' + marker.street} ) )} ( {item.name} toggleSwitch( index )} /> )}/> ); } const styles = StyleSheet.create( { container: { marginTop: StatusBar.currentHeight, paddingTop: 25, flex: 1, }, titleContainer: { paddingLeft: 25, paddingRight: 25, flexDirection: 'row', alignItems: 'center', gap: 8, paddingBottom: 8, }, mapContainer: { marginTop: 15, height: Dimensions.get( 'window' ).height / 3, }, map: { width: Dimensions.get( 'window' ).width, height: Dimensions.get( 'window' ).height / 3, flex: 1, }, listContainer: { flex: 1, paddingTop: 20, paddingLeft: 25, paddingRight: 25, }, listItem: { flex: 1, display: 'flex', justifyContent: 'space-between', flexDirection: 'row', alignItems: 'center', paddingBottom: 20, marginBottom: 20, borderBottomWidth: 1, borderBottomColor: '#f2f2f2', }, listItemTitle: { flex: 1, flexDirection: 'row', alignItems: 'center', }, listImage: { width: 30, height: 30, borderRadius: 5, marginRight: 8, }, callout: { width: 200, padding: 5, }, } );