All files / libs/lti/views/participant-enrollment/src/lib/enrollment-content LtiEnrollmentContent.tsx

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

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147                                                                                                                                                                                                                                                                                                     
import { css } from '@emotion/react';
import { memo, useMemo } from 'react';
import { FormattedMessage } from 'react-intl';
import { match } from 'ts-pattern';

import { AlertBanner, Stack, Typography } from '@allshares/studio-design-system';
import { useAuthenticatedContext } from '@amalia/kernel/auth/state';
import {
  KpisStructureWidgetPresentation,
  PlanInformationWidgetsPresentation,
  SimulationWidgetPresentation,
} from '@amalia/lti/components';
import { useListGrantDocuments } from '@amalia/lti/state';
import { EnrollmentWidgetType, type CompanyShareWithPrice, type LtiGrant, type LtiPlan } from '@amalia/lti/types';

import { EnrollmentParticipantDocumentWidget } from './widgets/document/EnrollmentParticipantDocumentWidget';
import { EnrollmentPreviewDocumentWidget } from './widgets/document/EnrollmentPreviewDocumentWidget';

interface LtiEnrollmentContentProps {
  readonly plan: LtiPlan;
  readonly grant: LtiGrant;
  readonly share?: CompanyShareWithPrice | null;
  readonly isPreview?: boolean;
}

export const LtiEnrollmentContent = memo(function LtiEnrollmentContent({
  plan,
  grant,
  share,
  isPreview = false,
}: LtiEnrollmentContentProps) {
  const {
    authenticatedContext: {
      user: { company },
    },
  } = useAuthenticatedContext();
  const performancePeriod = useMemo(() => ({ startDate: plan.startDate, endDate: plan.endDate }), [plan]);
  const { data: grantDocuments = [], isPending: isLoadingGrantDocuments } = useListGrantDocuments(
    plan.id,
    grant.id,
    !isPreview,
  );
  const grantDocumentsByMetadataId = useMemo(
    () => new Map(grantDocuments.map((grantDocument) => [grantDocument.metadata.id, grantDocument])),
    [grantDocuments],
  );

  if (!grant.invitationSentAt) {
    return (
      <AlertBanner variant="error">
        <FormattedMessage defaultMessage="Onboarding procedure for this grant has not started" />
      </AlertBanner>
    );
  }

  if (!grant.enrollmentConfigurationVersion) {
    return (
      <AlertBanner variant="error">
        <FormattedMessage defaultMessage="Onboarding configuration is not available" />
      </AlertBanner>
    );
  }

  return grant.enrollmentConfigurationVersion.configuration.sections.map(({ id, label, widgets }) => (
    <Stack
      key={id}
      gap={24}
    >
      <Typography variant="heading3Bold">{label}</Typography>

      {widgets.map((widget) =>
        match(widget)
          .with({ type: EnrollmentWidgetType.DOCUMENT }, (widget) => {
            if (isPreview) {
              return (
                <EnrollmentPreviewDocumentWidget
                  key={widget.id}
                  planId={plan.id}
                  widget={widget}
                />
              );
            }

            const document = widget.documentId ? grantDocumentsByMetadataId.get(widget.documentId) : undefined;

            return (
              <EnrollmentParticipantDocumentWidget
                key={widget.id}
                document={document}
                grantId={grant.id}
                isLoading={isLoadingGrantDocuments}
                planId={plan.id}
              />
            );
          })
          .with({ type: EnrollmentWidgetType.TEXT }, (widget) => (
            <Typography
              key={widget.id}
              variant="bodyBaseRegular"
              css={css`
                white-space: pre-line;
              `}
            >
              {widget.text}
            </Typography>
          ))
          .with({ type: EnrollmentWidgetType.PLAN_INFORMATION }, (widget) => (
            <PlanInformationWidgetsPresentation
              key={widget.id}
              estimatedRewardDate={plan.earningDate}
              grantDate={grant.grantDate}
              grantedShares={grant.nbShares}
              offerExpiryDate={grant.offerExpiryDate}
              performancePeriod={performancePeriod}
              widgetsConfiguration={widget}
              grantedSharesMonetaryValue={
                share && share.value ? { value: share.value * grant.nbShares, symbol: share.currency } : undefined
              }
            />
          ))
          .with({ type: EnrollmentWidgetType.KPIS_STRUCTURE }, (widget) => (
            <KpisStructureWidgetPresentation
              key={widget.id}
              kpis={plan.kpis}
              sharePrice={share ?? undefined}
              sharesCount={grant.nbShares}
              widgetConfiguration={widget}
            />
          ))
          .with({ type: EnrollmentWidgetType.SIMULATION }, (widget) => (
            <SimulationWidgetPresentation
              key={widget.id}
              currency={company.currency}
              initialKpisValues={plan.kpiValues}
              kpis={plan.kpis}
              sharePrice={share ?? undefined}
              sharesCount={grant.nbShares}
              size="compact"
              widgetConfiguration={widget}
            />
          ))
          .exhaustive(),
      )}
    </Stack>
  ));
});