Skip to content

Commit

Permalink
fix: use string for date inputs [DHIS2-12489] (#1750)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp authored Jun 19, 2023
1 parent 17e453a commit 2c1d98b
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 59 deletions.
13 changes: 2 additions & 11 deletions src/components/DatePicker/DatePicker.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import { InputField } from '@dhis2/ui'
import PropTypes from 'prop-types'
import React from 'react'
import { jsDateToISO8601 } from '../../utils/helper.js'

const DatePicker = ({ name, error, label, date, onChange, dataTest }) => {
const onChangeHelper = ({ value }) => {
if (!value) {
onChange(value)
} else {
onChange(new Date(value))
}
}

const value = date && jsDateToISO8601(date)
const onChangeHelper = ({ value }) => onChange(value)

return (
<InputField
type="date"
name={name}
value={value}
value={date}
label={label}
onChange={onChangeHelper}
inputWidth="200px"
Expand Down
2 changes: 1 addition & 1 deletion src/components/DatePicker/DatePicker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const props = {
dataTest: 'date-picker',
name: 'file',
label: 'Start date',
date: new Date('10 Feb 2020 00:00:00 GMT'),
date: '2020-02-10',
onChange: () => 1,
}

Expand Down
25 changes: 10 additions & 15 deletions src/components/DatePicker/DatePickerField.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ const Wrapper = ({

Wrapper.propTypes = {
input: PropTypes.shape({
value: PropTypes.oneOfType([
PropTypes.instanceOf(Date),
PropTypes.string,
]),
value: PropTypes.string,
onChange: PropTypes.func,
}).isRequired,
inputName: PropTypes.string.isRequired,
Expand All @@ -45,17 +42,15 @@ Wrapper.propTypes = {
}).isRequired,
}

const DatePickerField = ({ name, validator, ...rest }) => {
return (
<Field
component={Wrapper}
name={name}
validate={validator}
inputName={name}
{...rest}
/>
)
}
const DatePickerField = ({ name, validator, ...rest }) => (
<Field
component={Wrapper}
name={name}
validate={validator}
inputName={name}
{...rest}
/>
)

DatePickerField.propTypes = {
name: PropTypes.string.isRequired,
Expand Down
15 changes: 9 additions & 6 deletions src/pages/DataExport/DataExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
ExportButton,
FormAlerts,
} from '../../components/Inputs/index.js'
import { jsDateToISO8601 } from '../../utils/helper.js'
import { onExport, validate } from './form-helper.js'

const { Form } = ReactFinalForm
Expand All @@ -44,18 +45,20 @@ export const PAGE_DESCRIPTION = i18n.t(
const PAGE_ICON = <DataIcon />

const today = new Date()
const threeMonthsBeforeToday = new Date(
today.getFullYear(),
today.getMonth() - 3,
today.getDate()
)

const initialValues = {
selectedOrgUnits: [],
includeChildren: true,
selectedDataSets: [],
format: defaultFormatOption,
compression: defaultCompressionOption,
startDate: new Date(
today.getFullYear(),
today.getMonth() - 3,
today.getDate()
),
endDate: today,
startDate: jsDateToISO8601(threeMonthsBeforeToday),
endDate: jsDateToISO8601(today),
includeDeleted: false,
dataElementIdScheme: defaultDataElementIdSchemeOption,
orgUnitIdScheme: defaultOrgUnitIdSchemeOption,
Expand Down
5 changes: 2 additions & 3 deletions src/pages/DataExport/form-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
locationAssign,
compressionToName,
fileFormatToFileExtension,
jsDateToISO8601,
pathToId,
} from '../../utils/helper.js'

Expand All @@ -32,8 +31,8 @@ const valuesToParams = (
`idScheme=${idScheme}`,
`includeDeleted=${includeDeleted}`,
`children=${includeChildren}`,
`startDate=${jsDateToISO8601(startDate)}`,
`endDate=${jsDateToISO8601(endDate)}`,
`startDate=${startDate}`,
`endDate=${endDate}`,
`orgUnit=${selectedOrgUnits.map((o) => pathToId(o))}`,
`dataSet=${selectedDataSets}`,
`format=${encodeURIComponent(format)}`,
Expand Down
15 changes: 9 additions & 6 deletions src/pages/EventExport/EventExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
IdScheme,
defaultIdSchemeOption,
} from '../../components/Inputs/index.js'
import { jsDateToISO8601 } from '../../utils/helper.js'
import { onExport, validate } from './form-helper.js'

const { Form } = ReactFinalForm
Expand All @@ -45,18 +46,20 @@ export const PAGE_DESCRIPTION = i18n.t(
const PAGE_ICON = <EventIcon />

const today = new Date()
const threeMonthsBeforeToday = new Date(
today.getFullYear(),
today.getMonth() - 3,
today.getDate()
)

const initialValues = {
selectedOrgUnits: [],
selectedPrograms: '',
programStage: undefined,
format: defaultFormatOption,
compression: defaultCompressionOption,
startDate: new Date(
today.getFullYear(),
today.getMonth() - 3,
today.getDate()
),
endDate: today,
startDate: jsDateToISO8601(threeMonthsBeforeToday),
endDate: jsDateToISO8601(today),
includeDeleted: false,
dataElementIdScheme: defaultDataElementIdSchemeOption,
orgUnitIdScheme: defaultOrgUnitIdSchemeOption,
Expand Down
10 changes: 3 additions & 7 deletions src/pages/EventExport/form-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import {
DATE_AFTER_VALIDATOR,
} from '../../components/DatePicker/DatePickerField.js'
import { ALL_VALUE } from '../../hooks/useProgramStages.js'
import {
jsDateToISO8601,
locationAssign,
pathToId,
} from '../../utils/helper.js'
import { locationAssign, pathToId } from '../../utils/helper.js'

const onExport = (baseUrl, setExportEnabled) => (values) => {
setExportEnabled(false)
Expand Down Expand Up @@ -42,8 +38,8 @@ const onExport = (baseUrl, setExportEnabled) => (values) => {
`orgUnitIdScheme=${orgUnitIdScheme}`,
`idScheme=${idScheme}`,
`attachment=${filename}`,
`startDate=${jsDateToISO8601(startDate)}`,
`endDate=${jsDateToISO8601(endDate)}`,
`startDate=${startDate}`,
`endDate=${endDate}`,
`ouMode=${inclusion}`,
`format=${format}`,
programStage != ALL_VALUE ? `programStage=${programStage}` : '',
Expand Down
15 changes: 5 additions & 10 deletions src/pages/TEIExport/form-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import {
DATE_AFTER_VALIDATOR,
} from '../../components/DatePicker/DatePickerField.js'
import { OU_MODE_MANUAL_VALUE } from '../../components/Inputs/index.js'
import {
locationAssign,
jsDateToISO8601,
pathToId,
} from '../../utils/helper.js'
import { locationAssign, pathToId } from '../../utils/helper.js'

// calculate minimum set of parameters based on given filters
const valuesToParams = (
Expand Down Expand Up @@ -78,11 +74,11 @@ const valuesToParams = (
minParams.followUpStatus = followUpStatus

if (programStartDate) {
minParams.programStartDate = jsDateToISO8601(programStartDate)
minParams.programStartDate = programStartDate
}

if (programEndDate) {
minParams.programEndDate = jsDateToISO8601(programEndDate)
minParams.programEndDate = programEndDate
}
}

Expand All @@ -92,12 +88,11 @@ const valuesToParams = (

if (lastUpdatedFilter == 'DATE') {
if (lastUpdatedStartDate) {
minParams.lastUpdatedStartDate =
jsDateToISO8601(lastUpdatedStartDate)
minParams.lastUpdatedStartDate = lastUpdatedStartDate
}

if (lastUpdatedEndDate) {
minParams.lastUpdatedEndDate = jsDateToISO8601(lastUpdatedEndDate)
minParams.lastUpdatedEndDate = lastUpdatedEndDate
}
}

Expand Down

0 comments on commit 2c1d98b

Please sign in to comment.