From d07664195dfc396d5c882368b94c9a92e1c830f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilia=20M=C3=A4kel=C3=A4?= Date: Fri, 13 Oct 2023 15:58:28 +0300 Subject: [PATCH] Fix area search creation date limitations - Disable past dates - Allow selecting the same date for both start and end - Allow omitting the end date --- .../components/AreaSearchApplicationCreatePage.js | 1 + .../components/AreaSearchApplicationCreateSpecs.js | 7 +++++-- src/components/form/FieldTypeDatePicker.js | 6 ++++++ src/components/form/FormField.js | 12 +++++++++++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/areaSearch/components/AreaSearchApplicationCreatePage.js b/src/areaSearch/components/AreaSearchApplicationCreatePage.js index 82c0e68b7..d275c3a2f 100644 --- a/src/areaSearch/components/AreaSearchApplicationCreatePage.js +++ b/src/areaSearch/components/AreaSearchApplicationCreatePage.js @@ -193,6 +193,7 @@ class AreaSearchApplicationCreatePage extends Component { createAreaSearchSpecs({ area_search_attachments: attachments.map((attachment) => attachment.id), ...specsFormValues, + end_date: specsFormValues.end_date || null, }); } }; diff --git a/src/areaSearch/components/AreaSearchApplicationCreateSpecs.js b/src/areaSearch/components/AreaSearchApplicationCreateSpecs.js index 5408aa7af..7122938c9 100644 --- a/src/areaSearch/components/AreaSearchApplicationCreateSpecs.js +++ b/src/areaSearch/components/AreaSearchApplicationCreateSpecs.js @@ -25,6 +25,7 @@ import AddFileButton from '$components/form/AddFileButton'; import RemoveButton from '$components/form/RemoveButton'; import type {UploadedFileMeta} from '$src/application/types'; import {nonEmptyGeometry} from '$src/areaSearch/validators'; +import {startOfToday} from 'date-fns'; type OwnProps = { onFileAdded: Function, @@ -95,6 +96,7 @@ class AreaSearchApplicationCreateSpecs extends Component { } const geometryHasError = geometryError && (isSaveClicked || formMeta.geometry?.touched); + const today = startOfToday(); return (
@@ -128,6 +130,7 @@ class AreaSearchApplicationCreateSpecs extends Component { label: 'Vuokra-ajan alkupvm', read_only: false, }} + minDate={today} /> @@ -139,11 +142,11 @@ class AreaSearchApplicationCreateSpecs extends Component { fieldAttributes={get(attributes, 'end_date')} name='end_date' overrideValues={{ - required: true, fieldType: FieldTypes.DATE, label: 'Vuokra-ajan loppupvm', read_only: false, }} + minDate={today} /> @@ -252,7 +255,7 @@ export default (flowRight( validate: (values) => { const errors = {}; - if (values.start_date && values.end_date && values.start_date >= values.end_date) { + if (values.start_date && values.end_date && values.start_date > values.end_date) { errors.start_date = 'Alkupäivämäärän on oltava ennen loppupäivämäärää'; errors.end_date = 'Loppupäivämäärän on oltava ennen alkupäivämäärää'; } diff --git a/src/components/form/FieldTypeDatePicker.js b/src/components/form/FieldTypeDatePicker.js index 79c423586..f88460894 100644 --- a/src/components/form/FieldTypeDatePicker.js +++ b/src/components/form/FieldTypeDatePicker.js @@ -14,6 +14,8 @@ type Props = { displayError: boolean, input: Object, isDirty: boolean, + minDate?: Date, + maxDate?: Date, placeholder?: string, setRefForField?: Function, } @@ -23,6 +25,8 @@ const FieldTypeDatePicker = ({ displayError = false, input: {name, onChange, value}, isDirty = false, + minDate, + maxDate, placeholder, setRefForField, }: Props): React$Node => { @@ -77,6 +81,8 @@ const FieldTypeDatePicker = ({ onChangeRaw={handleChange} onSelect={handleSelect} placeholderText={placeholder} + minDate={minDate} + maxDate={maxDate} />
); diff --git a/src/components/form/FormField.js b/src/components/form/FormField.js index 8f2fee7a5..381b51df7 100644 --- a/src/components/form/FormField.js +++ b/src/components/form/FormField.js @@ -106,6 +106,8 @@ type InputProps = { label: ?string, language?: string, meta: Object, + minDate?: Date, + maxDate?: Date, multiSelect?: boolean, optionLabel?: string, options: ?Array, @@ -140,6 +142,8 @@ const FormFieldInput = ({ label, language, meta, + minDate, + maxDate, multiSelect, optionLabel, options, @@ -234,7 +238,7 @@ const FormFieldInput = ({ }
- {createElement(fieldComponent, {autoBlur, autoComplete, displayError, disabled, filterOption, input, isDirty, isLoading, label, language, multiSelect, optionLabel, placeholder, options, rows, setRefForField, type, valueSelectedCallback})} + {createElement(fieldComponent, {autoBlur, autoComplete, displayError, disabled, filterOption, input, isDirty, isLoading, label, language, minDate, maxDate, multiSelect, optionLabel, placeholder, options, rows, setRefForField, type, valueSelectedCallback})} {unit && {unit}}
{displayError && } @@ -285,6 +289,8 @@ type Props = { isLoading?: boolean, isMulti?: boolean, language?: string, + minDate?: Date, + maxDate?: Date, name: string, onBlur?: Function, onChange?: Function, @@ -401,6 +407,8 @@ class FormField extends PureComponent { invisibleLabel, isLoading, language, + minDate, + maxDate, name, onBlur, onChange, @@ -445,6 +453,8 @@ class FormField extends PureComponent { isLoading={isLoading} label={label} language={language} + minDate={minDate} + maxDate={maxDate} name={name} normalize={this.handleGenericNormalize} onBlur={onBlur}