From 26cdcc1ae043afbb591c0fa9ded8dbfd20bcd476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Sandvik?= Date: Sat, 16 Sep 2023 14:07:01 +0200 Subject: [PATCH 1/7] chore: period type select refactor --- src/actions/layerEdit.js | 4 +-- src/components/periods/PeriodTypeSelect.js | 34 ++++++++++++---------- src/constants/periods.js | 14 +++++---- src/reducers/layerEdit.js | 6 ++-- src/util/periods.js | 6 ++-- 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/actions/layerEdit.js b/src/actions/layerEdit.js index 80037f548..e6fd006ad 100644 --- a/src/actions/layerEdit.js +++ b/src/actions/layerEdit.js @@ -179,10 +179,10 @@ export const setPeriodName = (periodName) => ({ }) // Set period type (thematic) -export const setPeriodType = (periodType, clearPeriod = true) => ({ +export const setPeriodType = (periodType, keepPeriod) => ({ type: types.LAYER_EDIT_PERIOD_TYPE_SET, periodType, - clearPeriod, + keepPeriod, }) // Set period (event & thematic) diff --git a/src/components/periods/PeriodTypeSelect.js b/src/components/periods/PeriodTypeSelect.js index 3c04c6e98..3b146f5de 100644 --- a/src/components/periods/PeriodTypeSelect.js +++ b/src/components/periods/PeriodTypeSelect.js @@ -1,34 +1,38 @@ import i18n from '@dhis2/d2-i18n' import PropTypes from 'prop-types' import React, { useEffect } from 'react' -import { RELATIVE_PERIODS } from '../../constants/periods.js' import { getPeriodTypes, getRelativePeriods } from '../../util/periods.js' import { SelectField } from '../core/index.js' +import { useSystemSettings } from '../SystemSettingsProvider.js' const PeriodTypeSelect = ({ onChange, className, errorText, - hiddenPeriods, + includeRelativePeriods = true, period, value, }) => { + const { hiddenPeriods } = useSystemSettings() + + const periodTypes = useMemo( + () => getPeriodTypes(includeRelativePeriods, hiddenPeriods), + [includeRelativePeriods, hiddenPeriods] + ) + + // Set default period type useEffect(() => { - const relativePeriodType = { - id: RELATIVE_PERIODS, - name: i18n.t('Relative'), - } + if (!value) { + const isRelativePeriod = + includeRelativePeriods && + getRelativePeriods().find((p) => p.id === period.id) - if (!value && period) { - if (getRelativePeriods().find((p) => p.id === period.id)) { - // false will not clear the period dropdown - onChange(relativePeriodType, false) + if (!period || isRelativePeriod) { + // default to first period type + onChange(periodTypes[0], isRelativePeriod) } - } else if (!value) { - // set relativePeriods as default - onChange(relativePeriodType) } - }, [value, period, onChange]) + }, [value, period, includeRelativePeriods, onChange]) return ( [ - { - id: RELATIVE_PERIODS, - name: i18n.t('Relative'), - }, +export const periodTypes = (includeRelativePeriods) => [ + ...(includeRelativePeriods + ? [ + { + id: RELATIVE_PERIODS, + name: i18n.t('Relative'), + }, + ] + : []), { id: DAILY, name: i18n.t('Daily'), diff --git a/src/reducers/layerEdit.js b/src/reducers/layerEdit.js index 2549cbb36..f40823e43 100644 --- a/src/reducers/layerEdit.js +++ b/src/reducers/layerEdit.js @@ -107,9 +107,9 @@ const layerEdit = (state = null, action) => { return { ...state, periodType: action.periodType.id, - filters: action.clearPeriod - ? removePeriodFromFilters(state.filters) - : state.filters, + filters: action.keepPeriod + ? state.filters + : removePeriodFromFilters(state.filters), } case types.LAYER_EDIT_PERIOD_SET: diff --git a/src/util/periods.js b/src/util/periods.js index 777ab1bd6..cfc89b3e8 100644 --- a/src/util/periods.js +++ b/src/util/periods.js @@ -13,8 +13,10 @@ const filterPeriods = (periods, firstDate, lastDate) => (!lastDate || p.endDate <= lastDate) ) -export const getPeriodTypes = (hiddenPeriods = []) => - periodTypes().filter(({ group }) => !hiddenPeriods.includes(group)) +export const getPeriodTypes = (includeRelativePeriods, hiddenPeriods = []) => + periodTypes(includeRelativePeriods).filter( + ({ group }) => !hiddenPeriods.includes(group) + ) export const getFixedPeriodsByType = ({ periodType, From 7584d7525aa6fed5013b6f73ab1cc8b545a4ef79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Sandvik?= Date: Sat, 16 Sep 2023 14:18:28 +0200 Subject: [PATCH 2/7] chore: add missing dependency --- src/components/periods/PeriodTypeSelect.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/periods/PeriodTypeSelect.js b/src/components/periods/PeriodTypeSelect.js index 3b146f5de..e6d34a895 100644 --- a/src/components/periods/PeriodTypeSelect.js +++ b/src/components/periods/PeriodTypeSelect.js @@ -1,6 +1,6 @@ import i18n from '@dhis2/d2-i18n' import PropTypes from 'prop-types' -import React, { useEffect } from 'react' +import React, { useMemo, useEffect } from 'react' import { getPeriodTypes, getRelativePeriods } from '../../util/periods.js' import { SelectField } from '../core/index.js' import { useSystemSettings } from '../SystemSettingsProvider.js' From d5d6dda102b8edb35cbb3909a4ce7e91998157a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Sandvik?= Date: Sat, 16 Sep 2023 14:20:32 +0200 Subject: [PATCH 3/7] chore: add missing dependency --- src/components/periods/PeriodTypeSelect.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/periods/PeriodTypeSelect.js b/src/components/periods/PeriodTypeSelect.js index e6d34a895..d3455cd33 100644 --- a/src/components/periods/PeriodTypeSelect.js +++ b/src/components/periods/PeriodTypeSelect.js @@ -32,7 +32,7 @@ const PeriodTypeSelect = ({ onChange(periodTypes[0], isRelativePeriod) } } - }, [value, period, includeRelativePeriods, onChange]) + }, [value, period, periodTypes, includeRelativePeriods, onChange]) return ( Date: Sat, 16 Sep 2023 14:39:13 +0200 Subject: [PATCH 4/7] chore: code cleaning --- src/components/edit/thematic/ThematicDialog.js | 2 +- src/components/periods/PeriodTypeSelect.js | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/components/edit/thematic/ThematicDialog.js b/src/components/edit/thematic/ThematicDialog.js index 7470902a9..a06e6f7be 100644 --- a/src/components/edit/thematic/ThematicDialog.js +++ b/src/components/edit/thematic/ThematicDialog.js @@ -430,7 +430,7 @@ class ThematicDialog extends Component { { @@ -23,9 +23,13 @@ const PeriodTypeSelect = ({ // Set default period type useEffect(() => { if (!value) { - const isRelativePeriod = + const isRelativePeriod = !!( includeRelativePeriods && + period && getRelativePeriods().find((p) => p.id === period.id) + ) + + console.log('isRelativePeriod', isRelativePeriod) if (!period || isRelativePeriod) { // default to first period type From 5332da62ead9cd7ebe79862dc63e1ada0a740988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Sandvik?= Date: Sat, 16 Sep 2023 14:52:23 +0200 Subject: [PATCH 5/7] chore: hidden periods fix --- src/components/periods/PeriodTypeSelect.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/components/periods/PeriodTypeSelect.js b/src/components/periods/PeriodTypeSelect.js index cc420a735..89cf0e392 100644 --- a/src/components/periods/PeriodTypeSelect.js +++ b/src/components/periods/PeriodTypeSelect.js @@ -29,8 +29,6 @@ const PeriodTypeSelect = ({ getRelativePeriods().find((p) => p.id === period.id) ) - console.log('isRelativePeriod', isRelativePeriod) - if (!period || isRelativePeriod) { // default to first period type onChange(periodTypes[0], isRelativePeriod) @@ -41,7 +39,7 @@ const PeriodTypeSelect = ({ return ( Date: Sat, 16 Sep 2023 14:59:37 +0200 Subject: [PATCH 6/7] chore: lint fix --- src/components/edit/thematic/ThematicDialog.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/edit/thematic/ThematicDialog.js b/src/components/edit/thematic/ThematicDialog.js index a06e6f7be..77b089376 100644 --- a/src/components/edit/thematic/ThematicDialog.js +++ b/src/components/edit/thematic/ThematicDialog.js @@ -225,7 +225,6 @@ class ThematicDialog extends Component { noDataColor, operand, periodType, - settings, renderingStrategy, startDate, endDate, From c2557ccfcf6b307a58e72debe67e6cdf3a369a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Sandvik?= Date: Wed, 27 Sep 2023 16:55:47 +0200 Subject: [PATCH 7/7] chore: pot file --- i18n/en.pot | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/i18n/en.pot b/i18n/en.pot index fbce2a3d2..7686c57f4 100644 --- a/i18n/en.pot +++ b/i18n/en.pot @@ -5,8 +5,8 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" -"POT-Creation-Date: 2023-09-06T13:08:55.205Z\n" -"PO-Revision-Date: 2023-09-06T13:08:55.205Z\n" +"POT-Creation-Date: 2023-09-27T14:55:16.411Z\n" +"PO-Revision-Date: 2023-09-27T14:55:16.411Z\n" msgid "Untitled map, {{date}}" msgstr "Untitled map, {{date}}" @@ -802,9 +802,6 @@ msgstr "Previous year" msgid "Next year" msgstr "Next year" -msgid "Relative" -msgstr "Relative" - msgid "Period type" msgstr "Period type" @@ -1202,6 +1199,9 @@ msgstr "Equal counts" msgid "Symbol" msgstr "Symbol" +msgid "Relative" +msgstr "Relative" + msgid "Daily" msgstr "Daily"