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 | 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 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 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 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x | import { css } from '@emotion/react';
import { IconCheck, IconSettings } from '@tabler/icons-react';
import { Form } from 'formik';
import { sortBy } from 'lodash-es';
import { memo, useCallback, useMemo } from 'react';
import { FormattedMessage, useIntl } from 'react-intl';
import { generatePath } from 'react-router-dom';
import { AlertBanner, FormLayout, Modal, useSnackbars, type SelectOption } from '@allshares/studio-design-system';
import { routes } from '@amalia/core/routes';
import { FormikInput, FormikSelect } from '@amalia/design-system/ext';
import { FormikForm } from '@amalia/ext/formik';
import { useNavigate } from '@amalia/ext/react-router-dom';
import { nameGenerator } from '@amalia/ext/string';
import { toError } from '@amalia/ext/typescript';
import { PlanNameWithVisibility } from '@amalia/payout-definition/plans/components';
import { type Plan } from '@amalia/payout-definition/plans/types';
import { usePlansList } from '@amalia/payout-definition/state';
import { useCreatePlanAgreement, useFindPlanAgreements } from '@amalia/plan-agreements/state';
import { createPlanAgreementSchema, type CreatePlanAgreementRequest } from '@amalia/plan-agreements/types';
import { usePlanAgreementsModalsContext } from './PlanAgreementsModals.context';
const DEFAULT_AGREEMENT_NAME = 'New Agreement';
export const PlanAgreementsCreateModal = memo(function PlanAgreementsCreateModal() {
const navigate = useNavigate();
const { formatMessage } = useIntl();
const { snackError } = useSnackbars();
const { closeModal } = usePlanAgreementsModalsContext();
const { mutateAsync: createPlanAgreement, isPending } = useCreatePlanAgreement();
const { data: planAgreements } = useFindPlanAgreements();
const { data: plans = [] } = usePlansList();
const planOptions: SelectOption<Plan['id']>[] = useMemo(
() =>
sortBy(
plans
.filter((plan) => !plan.archived)
.map((plan) => ({
label: (
<PlanNameWithVisibility
isPlanHidden={plan.isHidden}
planName={plan.name}
/>
),
value: plan.id,
key: plan.id,
filterLabel: plan.name,
})),
'label',
),
[plans],
);
const formInitialValues: CreatePlanAgreementRequest = useMemo(
() => ({
planId: '',
name: nameGenerator(DEFAULT_AGREEMENT_NAME, planAgreements?.map((planAgreement) => planAgreement.name) ?? []),
}),
[planAgreements],
);
const sanitizePlanAgreementArgs = useCallback(
(planAgreementArgs: CreatePlanAgreementRequest) => ({
...planAgreementArgs,
planId: planAgreementArgs.planId || null,
}),
[],
);
const handleFormSubmitWithRedirect = useCallback(
async (newPlanAgreementArgs: CreatePlanAgreementRequest) => {
try {
const { id: createdPlanAgreementId } = await createPlanAgreement(
sanitizePlanAgreementArgs(newPlanAgreementArgs),
);
closeModal();
navigate(generatePath(routes.PLAN_AGREEMENT_EDIT, { planAgreementId: createdPlanAgreementId }));
} catch (error) {
snackError(toError(error).message);
}
},
[closeModal, createPlanAgreement, navigate, snackError, sanitizePlanAgreementArgs],
);
const handleFormSubmitWithoutRedirect = useCallback(
(newPlanAgreementArgs: CreatePlanAgreementRequest) => async () => {
try {
await createPlanAgreement(sanitizePlanAgreementArgs(newPlanAgreementArgs));
closeModal();
} catch (error) {
snackError(toError(error).message);
}
},
[closeModal, createPlanAgreement, snackError, sanitizePlanAgreementArgs],
);
return (
<Modal
isOpen
onClose={closeModal}
>
<FormikForm
initialValues={formInitialValues}
validationSchema={createPlanAgreementSchema}
onSubmit={handleFormSubmitWithRedirect}
>
{({ isValid, values }) => {
const isSubmitButtonDisabled = isPending || !isValid;
return (
<Form
css={css`
display: contents;
`}
>
<Modal.Content>
<Modal.Header>
<Modal.Title>
<FormattedMessage defaultMessage="New agreement" />
</Modal.Title>
</Modal.Header>
<Modal.Body>
<FormLayout>
<FormLayout.Group>
<AlertBanner inline>
<FormattedMessage defaultMessage="You will be able to edit this information after creation if needed." />
</AlertBanner>
<FormikInput
required
id="name"
label={<FormattedMessage defaultMessage="Name" />}
name="name"
placeholder={formatMessage({ defaultMessage: 'New agreement' })}
/>
<FormikSelect
id="associated-plan-id"
label={<FormattedMessage defaultMessage="Plan" />}
name="planId"
options={planOptions}
/>
</FormLayout.Group>
</FormLayout>
</Modal.Body>
</Modal.Content>
<Modal.Actions>
<Modal.CancelAction />
<Modal.MainAction
disabled={isSubmitButtonDisabled}
icon={<IconCheck />}
onClick={handleFormSubmitWithoutRedirect(values)}
>
<FormattedMessage defaultMessage="Create" />
</Modal.MainAction>
<Modal.MainAction
disabled={isSubmitButtonDisabled}
icon={<IconSettings />}
type="submit"
>
<FormattedMessage defaultMessage="Create and configure" />
</Modal.MainAction>
</Modal.Actions>
</Form>
);
}}
</FormikForm>
</Modal>
);
});
|