All files / modules/75-ce/components/COAccessPointDelete DeleteAccessPoint.tsx

50% Statements 21/42
37.5% Branches 3/8
9.09% Functions 1/11
53.85% Lines 21/39

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 114 115 116 117 118 119 120 121 122              1x 1x 1x   1x 1x 1x   1x                                     1x 2x 2x 2x 2x 2x             2x 2x 2x                             2x             2x                                                                             2x 2x                   1x  
/*
 * 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 { Button, Formik, FormikForm, FormInput, Layout, Heading } from '@wings-software/uicore'
import { Dialog, IDialogProps } from '@blueprintjs/core'
import type { AccessPoint } from 'services/lw'
import { useToaster } from '@common/exports'
import { useStrings } from 'framework/strings'
import { useDeleteAccessPoints, DeleteAccessPointPayload } from 'services/lw'
 
const modalPropsLight: IDialogProps = {
  isOpen: true,
  enforceFocus: false,
  style: {
    width: 500,
    minHeight: 200,
    position: 'relative',
    overflow: 'hidden'
  }
}
 
interface DeleteAccessPointProps {
  accessPoints: AccessPoint[]
  orgID: string
  projectID: string
  accountId: string
  refresh: () => void
}
 
const DeleteAccessPoint = (props: DeleteAccessPointProps) => {
  const { getString } = useStrings()
  const { showError, showSuccess } = useToaster()
  const { accessPoints, accountId, refresh } = props
  const [modalState, setModalState] = useState(false)
  const { mutate: deleteAccessPoints, loading } = useDeleteAccessPoints({
    account_id: accountId,
    queryParams: {
      accountIdentifier: accountId
    }
  })
 
  const showModal = () => setModalState(true)
  const hideModal = () => setModalState(false)
  const deleteFromCloud = async (withResources: boolean): Promise<void> => {
    const ids = accessPoints.map(ap => ap.id as string)
    const payload: DeleteAccessPointPayload = {
      ids,
      with_resources: withResources // eslint-disable-line
    }
    try {
      await deleteAccessPoints(payload)
      showSuccess(getString('ce.co.accessPoint.delete.success'))
      refresh()
    } catch (e) {
      showError(e.data.errors[0], undefined, 'ce.delete.ap.error')
    }
    hideModal()
  }
  const getTitle = (): string => {
    let str = getString('ce.co.accessPoint.loadbalancers')
    if (accessPoints.length == 1) {
      str = getString('ce.co.accessPoint.loadbalancer')
    }
    return getString('delete') + ' ' + accessPoints.length + ' ' + str
  }
  const DeleteConfirmation = () => (
    <Dialog onClose={hideModal} {...modalPropsLight}>
      <Formik
        onSubmit={values => {
          deleteFromCloud(values.withResources)
        }}
        initialValues={{
          withResources: false
        }}
        formName="deleteAccessPt"
        render={_ => (
          <FormikForm>
            <Layout.Vertical spacing="xlarge" margin="xlarge">
              <Heading level={2} font={{ weight: 'bold' }}>
                {getTitle()}
              </Heading>
              <FormInput.CheckBox
                name="withResources"
                label={getString('ce.co.accessPoint.delete.withResource')}
                margin={{ left: 'xlarge' }}
                font={{ weight: 'semi-bold' }}
                disabled={loading}
              />
              <Layout.Horizontal spacing="xlarge">
                <Button
                  intent="primary"
                  type="submit"
                  text={getString('delete')}
                  loading={loading}
                  disabled={loading}
                />
                <Button text={getString('cancel')} onClick={() => hideModal()} disabled={loading} />
              </Layout.Horizontal>
            </Layout.Vertical>
          </FormikForm>
        )}
      />
    </Dialog>
  )
  Eif (!accessPoints || accessPoints.length == 0) {
    return <></>
  }
  return (
    <>
      <Button intent="primary" icon="trash" text={getString('delete')} onClick={() => showModal()} />
      {modalState && <DeleteConfirmation />}
    </>
  )
}
 
export default DeleteAccessPoint