All files / libs/payout-calculation/payments/views/commission-report/src/lib/split PaymentSplitModal.tsx

79.68% Statements 102/128
77.77% Branches 14/18
100% Functions 1/1
79.68% Lines 102/128

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 1291x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 5x 5x 5x 1x 1x 1x 5x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 1x 1x 1x 1x 7x 7x 7x 7x 7x 2x 2x 5x 7x                                                     5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 5x 5x 5x 1x  
import { css } from '@emotion/react';
import { memo, useCallback, useMemo } from 'react';
import { FormattedMessage } from 'react-intl';
 
import { CircularLoader, Group, Modal } from '@allshares/studio-design-system';
import { isSplit, type PaymentContract } from '@amalia/core/types';
import { FormikForm } from '@amalia/ext/formik';
import {
  useClearPaymentSplit,
  useCreatePaymentSplit,
  usePaymentsDetails,
} from '@amalia/payout-calculation/payments/state';
import { type PeriodsMap } from '@amalia/payout-definition/periods/types';
 
import { PaymentSplitModalForm, type PaymentSplitFormValues } from './PaymentSplitModalForm';
import { type PaymentSplitPartWithPercent } from './types';
 
interface PaymentSplitModalProps {
  // Modal
  readonly isOpen: boolean;
  readonly onClose: () => void;
 
  // Data
  readonly selectedPaymentToSplit?: PaymentContract | null;
  readonly periodsMap: PeriodsMap;
}
 
export const PaymentSplitModal = memo(function PaymentSplitModal({
  isOpen,
  onClose,
  selectedPaymentToSplit,
  periodsMap,
}: PaymentSplitModalProps) {
  const { data: paymentDetails, isPending: areDetailsLoading } = usePaymentsDetails(selectedPaymentToSplit, {
    enabled: !!isOpen,
  });
 
  // FORM SOURCE
  const isSelectedPaymentSplit = !!selectedPaymentToSplit && isSplit(selectedPaymentToSplit);
 
  const originalPayment = useMemo(() => paymentDetails?.find((p) => !p.masterPaymentId), [paymentDetails]);
 
  const initialValues = useMemo(
    () => ({
      newValue: selectedPaymentToSplit?.value || 0,
      splitParts: (paymentDetails ?? [])
        // Remove the original payment as it's printed apart
        .filter((p) => !!p.masterPaymentId)
        .map((p) => ({
          value: p.value,
          percent: p.value / (selectedPaymentToSplit?.totalValue || selectedPaymentToSplit?.value || 1),
          paymentPeriodId: p.paymentPeriodId,
        })) as PaymentSplitPartWithPercent[],
    }),
    [selectedPaymentToSplit, paymentDetails],
  );
 
  const { mutateAsync: createSplit } = useCreatePaymentSplit();
  const { mutateAsync: clearSplit } = useClearPaymentSplit();
 
  // HANDLERS
  const handleSubmit = useCallback(
    async (values: PaymentSplitFormValues) => {
      if (isSelectedPaymentSplit) {
        await clearSplit(selectedPaymentToSplit);
      } else {
        await createSplit({ payment: selectedPaymentToSplit!, splitParts: values.splitParts });
      }
    },
    [createSplit, clearSplit, isSelectedPaymentSplit, selectedPaymentToSplit],
  );
 
  if (!isOpen || !selectedPaymentToSplit || !originalPayment) {
    return null;
  }
 
  if (areDetailsLoading) {
    return (
      <Modal
        isOpen
        onClose={onClose}
      >
        <Modal.Content>
          <Modal.Header>
            <Modal.Title>
              <FormattedMessage defaultMessage="Split a payment" />
            </Modal.Title>
          </Modal.Header>

          <Modal.Body>
            <Group
              justify="center"
              css={css`
                padding: 20px;
              `}
            >
              <CircularLoader />
            </Group>
          </Modal.Body>
        </Modal.Content>
      </Modal>
    );
  }
 
  return (
    <FormikForm
      enableReinitialize
      isInitialValid
      initialValues={initialValues}
      onSubmit={handleSubmit}
      onSubmitSuccess={onClose}
    >
      {({ isValid, setFieldValue, values }) => (
        <PaymentSplitModalForm
          isOpen={isOpen}
          isSelectedPaymentSplit={isSelectedPaymentSplit}
          isValid={isValid}
          originalPayment={originalPayment}
          periodsMap={periodsMap}
          readonly={isSelectedPaymentSplit}
          setFieldValue={setFieldValue}
          values={values}
          onClose={onClose}
        />
      )}
    </FormikForm>
  );
});