All files / libs/payout-definition/plans/components/src/lib/cofirm-save-global-token-modal ConfirmSaveGlobalTokenModal.tsx

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

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                                                                                                                                                                                                             
import { css } from '@emotion/react';
import { isEmpty } from 'lodash-es';
import { memo } from 'react';
import { defineMessages, FormattedMessage } from 'react-intl';

import { Modal, Stack, Typography } from '@allshares/studio-design-system';
import { TokenType } from '@amalia/amalia-lang/tokens/types';

type ConfirmSaveGlobalTokenModalProps = {
  readonly onConfirm: () => Promise<void>;
  readonly usedInPlans: { name: string; id: string }[];
  readonly tokenType: TokenType.FIELD | TokenType.FILTER | TokenType.VARIABLE;
  readonly tokenName: string;
  readonly isModalOpen: boolean;
  readonly onCloseModal: () => void;
};

const TokenTypeToMessage = defineMessages<ConfirmSaveGlobalTokenModalProps['tokenType']>({
  [TokenType.FIELD]: {
    defaultMessage: 'This column is used in {plansCount, plural, one {another plan} other {other plans}}',
  },
  [TokenType.FILTER]: {
    defaultMessage: 'This table is used in {plansCount, plural, one {another plan} other {other plans}}',
  },
  [TokenType.VARIABLE]: {
    defaultMessage: 'This variable is used in {plansCount, plural, one {another plan} other {other plans}}',
  },
});

export const ConfirmSaveGlobalTokenModal = memo(function ConfirmSaveGlobalTokenModal({
  isModalOpen,
  tokenName,
  tokenType,
  usedInPlans,
  onConfirm,
  onCloseModal,
}: ConfirmSaveGlobalTokenModalProps) {
  if (isEmpty(usedInPlans)) return null;

  return (
    <Modal
      isOpen={isModalOpen}
      onClose={onCloseModal}
    >
      <Modal.Content>
        <Modal.Header>
          <Modal.Title>
            <FormattedMessage
              {...TokenTypeToMessage[tokenType]}
              values={{ plansCount: usedInPlans.length }}
            />
          </Modal.Title>
        </Modal.Header>

        <Modal.Body>
          <Stack gap={16}>
            <Typography variant="bodyBaseRegular">
              <FormattedMessage
                defaultMessage="Saving changes for <bold>{tokenName}</bold> will also impact the following {plansCount, plural, one {plan} other {plans}}: {plansUsedInNames}"
                values={{
                  tokenName,
                  plansCount: usedInPlans.length,
                  bold: (text) => <Typography variant="bodyBaseBold">{text}</Typography>,
                  plansUsedInNames:
                    usedInPlans.length > 1 ? (
                      <Stack
                        gap={6}
                        css={css`
                          margin-top: 16px;
                        `}
                      >
                        {usedInPlans.map((plan) => (
                          <Typography
                            key={plan.id}
                            variant="bodyBaseBold"
                          >
                            {plan.name}
                          </Typography>
                        ))}
                      </Stack>
                    ) : (
                      <Typography variant="bodyBaseBold">{usedInPlans[0].name}</Typography>
                    ),
                }}
              />
            </Typography>
            <Typography variant="bodyBaseRegular">
              <FormattedMessage defaultMessage="Are you sure you want to save changes?" />
            </Typography>
          </Stack>
        </Modal.Body>
      </Modal.Content>

      <Modal.Actions>
        <Modal.CancelAction />
        <Modal.MainAction onClick={onConfirm}>
          <FormattedMessage defaultMessage="Confirm" />
        </Modal.MainAction>
      </Modal.Actions>
    </Modal>
  );
});