|
| 1 | +import { Form, FormGroup, TextArea } from '@patternfly/react-core'; |
| 2 | + |
| 3 | +import { DeliverableAnalyzerReport } from 'pnc-api-types-ts'; |
| 4 | + |
| 5 | +import { ButtonTitles } from 'common/constants'; |
| 6 | +import { |
| 7 | + EditableDeliverableAnalysisLabel, |
| 8 | + deliverableAnalysisLabelEntryEntityAttributes, |
| 9 | +} from 'common/deliverableAnalysisLabelEntryEntityAttributes'; |
| 10 | + |
| 11 | +import { IFieldConfigs, IFieldValues, useForm } from 'hooks/useForm'; |
| 12 | +import { useServiceContainer } from 'hooks/useServiceContainer'; |
| 13 | + |
| 14 | +import { ActionModal } from 'components/ActionModal/ActionModal'; |
| 15 | +import { FormInputHelperText } from 'components/FormInputHelperText/FormInputHelperText'; |
| 16 | +import { DeliverableAnalysisLabelLabelMapper } from 'components/LabelMapper/DeliverableAnalysisLabelLabelMapper'; |
| 17 | + |
| 18 | +import * as deliverableAnalysisApi from 'services/deliverableAnalysisApi'; |
| 19 | + |
| 20 | +import { maxLengthValidator } from 'utils/formValidationHelpers'; |
| 21 | + |
| 22 | +const fieldConfigs = { |
| 23 | + reason: { |
| 24 | + isRequired: true, |
| 25 | + validators: [maxLengthValidator(200)], |
| 26 | + }, |
| 27 | +} satisfies IFieldConfigs; |
| 28 | + |
| 29 | +interface IDeliverableAnalysisRemoveLabelModalProps { |
| 30 | + isModalOpen: boolean; |
| 31 | + toggleModal: () => void; |
| 32 | + deliverableAnalysisReport: DeliverableAnalyzerReport; |
| 33 | + label: EditableDeliverableAnalysisLabel; |
| 34 | +} |
| 35 | + |
| 36 | +export const DeliverableAnalysisRemoveLabelModal = ({ |
| 37 | + isModalOpen, |
| 38 | + toggleModal, |
| 39 | + deliverableAnalysisReport, |
| 40 | + label, |
| 41 | +}: IDeliverableAnalysisRemoveLabelModalProps) => { |
| 42 | + const serviceContainerDeliverableAnalysisRemoveLabel = useServiceContainer( |
| 43 | + deliverableAnalysisApi.removeDeliverableAnalysisLabel, |
| 44 | + 0 |
| 45 | + ); |
| 46 | + |
| 47 | + const { register, getFieldErrors, handleSubmit, isSubmitDisabled, hasFormChanged } = useForm(); |
| 48 | + |
| 49 | + const confirmModal = (data: IFieldValues) => { |
| 50 | + return serviceContainerDeliverableAnalysisRemoveLabel.run({ |
| 51 | + serviceData: { |
| 52 | + id: deliverableAnalysisReport.id!, |
| 53 | + data: { label: label, reason: data.reason }, |
| 54 | + }, |
| 55 | + onError: () => console.error('Failed to remove Deliverable Analysis Label.'), |
| 56 | + }); |
| 57 | + }; |
| 58 | + |
| 59 | + return ( |
| 60 | + <ActionModal |
| 61 | + modalTitle={`${ButtonTitles.remove} Deliverable Analysis Label: ${deliverableAnalysisReport.id}`} |
| 62 | + actionTitle={ButtonTitles.remove} |
| 63 | + isOpen={isModalOpen} |
| 64 | + isSubmitDisabled={isSubmitDisabled} |
| 65 | + wereSubmitDataChanged={hasFormChanged} |
| 66 | + onToggle={toggleModal} |
| 67 | + onSubmit={handleSubmit(confirmModal)} |
| 68 | + serviceContainer={serviceContainerDeliverableAnalysisRemoveLabel} |
| 69 | + modalVariant="large" |
| 70 | + > |
| 71 | + <Form |
| 72 | + onSubmit={(e) => { |
| 73 | + e.preventDefault(); |
| 74 | + }} |
| 75 | + > |
| 76 | + <FormGroup |
| 77 | + label={deliverableAnalysisLabelEntryEntityAttributes.label.title} |
| 78 | + fieldId={deliverableAnalysisLabelEntryEntityAttributes.label.id} |
| 79 | + > |
| 80 | + <DeliverableAnalysisLabelLabelMapper label={label} /> |
| 81 | + </FormGroup> |
| 82 | + <FormGroup |
| 83 | + isRequired |
| 84 | + label={deliverableAnalysisLabelEntryEntityAttributes.reason.title} |
| 85 | + fieldId={deliverableAnalysisLabelEntryEntityAttributes.reason.id} |
| 86 | + > |
| 87 | + <TextArea |
| 88 | + isRequired |
| 89 | + type="text" |
| 90 | + id={deliverableAnalysisLabelEntryEntityAttributes.reason.id} |
| 91 | + name={deliverableAnalysisLabelEntryEntityAttributes.reason.id} |
| 92 | + resizeOrientation="vertical" |
| 93 | + autoComplete="off" |
| 94 | + {...register<string>(deliverableAnalysisLabelEntryEntityAttributes.reason.id, fieldConfigs.reason)} |
| 95 | + /> |
| 96 | + <FormInputHelperText variant="error"> |
| 97 | + {getFieldErrors(deliverableAnalysisLabelEntryEntityAttributes.reason.id)} |
| 98 | + </FormInputHelperText> |
| 99 | + </FormGroup> |
| 100 | + </Form> |
| 101 | + </ActionModal> |
| 102 | + ); |
| 103 | +}; |
0 commit comments