All files / modules/75-cf/pages/target-group-detail/components/EditTargetGroupCriteria EditTargetGroupCriteriaDialog.tsx

100% Statements 32/32
76.32% Branches 29/38
100% Functions 4/4
100% Lines 32/32

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157              2x 2x 2x 2x                   2x 2x 2x 2x 2x 2x 2x 2x   2x                           2x         6x 6x 6x   6x                   6x   3x   3x                               3x   3x   2x 2x 1x 1x 1x   1x                                   6x                     20x                                                                   2x  
/*
 * 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 React, { FC, useCallback } from 'react'
import { useParams } from 'react-router-dom'
import { Spinner } from '@blueprintjs/core'
import {
  Button,
  ButtonVariation,
  Dialog,
  Formik,
  FormikForm,
  Layout,
  SelectOption,
  useToaster
} from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import { Clause, Segment, usePatchSegment } from 'services/cf'
import targetToSelectOption from '@cf/utils/targetToSelectOption'
import patch, { Instruction } from '@cf/utils/instructions'
import { getErrorMessage } from '@cf/utils/CFUtils'
import SpecifyIndividualTargets from './SpecifyIndividualTargets'
import TargetBasedOnConditions from './TargetBasedOnConditions'
import { getRulesInstructions, getTargetInstructions } from './getInstructions'
 
import css from './EditTargetGroupCriteriaDialog.module.scss'
 
export interface EditTargetGroupCriteriaDialogProps {
  hideModal: () => void
  targetGroup: Segment
  onUpdate: () => void
}
 
export interface EditTargetGroupCriteriaFormValues {
  included: SelectOption[]
  excluded: SelectOption[]
  rules: Clause[]
}
 
const EditTargetGroupCriteriaDialog: FC<EditTargetGroupCriteriaDialogProps> = ({
  hideModal,
  targetGroup,
  onUpdate
}) => {
  const { getString } = useStrings()
  const { accountId: accountIdentifier, orgIdentifier, projectIdentifier } = useParams<Record<string, string>>()
  const { showError, showSuccess } = useToaster()
 
  const { mutate: sendPatch, loading } = usePatchSegment({
    identifier: targetGroup.identifier,
    queryParams: {
      environmentIdentifier: targetGroup.environment as string,
      accountIdentifier,
      orgIdentifier,
      projectIdentifier
    }
  })
 
  const onSubmit = useCallback(
    (values: EditTargetGroupCriteriaFormValues) => {
      patch.segment.reset()
 
      const instructions: Instruction[] = [
        ...getTargetInstructions(
          targetGroup?.included || [],
          values.included,
          patch.creators.addToIncludeList,
          patch.creators.removeFromIncludeList
        ),
        ...getTargetInstructions(
          targetGroup?.excluded || [],
          values.excluded,
          patch.creators.addToExcludeList,
          patch.creators.removeFromExcludeList
        ),
        ...getRulesInstructions(targetGroup?.rules || [], values.rules)
      ]
 
      patch.segment.addAllInstructions(instructions)
 
      patch.segment
        .onPatchAvailable(async patchData => {
          try {
            await sendPatch(patchData)
            showSuccess(getString('cf.segmentDetail.updated'))
            hideModal()
            onUpdate()
          } catch (e) {
            showError(getErrorMessage(e), undefined, 'cf.send.patch.error')
          }
        })
        .onEmptyPatch(hideModal)
    },
    [
      getString,
      hideModal,
      onUpdate,
      sendPatch,
      showError,
      showSuccess,
      targetGroup?.excluded,
      targetGroup?.included,
      targetGroup?.rules
    ]
  )
 
  return (
    <Formik<EditTargetGroupCriteriaFormValues>
      formName="TargetGroupCriteria"
      onSubmit={onSubmit}
      initialValues={{
        included: (targetGroup.included || []).map(targetToSelectOption),
        excluded: (targetGroup.excluded || []).map(targetToSelectOption),
        rules: targetGroup.rules || []
      }}
    >
      {({ submitForm, values }) => (
        <Dialog
          className={css.dialog}
          isOpen
          enforceFocus={false}
          title={getString('cf.segmentDetail.targetGroupCriteria')}
          onClose={hideModal}
          footer={
            <Layout.Horizontal spacing="small">
              <Button variation={ButtonVariation.PRIMARY} type="submit" intent="primary" onClick={submitForm}>
                {getString('save')}
              </Button>
              <Button variation={ButtonVariation.SECONDARY} onClick={hideModal}>
                {getString('cancel')}
              </Button>
              {loading && (
                <span data-testid="saving-spinner">
                  <Spinner size={24} />
                </span>
              )}
            </Layout.Horizontal>
          }
        >
          <FormikForm>
            <Layout.Vertical spacing="small" className={css.body} padding="xsmall">
              <SpecifyIndividualTargets targetGroup={targetGroup} />
              <TargetBasedOnConditions targetGroup={targetGroup} values={values} />
            </Layout.Vertical>
          </FormikForm>
        </Dialog>
      )}
    </Formik>
  )
}
 
export default EditTargetGroupCriteriaDialog