All files / modules/70-pipeline/components/PipelineSteps/Steps/JiraCreate JiraFieldSelector.tsx

83.33% Statements 20/24
50% Branches 1/2
83.33% Functions 5/6
86.96% Lines 20/23

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              111x 111x 111x 111x     111x 111x   111x 18x 16x 16x   16x 2x 2x 2x 2x 2x               16x       10x       2x                     2x              
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Free Trial 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/05/PolyForm-Free-Trial-1.0.0.txt.
 */
 
import { isEmpty } from 'lodash-es'
import React, { useState } from 'react'
import { Button, Checkbox } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import type { JiraFieldNG } from 'services/cd-ng'
import type { JiraFieldSelectorProps } from './types'
import { updateMap } from './helper'
import css from './JiraFieldSelector.module.scss'
 
export function JiraFieldSelector(props: JiraFieldSelectorProps) {
  const { getString } = useStrings()
  const [selectedFields, setSelectedFields] = useState<JiraFieldNG[]>(props.selectedFields)
  const [selectedFieldsMap, setSelectedFieldsMap] = useState<Record<string, boolean>>(updateMap(selectedFields))
 
  const onSelect = (field: JiraFieldNG, checked: boolean) => {
    const alreadySelectedFields: JiraFieldNG[] = [...selectedFields]
    Eif (checked) {
      alreadySelectedFields.push(field)
      setSelectedFields(alreadySelectedFields)
      setSelectedFieldsMap(updateMap(alreadySelectedFields))
    } else {
      const newSelected = alreadySelectedFields.filter(item => item.name !== field.name)
      setSelectedFieldsMap(updateMap(newSelected))
      setSelectedFields(newSelected)
    }
  }
 
  return (
    <div>
      <div className={css.fieldsSection}>
        {props.fields.map(field => (
          <Checkbox
            key={field.name}
            className={css.checkbox}
            checked={selectedFieldsMap[field.name]}
            onChange={ev => onSelect(field, (ev.target as HTMLInputElement).checked)}
          >
            {field.name}
          </Checkbox>
        ))}
      </div>
      <div className={css.buttons}>
        <Button
          text={getString('add')}
          disabled={isEmpty(selectedFields)}
          intent="primary"
          onClick={() => props.addSelectedFields(selectedFields)}
        />
        <Button className={css.secondButton} text={getString('cancel')} onClick={props.onCancel} />
      </div>
    </div>
  )
}