All files / libs/lti/components/src/lib/participant-dashboard/end-employment-widget LtiEndEmploymentWidgetForm.tsx

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

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                                                                                                                                                                                                                                                                                                                                                                   
import { css } from '@emotion/react';
import { IconHelp } from '@tabler/icons-react';
import { useFormikContext } from 'formik';
import { Fragment, memo, useMemo } from 'react';
import { FormattedMessage, useIntl } from 'react-intl';

import {
  FormFieldLabel,
  Group,
  Modal,
  Stack,
  Tooltip,
  Typography,
  type SelectOption,
} from '@allshares/studio-design-system';
import { FormikDatePicker, FormikSelect } from '@amalia/design-system/ext';
import { FormikForm } from '@amalia/ext/formik';
import {
  isGrantActiveAtEmploymentEnd,
  LtiGrantLeaverType,
  type LtiEndEmploymentRequest,
  type LtiParticipantGrants,
} from '@amalia/lti/types';

interface LtiEndEmploymentWidgetFormProps {
  readonly grants: LtiParticipantGrants[];
  readonly isPending: boolean;
}

export const LtiEndEmploymentWidgetForm = memo(function LtiEndEmploymentWidgetForm({
  grants,
  isPending,
}: LtiEndEmploymentWidgetFormProps) {
  const { formatMessage } = useIntl();

  const leaverTypeOptions = useMemo<SelectOption<LtiGrantLeaverType>[]>(
    () => [
      {
        label: formatMessage({ defaultMessage: 'Good leaver' }),
        value: LtiGrantLeaverType.GOOD_LEAVER,
      },
      {
        label: formatMessage({ defaultMessage: 'Bad leaver' }),
        value: LtiGrantLeaverType.BAD_LEAVER,
      },
      {
        label: formatMessage({ defaultMessage: 'Exceptional leaver' }),
        value: LtiGrantLeaverType.EXCEPTIONAL_LEAVER,
      },
    ],
    [formatMessage],
  );

  const { values } = useFormikContext<LtiEndEmploymentRequest>();

  const activeGrants = useMemo(
    () => grants.filter((grant) => isGrantActiveAtEmploymentEnd(grant, values.employmentEndedDate)),
    [grants, values.employmentEndedDate],
  );

  return (
    <FormikForm.FormDisplayContents>
      <Modal.Content>
        <Modal.Header>
          <Modal.Title>
            <FormattedMessage defaultMessage="End employment" />
          </Modal.Title>
        </Modal.Header>

        <Modal.Body>
          <Stack gap={24}>
            <Typography variant="bodyBaseRegular">
              <FormattedMessage defaultMessage="Set the termination date and review the impact on each active plan." />
            </Typography>

            <Stack gap={55}>
              <Group gap={24}>
                <FormFieldLabel
                  required
                  htmlFor="employmentEndedDate"
                >
                  <FormattedMessage defaultMessage="Termination date" />
                </FormFieldLabel>

                <FormikDatePicker
                  required
                  name="employmentEndedDate"
                />
              </Group>

              {activeGrants.length ? (
                <div
                  css={css`
                    display: grid;
                    grid-template-columns: 1fr 1fr;
                    gap: 16px;
                  `}
                >
                  <Typography
                    variant="bodySmallRegular"
                    css={(theme) => css`
                      color: ${theme.ds.colors.gray['500']};
                    `}
                  >
                    <FormattedMessage defaultMessage="Plan" />
                  </Typography>

                  <Group
                    align="center"
                    gap={8}
                  >
                    <Typography
                      variant="bodySmallRegular"
                      css={(theme) => css`
                        color: ${theme.ds.colors.gray['500']};
                      `}
                    >
                      <FormattedMessage defaultMessage="Leaver type" />
                    </Typography>
                    <Tooltip
                      content={
                        <FormattedMessage
                          defaultMessage="<b>Bad leaver</b>: The grant is canceled. Potential drops to 0.{br}<b>Good leaver</b>: The grant is pro-rated based on termination date.{br}<b>Exceptional leaver</b>: The grant is kept at 100% (no reduction)."
                          values={{
                            b: (nodes) => <b>{nodes}</b>,
                          }}
                        />
                      }
                    >
                      <IconHelp size={14} />
                    </Tooltip>
                  </Group>

                  {activeGrants.map(({ plan }) => (
                    <Fragment key={plan.id}>
                      <FormFieldLabel
                        required
                        htmlFor={`leaverTypes.${plan.id}`}
                        size="medium"
                      >
                        {plan.name}
                      </FormFieldLabel>

                      <FormikSelect<SelectOption<LtiGrantLeaverType>, false, false, false>
                        required
                        isClearable={false}
                        name={`leaverTypes.${plan.id}`}
                        options={leaverTypeOptions}
                        size="small"
                      />
                    </Fragment>
                  ))}
                </div>
              ) : (
                <Typography variant="bodyBaseRegular">
                  <FormattedMessage defaultMessage="No active plans match the selected termination date." />
                </Typography>
              )}
            </Stack>
          </Stack>
        </Modal.Body>
      </Modal.Content>

      <Modal.Actions>
        <Modal.CancelAction />

        <Modal.MainAction
          disabled={!activeGrants.length}
          isLoading={isPending}
          type="submit"
        >
          <FormattedMessage defaultMessage="Confirm" />
        </Modal.MainAction>
      </Modal.Actions>
    </FormikForm.FormDisplayContents>
  );
});