All files / libs/payout-definition/plans/views/hub/rule-designer/src/lib/modals/make-field-global-confirmation-modal MakeFieldGlobalConfirmationModal.tsx

0% Statements 0/59
0% Branches 0/1
0% Functions 0/1
0% Lines 0/59

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                                                                                                                       
import { memo, useCallback } from 'react';
import { FormattedMessage } from 'react-intl';

import { IconLoading, Modal } from '@allshares/studio-design-system';
import { type Variable } from '@amalia/amalia-lang/tokens/types';
import { usePatchVariable } from '@amalia/payout-definition/state';

export type MakeFieldGlobalConfirmationModalProps = {
  readonly field: Pick<Variable, 'id' | 'name'>;
  readonly isOpen: boolean;
  readonly onClose: () => void;
};

export const MakeFieldGlobalConfirmationModal = memo(function MakeFieldGlobalConfirmationModal({
  field,
  isOpen,
  onClose,
}: MakeFieldGlobalConfirmationModalProps) {
  const { mutate: handlePatchVariable, isPending } = usePatchVariable();

  const handleConfirm = useCallback(
    () => handlePatchVariable({ id: field.id, variable: { planId: null } }, { onSuccess: onClose }),
    [handlePatchVariable, field, onClose],
  );

  return (
    <Modal
      isOpen={isOpen}
      onClose={onClose}
    >
      <Modal.Content>
        <Modal.Header>
          <Modal.Title>
            <FormattedMessage defaultMessage="Make calculated column available for all plans" />
          </Modal.Title>
        </Modal.Header>

        <Modal.Body>
          <FormattedMessage
            defaultMessage="Are you sure you want to make the calculated column “<b>{fieldName}</b>” available for all plans?{br}{br}<b>This action is irreversible.</b>"
            values={{ fieldName: field.name, b: (chunks) => <b>{chunks}</b> }}
          />
        </Modal.Body>
      </Modal.Content>

      <Modal.Actions>
        <Modal.CancelAction />

        <Modal.MainAction
          icon={isPending ? <IconLoading /> : undefined}
          isLoading={isPending}
          onClick={handleConfirm}
        >
          <FormattedMessage defaultMessage="Confirm" />
        </Modal.MainAction>
      </Modal.Actions>
    </Modal>
  );
});