All files / libs/lti/components/src/lib/forms/fields LtiPlanFields.tsx

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

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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import { css, type Theme } from '@emotion/react';
import { useFormikContext } from 'formik';
import { get } from 'lodash-es';
import { Fragment, memo, type ReactNode } from 'react';
import { FormattedMessage, useIntl } from 'react-intl';

import {
  Divider,
  FormFieldLabel,
  Group,
  Paper,
  RadioButtonGroup,
  Stack,
  Typography,
} from '@allshares/studio-design-system';
import { FormikDatePicker, FormikDebouncedInput, FormikSelect, FormikSwitch } from '@amalia/design-system/ext';

import { type LtiPlanFormValues } from '../configuration/LtiPlanForm.types';
import { useCurrencySymbolForPlan } from '../hooks/use-currency-symbol-for-plan';

import { useLtiGrantTypeOptions } from './grant-type/use-lti-grant-type-options';
import { KpiPerformanceChart } from './kpi-performance/chart/KpiPerformanceChart';
import { KpiListForm } from './kpi-performance/KpiListForm';
import { KpiPerformanceLinearForm } from './kpi-performance/KpiPerformanceLinearForm';
import { useLtiPlanTypeOptions } from './plan-type/use-lti-plan-type-options';

const FormSection = memo(function FormSection({
  title,
  children,
}: {
  readonly title: ReactNode;
  readonly children: ReactNode;
}) {
  return (
    <Stack
      align="stretch"
      gap={24}
    >
      <Typography variant="heading2Bold">{title}</Typography>
      <Paper
        css={css`
          padding: 32px 48px 40px 48px;
          width: 100%;
        `}
      >
        {children}
      </Paper>
    </Stack>
  );
});

interface LtiPlanFieldsProps {
  readonly isLtiPlanLocked?: boolean;
}

export const LtiPlanFields = memo(function LtiPlanFields({ isLtiPlanLocked = false }: LtiPlanFieldsProps) {
  const ltiGrantTypeOptions = useLtiGrantTypeOptions();
  const ltiPlanTypeOptions = useLtiPlanTypeOptions();

  const { formatMessage } = useIntl();

  const { values, errors } = useFormikContext<LtiPlanFormValues>();

  const shouldShowSection2 = values.grantType && values.planType && values.name && values.grantTotal;
  const shouldShowSection3 =
    values.kpis && values.kpis.length > 0 && values.startDate && values.endDate && values.kpis[0].name;
  const shouldShowSection4 = null as Date | null; // values.earningDate; TODO: dunno why but I got to hide this temporarily.

  const currencySymbol = useCurrencySymbolForPlan(values.grantType);

  return (
    <Stack
      gap={56}
      width="100%"
    >
      <FormSection title={<FormattedMessage defaultMessage="1. Plan information" />}>
        <div
          css={css`
            display: grid;
            row-gap: 20px;
            column-gap: 24px;
            grid-template-columns: auto 1fr;
          `}
        >
          <FormFieldLabel
            required
            htmlFor="name"
            size="medium"
          >
            <FormattedMessage defaultMessage="Plan name" />
          </FormFieldLabel>
          <FormikDebouncedInput
            required
            name="name"
          />
          <FormFieldLabel
            required
            htmlFor="planType"
            size="medium"
          >
            <FormattedMessage defaultMessage="Plan type" />
          </FormFieldLabel>
          <FormikSelect
            required
            isClearable={false}
            name="planType"
            options={ltiPlanTypeOptions}
          />
          <FormFieldLabel
            required
            htmlFor="grantType"
            size="medium"
          >
            <FormattedMessage defaultMessage="Grant type" />
          </FormFieldLabel>
          <FormikSelect
            required
            isClearable={false}
            name="grantType"
            options={ltiGrantTypeOptions}
          />
          <FormFieldLabel
            required
            htmlFor="grantTotal"
            size="medium"
          >
            <FormattedMessage defaultMessage="Plan pool size" />
          </FormFieldLabel>
          <FormikDebouncedInput
            required
            min={0}
            name="grantTotal"
            type="number"
          />
        </div>
      </FormSection>
      {!!shouldShowSection2 && (
        <FormSection title={<FormattedMessage defaultMessage="2. Define performance" />}>
          <Stack gap={32}>
            <Stack gap={48}>
              <Group
                align="center"
                gap={20}
              >
                <FormFieldLabel
                  required
                  htmlFor="startDate"
                >
                  <FormattedMessage defaultMessage="Performance period starts on" />
                </FormFieldLabel>
                <FormikDatePicker
                  required
                  maxDate={values.endDate}
                  name="startDate"
                />
                <FormFieldLabel
                  required
                  htmlFor="endDate"
                >
                  <FormattedMessage defaultMessage="and ends on" />
                </FormFieldLabel>
                <FormikDatePicker
                  required
                  minDate={values.startDate}
                  name="endDate"
                />
              </Group>
              {/* plan kpis */}
              <Stack gap={20}>
                <FormFieldLabel size="medium">
                  <div
                    css={css`
                      display: grid;
                      grid-template-columns: 500px 110px;
                    `}
                  >
                    <span>
                      <FormattedMessage defaultMessage="Performance metric" />
                    </span>
                    <span>
                      <FormattedMessage defaultMessage="Weight" />
                    </span>
                  </div>
                </FormFieldLabel>

                <KpiListForm isLtiPlanLocked={isLtiPlanLocked} />
              </Stack>
            </Stack>
            {!!values.kpis && values.kpis.length > 0 && !!values.kpis[0].name && (
              <Fragment>
                <Divider.Horizontal
                  grayShade={100}
                  css={css`
                    margin-left: -48px;
                    width: calc(100% + 96px);
                  `}
                />
                {values.kpis
                  .filter((kpi) => !!kpi.name)
                  .map((kpi, kpiIndex) => (
                    <Stack
                      key={kpi.id}
                      gap={24}
                    >
                      <Typography variant="bodyLargeMedium">{kpi.name}</Typography>
                      <Group
                        align="center"
                        gap={60}
                        css={css`
                          margin-right: 32px;
                        `}
                      >
                        <Stack gap={32}>
                          <RadioButtonGroup
                            name=""
                            size="small"
                            value={kpi.configuration.type}
                            options={[
                              { label: 'Linear', value: 'LINEAR' },
                              { label: 'Tier', value: 'TIER' },
                            ]}
                          />
                          <KpiPerformanceLinearForm
                            isLtiPlanLocked={isLtiPlanLocked}
                            kpi={kpi}
                            kpiCurrencySymbol={currencySymbol}
                            kpiIndex={kpiIndex}
                          />
                        </Stack>
                        <KpiPerformanceChart
                          hasError={!!get(errors, `kpis[${kpiIndex}].configuration.steps`)}
                          kpi={kpi}
                          kpiCurrencySymbol={currencySymbol}
                          style={{
                            width: '100%',
                            maxWidth: '440px',
                            height: '100%',
                            maxHeight: '200px',
                            aspectRatio: 1.618,
                          }}
                        />
                      </Group>
                    </Stack>
                  ))}
              </Fragment>
            )}
            <Stack />
          </Stack>
        </FormSection>
      )}
      {!!shouldShowSection3 && (
        <FormSection title={<FormattedMessage defaultMessage="3. Schedule reward" />}>
          <Group
            align="center"
            gap={32}
          >
            <FormFieldLabel
              required
              htmlFor="earningDate"
            >
              <FormattedMessage defaultMessage="Preliminary reward date" />
            </FormFieldLabel>
            <FormikDatePicker
              required
              minDate={values.endDate}
              name="earningDate"
              placeholder={formatMessage({ defaultMessage: 'Preliminary reward date' })}
            />
          </Group>
        </FormSection>
      )}
      {!!shouldShowSection4 && (
        <FormSection title={<FormattedMessage defaultMessage="4. Eligibility (cliff & minimum service) " />}>
          <Stack gap={48}>
            <FormikSwitch
              label={<FormattedMessage defaultMessage="Minimum service required before any entitlement" />}
              name="isCliffEnabled"
            />
            {values.isCliffEnabled ? (
              <Group
                align="center"
                gap={32}
              >
                <FormFieldLabel
                  required
                  htmlFor="eligibilityDate"
                >
                  <FormattedMessage defaultMessage="Participants become eligible for the plan from" />
                </FormFieldLabel>
                <FormikDatePicker
                  required
                  maxDate={values.endDate}
                  minDate={values.startDate}
                  name="eligibilityDate"
                  placeholder={formatMessage({ defaultMessage: 'Eligibility start date' })}
                />
              </Group>
            ) : (
              <Typography
                variant="bodyBaseRegular"
                css={(theme: Theme) => css`
                  font-style: italic;
                  color: ${theme.ds.colors.gray[800]};
                `}
              >
                <FormattedMessage defaultMessage="Participants are eligible for shares from grant date. " />
              </Typography>
            )}
          </Stack>
        </FormSection>
      )}
    </Stack>
  );
});