diff --git a/packages/admin-panel/src/VizBuilderApp/api/queries/useReportPreview.js b/packages/admin-panel/src/VizBuilderApp/api/queries/useReportPreview.js index da479710a6..8aecefc140 100644 --- a/packages/admin-panel/src/VizBuilderApp/api/queries/useReportPreview.js +++ b/packages/admin-panel/src/VizBuilderApp/api/queries/useReportPreview.js @@ -2,6 +2,7 @@ * Tupaia * Copyright (c) 2017 - 2021 Beyond Essential Systems Pty Ltd */ +import moment from 'moment'; import { useQuery } from 'react-query'; import { post } from '../api'; import { DEFAULT_REACT_QUERY_OPTIONS } from '../constants'; @@ -21,12 +22,14 @@ export const useReportPreview = ({ useQuery( ['fetchReportPreviewData', visualisation], async () => { + const today = moment().format('YYYY-MM-DD'); + const endDateToUse = endDate ?? today; // default to today if no end date is provided, so that we are getting data in the user's timezone, not UTC const response = await post('fetchReportPreviewData', { params: { entityCode: location, hierarchy: project, startDate, - endDate, + endDate: endDateToUse, dashboardItemOrMapOverlay, previewMode, permissionGroup: visualisation.permissionGroup || visualisation.reportPermissionGroup, diff --git a/packages/admin-panel/src/VizBuilderApp/components/DashboardItem/DashboardItemMetadataForm.jsx b/packages/admin-panel/src/VizBuilderApp/components/DashboardItem/DashboardItemMetadataForm.jsx index bfd93c9238..881f8e4e6a 100644 --- a/packages/admin-panel/src/VizBuilderApp/components/DashboardItem/DashboardItemMetadataForm.jsx +++ b/packages/admin-panel/src/VizBuilderApp/components/DashboardItem/DashboardItemMetadataForm.jsx @@ -4,19 +4,23 @@ */ import React, { useState } from 'react'; import PropTypes from 'prop-types'; -import { useForm } from 'react-hook-form'; +import { useForm, Controller } from 'react-hook-form'; import { Autocomplete, TextField } from '@tupaia/ui-components'; import { useSearchPermissionGroups } from '../../api/queries'; import { useVizConfigContext } from '../../context'; import { useDebounce } from '../../../utilities'; import { DASHBOARD_ITEM_VIZ_TYPES } from '../../constants'; +import { REQUIRED_FIELD_ERROR } from '../../../editor/validation'; export const DashboardItemMetadataForm = ({ Header, Body, Footer, onSubmit }) => { const vizTypeOptions = Object.entries(DASHBOARD_ITEM_VIZ_TYPES).map(([vizType, { name }]) => ({ value: vizType, label: name, })); - const { handleSubmit, register, errors } = useForm(); + const { handleSubmit, register, errors, control } = useForm({ + mode: 'onChange', + }); + const [ { visualisation, vizType }, { setVisualisationValue, setVizType, setPresentation, setPresentationValue }, @@ -35,11 +39,10 @@ export const DashboardItemMetadataForm = ({ Header, Body, Footer, onSubmit }) => const doSubmit = data => { setVisualisationValue('code', data.code); setVisualisationValue('permissionGroup', data.permissionGroup); - const selectedVizType = vizTypeOptions.find(({ label }) => label === data.vizType).value; - setVizType(selectedVizType); + setVizType(data.vizType.value); if (Object.keys(presentation).length === 0) { // If no presentation config exists, set the initial config by vizType - setPresentation(DASHBOARD_ITEM_VIZ_TYPES[selectedVizType].initialConfig); + setPresentation(DASHBOARD_ITEM_VIZ_TYPES[data.vizType.value].initialConfig); } setPresentationValue('name', data.name); onSubmit(); @@ -54,53 +57,82 @@ export const DashboardItemMetadataForm = ({ Header, Body, Footer, onSubmit }) => label="Code" defaultValue={code} error={!!errors.code} + required helperText={errors.code && errors.code.message} inputRef={register({ - required: 'Required', + required: REQUIRED_FIELD_ERROR, })} /> - p.name)} - disabled={isLoadingPermissionGroups} - error={!!errors.permissionGroup} - helperText={errors.permissionGroup && errors.permissionGroup.message} - inputRef={register({ - required: 'Required', - })} - value={permissionGroupSearchInput} - onInputChange={(event, newValue) => { - setPermissionGroupSearchInput(newValue); + rules={{ required: REQUIRED_FIELD_ERROR }} + render={({ onChange, value, ref, name }) => { + return ( + p.name)} + disabled={isLoadingPermissionGroups} + error={!!errors.permissionGroup} + helperText={errors.permissionGroup && errors.permissionGroup.message} + inputRef={ref} + inputValue={permissionGroupSearchInput} + onInputChange={(event, newValue) => { + setPermissionGroupSearchInput(newValue); + }} + onChange={(event, newValue) => { + onChange(newValue); + }} + value={value} + /> + ); }} /> - value === vizType)} - options={vizTypeOptions} - getOptionLabel={option => option.label} - getOptionSelected={option => option.value} - error={!!errors.vizType} - helperText={errors.vizType && errors.vizType.message} - inputRef={register({ - required: 'Required', - })} + control={control} + required + defaultValue={vizTypeOptions.find(({ value: optionValue }) => optionValue === vizType)} + rules={{ required: REQUIRED_FIELD_ERROR }} + render={({ onChange, value, ref, name }) => ( + option.label} + getOptionSelected={option => { + return option.value === value; + }} + error={!!errors.vizType} + helperText={errors.vizType && errors.vizType.message} + inputRef={ref} + onChange={(event, newValue) => { + onChange(newValue); + }} + value={value} + /> + )} />