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 | import { css } from '@emotion/react'; import { IconPencil } from '@tabler/icons-react'; import { memo, useCallback, useContext, useMemo } from 'react'; import { FormattedMessage, useIntl } from 'react-intl'; import { Table, type SelectOption } from '@allshares/studio-design-system'; import { type Overwrite, type PaymentContract } from '@amalia/core/types'; import { type RecordContent, type RecordContentPropertyType } from '@amalia/data-capture/connectors/types'; import { FormatsEnum } from '@amalia/data-capture/fields/types'; import { OverwriteTooltip } from '@amalia/data-correction/overwrites/components'; import { OverwriteScopeEnum } from '@amalia/data-correction/overwrites/types'; import { formatDate } from '@amalia/ext/dates'; import { useBoolState } from '@amalia/ext/react/hooks'; import { canOverwritePayments } from '@amalia/kernel/auth/shared'; import { AbilityContext } from '@amalia/kernel/auth/state'; import { useClearPayment, usePatchPayment } from '@amalia/payout-calculation/payments/state'; import { formatPeriodName } from '@amalia/payout-definition/periods/shared'; import { type PeriodsMap } from '@amalia/payout-definition/periods/types'; import { formatUserFullName } from '@amalia/tenants/users/types'; import { PaymentErrorIcon } from './PaymentErrorIcon'; const getPeriodName = (periodsMap: PeriodsMap, value: RecordContent | RecordContentPropertyType) => typeof value === 'string' && value in periodsMap ? periodsMap[value].name : undefined; interface PaymentPeriodCellProps { readonly payment: PaymentContract; readonly periodsMap: PeriodsMap; } export const PaymentPeriodCell = memo(function PaymentPeriodCell({ payment, periodsMap }: PaymentPeriodCellProps) { const { mutate: patchPayment } = usePatchPayment(); const { mutate: clearPaymentOverwrite } = useClearPayment(); const { formatMessage } = useIntl(); const ability = useContext(AbilityContext); const { isEditMode, setEditModeTrue, setEditModeFalse } = useBoolState(false, 'editMode'); const onSubmit = useCallback( (paymentPeriodId: string | null) => { setEditModeFalse(); if (paymentPeriodId !== payment.paymentPeriodId) { patchPayment({ paymentId: payment.id, paymentPeriodId }); } }, [patchPayment, setEditModeFalse, payment], ); const options: SelectOption<string>[] = useMemo(() => { if (!isEditMode) { return []; } return Object.values(periodsMap) .sort((a, b) => b.startDate - a.startDate) .map((period) => ({ value: period.id, label: formatPeriodName(period.frequency, period.startDate), })); }, [periodsMap, isEditMode]); // FIXME: this specific endpoint does not return a PaymentContract but a PaymentContract with a complete populated overwrite. const overwrite = (payment.overwrites ?? []).find((o) => o.field === 'paymentPeriodId') as Overwrite | undefined; const clearOverwriteProxy = useCallback( () => clearPaymentOverwrite({ paymentId: payment.id, overwriteId: overwrite!.id }), [payment, clearPaymentOverwrite, overwrite], ); const value = overwrite ? ( <OverwriteTooltip author={formatUserFullName(overwrite.creator)} date={formatDate(overwrite.createdAt!, 'LLL')} handleOnClick={clearOverwriteProxy} isReadOnly={false} isSimulation={overwrite.scope === OverwriteScopeEnum.FORECAST} newValue={getPeriodName(periodsMap, overwrite.overwriteValue) || formatMessage({ defaultMessage: 'None' })} oldValue={getPeriodName(periodsMap, overwrite.sourceValue) || formatMessage({ defaultMessage: 'None' })} valueFormat={FormatsEnum.text} > <div css={(theme) => css` color: ${theme.ds.colors.primary[400]}; `} > {getPeriodName(periodsMap, overwrite.overwriteValue) || formatMessage({ defaultMessage: 'None' })} </div> </OverwriteTooltip> ) : payment.paymentPeriod ? ( formatPeriodName(payment.paymentPeriod.frequency, payment.paymentPeriod.startDate) ) : ( formatMessage({ defaultMessage: 'None' }) ); const actions = useMemo(() => { const actionsList = []; if (canOverwritePayments(ability)) { actionsList.push( <Table.Cell.IconAction key="overwrite" icon={<IconPencil />} label={<FormattedMessage defaultMessage="Overwrite" />} onClick={setEditModeTrue} />, ); } if (payment.error) { actionsList.push( <PaymentErrorIcon key="error" paymentError={payment.error} />, ); } return actionsList.length > 0 ? actionsList : null; }, [ability, payment.error, setEditModeTrue]); return isEditMode ? ( <Table.Cell.Select<SelectOption<string>> options={options} placeholder={formatMessage({ defaultMessage: 'None' })} row={undefined} value={payment.paymentPeriodId} onChange={onSubmit} /> ) : actions ? ( <Table.Cell.WithActions actions={actions} hideActions={!payment.error} > {value} </Table.Cell.WithActions> ) : ( <Table.Cell.Text>{value}</Table.Cell.Text> ); }); |