All files / modules/75-cf/pages/target-management/targets NewTarget.tsx

39.02% Statements 16/41
50% Branches 2/4
5% Functions 1/20
39.47% Lines 15/38

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113              1x 1x 1x 1x 1x 1x                           1x           7x 7x 7x     7x   7x                                                   7x                                                     7x                       7x                
/*
 * Copyright 2021 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 React, { useState } from 'react'
import { get } from 'lodash-es'
import { useToaster } from '@common/exports'
import useActiveEnvironment from '@cf/hooks/useActiveEnvironment'
import { CreateTargetQueryParams, useCreateTarget } from 'services/cf'
import CreateTargetModal, { TargetData } from './CreateTargetModal'
 
export interface NewTargetsProps {
  accountIdentifier: string
  orgIdentifier: string
  projectIdentifier: string
  onCreated: () => void
}
 
type SettledTarget = {
  status: 'fulfilled' | 'rejected'
  data: TargetData
}
 
export const NewTargets: React.FC<NewTargetsProps> = ({
  accountIdentifier,
  orgIdentifier,
  projectIdentifier,
  onCreated
}) => {
  const { showError, clear } = useToaster()
  const [loadingBulk, setLoadingBulk] = useState<boolean>(false)
  const { mutate: createTarget, loading: loadingCreateTarget } = useCreateTarget({
    queryParams: { accountIdentifier, orgIdentifier } as CreateTargetQueryParams
  })
  const { activeEnvironment: environmentIdentifier } = useActiveEnvironment()
 
  const bulkTargetCreation = (ts: TargetData[]): Promise<SettledTarget[]> => {
    return Promise.all(
      ts.map((t: TargetData) => {
        return createTarget({
          identifier: t.identifier,
          name: t.name,
          anonymous: false,
          attributes: {},
          environment: environmentIdentifier,
          project: projectIdentifier,
          account: accountIdentifier,
          org: orgIdentifier
        })
          .then(() => ({
            status: 'fulfilled',
            data: t
          }))
          .catch(error => ({
            status: 'rejected',
            data: t,
            error
          })) as Promise<SettledTarget>
      })
    )
  }
 
  const handleTargetCreation = (ts: TargetData[], hideModal: () => void): void => {
    setLoadingBulk(true)
    bulkTargetCreation(ts)
      .then(results => {
        if (results.every(res => res.status === 'rejected')) {
          return Promise.reject(results)
        }
        results
          .filter(res => res.status === 'rejected')
          .forEach((res: SettledTarget) => {
            clear()
            showError(get(res, 'error.data.message', get(res, 'error.message')), 0, 'cf.create.bulk.target..error')
          })
      })
      .then(() => {
        hideModal()
        onCreated()
      })
      .catch(results => {
        results.forEach((res: SettledTarget) => {
          clear()
          showError(get(res, 'error.data.message', get(res, 'error.message')), 0, 'cf.create.bulk.target.error')
        })
      })
      .finally(() => setLoadingBulk(false))
  }
 
  const handleTargetUpload = (file: File, hideModal: () => void): void => {
    file
      .text()
      .then((str: string) => {
        return str
          .split(/\r?\n/)
          .map(row => row.split(',').map(x => x.trim()))
          .map(([name, identifier]) => ({ name, identifier } as TargetData))
      })
      .then((ts: TargetData[]) => handleTargetCreation(ts, hideModal))
  }
 
  return (
    <CreateTargetModal
      loading={loadingCreateTarget || loadingBulk}
      onSubmitTargets={handleTargetCreation}
      onSubmitUpload={handleTargetUpload}
    />
  )
}