All files / src/screens/home/MapView useMapViewController.tsx

50% Statements 4/8
100% Branches 0/0
25% Functions 1/4
50% Lines 4/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80                                                                2x 2x             2x                                                                 2x            
/**
 * @fileoverview Map view controller.
 * @module useMapViewController
 * @description Controller for the map view, it handles all the logic related to the map view component.
 * @requires react react-native
 * @requires MapView react-native-maps
 */
import { Ref, useContext, useRef } from 'react'
import { Alert } from 'react-native'
import MapView from 'react-native-maps'
import { AccountContext } from '@global/contexts/AccountProvider'
import { AccountType } from '@global/types/Account'
import { Place } from '@global/types/Places'
 
interface MapViewController {
  account: AccountType
  mapRef: Ref<MapView>
  onMarkerPress: (place: Place) => void
}
 
interface Props {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  navigation: any
}
 
/**
 * @function useMapViewController
 * @description Controller that handles the logic for the map view.
 * @param navigation Navigation object
 * @returns {MapViewController} Map view controller.
 */
export default function useMapViewController({ navigation }: Props): MapViewController {
  const mapRef = useRef(null)
  const { account } = useContext(AccountContext)
 
  /**
   * @function navigateToPlaceDescription
   * @description Navigates to the place description screen.
   * @param place Place to display
   */
  const navigateToPlaceDescription = (place: Place): void => {
    navigation.navigate('PlaceDescription', { place })
  }
 
  /**
   * @function onMarkerPress
   * @description Handles the marker press event.
   * @param place Place to display
   */
  function onMarkerPress(place: Place): void {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore: Object is possibly 'null'.
    mapRef.current?.animateToRegion(
      {
        latitude: place.address.latitude,
        longitude: place.address.longitude,
        latitudeDelta: 0.02,
        longitudeDelta: 0.02,
      },
      500
    )
    Alert.alert(place.name, place.shortDescription, [
      {
        text: 'Annuler',
        style: 'cancel',
      },
      {
        text: 'Voir',
        onPress: () => navigateToPlaceDescription(place),
      },
    ])
  }
 
  return {
    account,
    mapRef,
    onMarkerPress,
  }
}