All files / src/helpers/httpClient/queries/places useNoloPlaces.ts

35.71% Statements 5/14
100% Branches 1/1
37.5% Functions 3/8
35.71% Lines 5/14

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 81 82 83 84 85 86 87                                                                        6x 2x                   6x                                                   3x                     3x    
/**
 * @fileoverview useNoloPlaces hook, used to fetch places from the Nolo API.
 * @module useNoloPlaces
 * @requires react-query
 */
import { useMutation } from '@tanstack/react-query'
import { Place } from '@global/types/Places'
import { NoloPlacesJSON } from '@global/types/httpClient/queries/places'
import searchPlaces, { getPlaces } from '@helpers/httpClient/places'
 
type UpdatePlacesDisplayedProps = {
  setPlaces: (places: Place[]) => void
  token: string
  getLatest?: boolean
}
 
interface UpdatePlacesDisplayedPropsSearch extends UpdatePlacesDisplayedProps {
  latitude?: number
  longitude?: number
  q?: string
  radius?: number
}
 
/**
 * @function useNoloPlaces Handles the places mutation
 * @param props The setPlaces function and token
 * @param props.setPlaces The function to set the places
 * @param props.token The token
 * @param props.getLatest The flag to get the latest places
 * @returns The mutation object
 */
export default function useNoloPlaces({ setPlaces, token, getLatest = false }: UpdatePlacesDisplayedProps) {
  function updatePlacesDisplayed({ newPlaces }: { newPlaces: Place[] }) {
    setPlaces(newPlaces)
  }
 
  const mutation = useMutation<NoloPlacesJSON>({
    mutationFn: () => getPlaces({ token, getLatest }),
    onSuccess: data => {
      try {
        updatePlacesDisplayed({ newPlaces: data.json })
      } catch (error) {
        console.error(error)
      }
    },
  })
 
  return mutation
}
 
/**
 * @function useNoloPlaces Handles the places mutation
 * @param props The setPlaces function, latitude, longitude, q, radius, and token
 * @param props.setPlaces The function to set the places
 * @param props.latitude The latitude
 * @param props.longitude The longitude
 * @param props.q The query
 * @param props.radius The radius
 * @param props.token The token
 * @returns The mutation object
 */
export function useSearchNoloPlaces({
  setPlaces,
  latitude,
  longitude,
  q,
  radius,
  token,
}: UpdatePlacesDisplayedPropsSearch) {
  function updatePlacesDisplayed({ newPlaces }: { newPlaces: Place[] }) {
    setPlaces(newPlaces)
  }
 
  const mutation = useMutation<NoloPlacesJSON>({
    mutationFn: () => searchPlaces({ latitude, longitude, q, radius, token }),
    onSuccess: data => {
      try {
        updatePlacesDisplayed({ newPlaces: data.json })
      } catch (error) {
        console.error(error)
      }
    },
  })
 
  return mutation
}