Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge: update branch with latest dev #5803

Merged
merged 9 commits into from
Jul 22, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down
63 changes: 42 additions & 21 deletions packages/admin-panel/src/editor/EditModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { withConnectedEditor } from './withConnectedEditor';
import { useValidationScroll } from './useValidationScroll';

export const EditModalComponent = ({
errorMessage,
error,
isOpen,
isLoading,
onDismiss,
Expand Down Expand Up @@ -49,26 +49,47 @@ export const EditModalComponent = ({
onEditField,
validationErrors,
);
const buttons = [
{
onClick: onDismiss,
text: errorMessage ? dismissButtonText : cancelButtonText,
disabled: isLoading,
variant: 'outlined',
id: 'form-button-cancel',
},
{
onClick: onSaveWithTouched,
id: 'form-button-save',
text: saveButtonText,
disabled: !!errorMessage || isLoading || isUnchanged,
},
];

const getButtons = () => {
if (error) {
return [
{
onClick: onDismiss,
text: dismissButtonText,
disabled: isLoading,
variant: 'contained',
id: 'form-button-cancel',
},
];
}
return [
{
onClick: onDismiss,
text: cancelButtonText,
disabled: isLoading,
variant: 'outlined',
id: 'form-button-cancel',
},
{
onClick: onSaveWithTouched,
id: 'form-button-save',
text: saveButtonText,
disabled: !!error || isLoading || isUnchanged,
},
];
};

const buttons = getButtons();

const generateModalTitle = () => {
if (title) return title;
if (isLoading) return '';
if (!resourceName) return isNew ? 'Add' : 'Edit';
if (error) {
const capitalisedResourceName = `${resourceName.charAt(0).toUpperCase()}${resourceName.slice(
1,
)}`;
return `${capitalisedResourceName} error`;
}
if (isNew) return `Add ${resourceName}`;
return `Edit ${resourceName}`;
};
Expand All @@ -77,7 +98,7 @@ export const EditModalComponent = ({

return (
<Modal
errorMessage={errorMessage}
error={error}
isLoading={isLoading}
onClose={onDismiss}
isOpen={isOpen}
Expand All @@ -99,7 +120,7 @@ export const EditModalComponent = ({
};

EditModalComponent.propTypes = {
errorMessage: PropTypes.string,
error: PropTypes.object,
isOpen: PropTypes.bool.isRequired,
isLoading: PropTypes.bool.isRequired,
onDismiss: PropTypes.func.isRequired,
Expand All @@ -122,13 +143,13 @@ EditModalComponent.propTypes = {
};

EditModalComponent.defaultProps = {
errorMessage: null,
error: null,
recordData: null,
FieldsComponent: null,
isUnchanged: false,
displayUsedBy: false,
usedByConfig: {},
dismissButtonText: 'Dismiss',
dismissButtonText: 'Close',
cancelButtonText: 'Cancel',
saveButtonText: 'Save',
extraDialogProps: null,
Expand Down
7 changes: 5 additions & 2 deletions packages/admin-panel/src/editor/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export const editField = (fieldSource, newValue) => (dispatch, getState) => {
};

export const saveEdits =
(endpoint, editedFields, isNew, filesByFieldKey = {}, onSuccess) =>
(endpoint, editedFields, isNew, filesByFieldKey = {}, onSuccess, onError) =>
async (dispatch, getState, { api }) => {
try {
const { recordData, fields } = getEditorState(getState());
Expand Down Expand Up @@ -245,8 +245,11 @@ export const saveEdits =
} catch (error) {
dispatch({
type: EDITOR_ERROR,
errorMessage: error.message,
error,
});
if (onError) {
onError(error);
}
}
};

Expand Down
19 changes: 9 additions & 10 deletions packages/admin-panel/src/editor/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from './constants';

const defaultState = {
errorMessage: '',
error: null,
isOpen: false,
isLoading: false,
endpoint: null,
Expand All @@ -42,7 +42,7 @@ const stateChanges = {
},
[EDITOR_DATA_EDIT_BEGIN]: payload => ({
isLoading: true,
errorMessage: '',
error: null,
...payload,
}),
[EDITOR_DATA_FETCH_SUCCESS]: payload => ({
Expand All @@ -57,16 +57,15 @@ const stateChanges = {
isLoading: false,
...payload,
}),
[EDITOR_DISMISS]: (payload, { errorMessage }) => {
if (errorMessage) {
return { errorMessage: defaultState.errorMessage }; // If there is an error, dismiss it
[EDITOR_DISMISS]: (payload, { error }) => {
if (error) {
return { error: defaultState.error }; // If there is an error, dismiss it
}
return defaultState; // If no error, dismiss the whole modal and clear its state
},
[LOAD_EDITOR]: payload => ({
...payload,
errorMessage: '',
}),
[LOAD_EDITOR]: payload => {
return { ...payload, error: null };
},
[OPEN_EDIT_MODAL]: ({ recordId }) => ({ recordId, isOpen: true }),
[EDITOR_FIELD_EDIT]: (
{ fieldKey, newValue, otherValidationErrorsToClear = [] },
Expand All @@ -89,7 +88,7 @@ const stateChanges = {
[SET_VALIDATION_ERRORS]: payload => ({
validationErrors: payload,
}),
[RESET_EDITS]: () => ({ editedFields: {}, errorMessage: '' }),
[RESET_EDITS]: () => ({ editedFields: {}, error: null }),
};

export const reducer = createReducer(defaultState, stateChanges);
4 changes: 2 additions & 2 deletions packages/admin-panel/src/editor/withConnectedEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ const mergeProps = (
...editedFields,
}, // Include edits in visible record data
isNew,
onSave: (files, onSuccess) => {
onSave: (files, onSuccess, onError) => {
// If there is no record data, this is a new record
let fieldValuesToSave = isNew ? { ...initialValues, ...editedFields } : { ...editedFields };
if (onProcessDataForSave) {
fieldValuesToSave = onProcessDataForSave(fieldValuesToSave, recordData);
}
dispatch(saveEdits(endpoint, fieldValuesToSave, isNew, files, onSuccess));
dispatch(saveEdits(endpoint, fieldValuesToSave, isNew, files, onSuccess, onError));
},
usedByConfig,
};
Expand Down
16 changes: 8 additions & 8 deletions packages/admin-panel/src/importExport/ExportModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ export const ExportModal = React.memo(
}) => {
const api = useApiContext();
const [status, setStatus] = useState(STATUS.IDLE);
const [errorMessage, setErrorMessage] = useState(null);
const [error, setError] = useState(null);
const [isOpen, setIsOpen] = useState(false);

const handleOpen = () => setIsOpen(true);

const handleDismiss = () => {
setStatus(STATUS.IDLE);
setErrorMessage(null);
setError(null);
};

const handleClose = () => {
setStatus(STATUS.IDLE);
setErrorMessage(null);
setError(null);
setIsOpen(false);
};

const handleSubmit = async event => {
event.preventDefault();
setErrorMessage(null);
setError(null);
setStatus(STATUS.LOADING);

try {
Expand All @@ -63,9 +63,9 @@ export const ExportModal = React.memo(
} else {
handleClose();
}
} catch (error) {
} catch (e) {
setStatus(STATUS.ERROR);
setErrorMessage(error.message);
setError(e);
}
};

Expand Down Expand Up @@ -120,8 +120,8 @@ export const ExportModal = React.memo(
onClose={handleClose}
isOpen={isOpen}
disableBackdropClick
title={errorMessage ? 'Error' : title}
errorMessage={errorMessage}
title={error ? 'Error' : title}
error={error}
isLoading={status === STATUS.LOADING}
buttons={buttons}
>
Expand Down
Loading
Loading