All files / modules/75-ce/common/hooks useRegionsForSelection.tsx

100% Statements 15/15
100% Branches 9/9
100% Functions 4/4
100% Lines 14/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              14x 14x   14x                         14x       57x   57x   57x                 57x 21x     57x 21x 21x     57x           14x  
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import { useEffect, useState } from 'react'
import { useParams } from 'react-router-dom'
import type { SelectOption } from '@harness/uicore'
import { Region, useAllRegions, UseAllRegionsProps } from 'services/lw'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
 
interface UseRegionsForSelectionProps {
  cloudAccountId: string
  additionalProps: Omit<UseAllRegionsProps, 'account_id'>
}
 
interface UseRegionsForSelectionReturnProps {
  data: SelectOption[]
  loading: boolean
}
 
const useRegionsForSelection = ({
  cloudAccountId,
  additionalProps
}: UseRegionsForSelectionProps): UseRegionsForSelectionReturnProps => {
  const { accountId } = useParams<AccountPathProps>()
 
  const [regionsData, setRegionsData] = useState<SelectOption[]>([])
 
  const { data, loading } = useAllRegions({
    account_id: accountId,
    queryParams: {
      cloud_account_id: cloudAccountId,
      accountIdentifier: accountId
    },
    ...additionalProps
  })
 
  useEffect(() => {
    setRegionsForSelection(data?.response)
  }, [data?.response])
 
  const setRegionsForSelection = (response: Region[] = []) => {
    const loaded = response.map(r => ({ label: r.label as string, value: r.name as string }))
    setRegionsData(loaded)
  }
 
  return {
    data: regionsData,
    loading
  }
}
 
export default useRegionsForSelection