|
| 1 | +import React, { Fragment } from 'react' |
| 2 | + |
| 3 | +import { useTranslation } from 'employee-frontend/state/i18n' |
| 4 | +import { Result } from 'lib-common/api' |
| 5 | +import { localDateRange } from 'lib-common/form/fields' |
| 6 | +import { object, required } from 'lib-common/form/form' |
| 7 | +import { useForm, useFormFields } from 'lib-common/form/hooks' |
| 8 | +import { StateOf } from 'lib-common/form/types' |
| 9 | +import { AsyncButton } from 'lib-components/atoms/buttons/AsyncButton' |
| 10 | +import { Button } from 'lib-components/atoms/buttons/Button' |
| 11 | +import ButtonContainer from 'lib-components/layout/ButtonContainer' |
| 12 | +import { DateRangePickerF } from 'lib-components/molecules/date-picker/DateRangePicker' |
| 13 | +import { Gap } from 'lib-components/white-space' |
| 14 | + |
| 15 | +const outOfOfficeForm = object({ period: required(localDateRange()) }) |
| 16 | + |
| 17 | +function initialFormState(): StateOf<typeof outOfOfficeForm> { |
| 18 | + return { period: localDateRange.empty() } |
| 19 | +} |
| 20 | + |
| 21 | +export interface OutOfOfficeEditorProps { |
| 22 | + onCancel: () => void |
| 23 | +} |
| 24 | + |
| 25 | +export default React.memo(function OutOfOfficeEditor({ |
| 26 | + onCancel |
| 27 | +}: OutOfOfficeEditorProps) { |
| 28 | + const { i18n, lang } = useTranslation() |
| 29 | + |
| 30 | + const form = useForm(outOfOfficeForm, initialFormState, { |
| 31 | + ...i18n.validationErrors, |
| 32 | + ...i18n.components.datePicker.validationErrors |
| 33 | + }) |
| 34 | + |
| 35 | + const { period } = useFormFields(form) |
| 36 | + |
| 37 | + return ( |
| 38 | + <Fragment> |
| 39 | + <DateRangePickerF bind={period} locale={lang} hideErrorsBeforeTouched /> |
| 40 | + <Gap size="X4L" /> |
| 41 | + <ButtonContainer> |
| 42 | + <AsyncButton |
| 43 | + text={i18n.common.save} |
| 44 | + primary |
| 45 | + disabled={!form.isValid()} |
| 46 | + onClick={function (): void | Promise<Result<unknown>> { |
| 47 | + throw new Error('Function not implemented.') |
| 48 | + }} |
| 49 | + onSuccess={function (): void { |
| 50 | + throw new Error('Function not implemented.') |
| 51 | + }} |
| 52 | + /> |
| 53 | + <Gap size="xs" horizontal /> |
| 54 | + <Button |
| 55 | + text={i18n.common.cancel} |
| 56 | + appearance="inline" |
| 57 | + onClick={onCancel} |
| 58 | + /> |
| 59 | + </ButtonContainer> |
| 60 | + </Fragment> |
| 61 | + ) |
| 62 | +}) |
0 commit comments