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

SIMSBIOHUB-461: "Missing Field" snackbar fix #1215

Merged
merged 9 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions app/src/components/alert/SnackbarComponent.tsx
KjartanE marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Alert from '@mui/material/Alert';
import Snackbar from '@mui/material/Snackbar';
import { useFormikContext } from 'formik';
import { useEffect, useState } from 'react';

const SnackbarComponent = () => {
const formikProps = useFormikContext<any>();
const { errors, submitCount } = formikProps;
const [openSnackbar, setOpenSnackbar] = useState({ open: false, msg: '' });

useEffect(() => {
const showSnackBar = (message: string) => {
KjartanE marked this conversation as resolved.
Show resolved Hide resolved
setOpenSnackbar({ open: true, msg: message });
};

if (errors && submitCount > 0) {
showSnackBar('Missing one or more required fields.');
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [errors, submitCount]);

const closeSnackBar = () =>
setOpenSnackbar((currentState) => {
return { open: false, msg: currentState.msg };
});

return (
<>
<Snackbar
anchorOrigin={{
vertical: 'bottom',
horizontal: 'center'
}}
open={openSnackbar.open}
onClose={closeSnackBar}>
<Alert elevation={4} severity="error" variant="filled" onClose={closeSnackBar}>
{openSnackbar.msg}
</Alert>
</Snackbar>
</>
);
};

export default SnackbarComponent;
100 changes: 0 additions & 100 deletions app/src/components/formik/ScrollToFormikError.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions app/src/components/species/FocalSpeciesComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SelectedSpecies from './components/SelectedSpecies';
import SpeciesAutocompleteField from './components/SpeciesAutocompleteField';

const FocalSpeciesComponent = () => {
const { values, setFieldValue, setErrors, errors } = useFormikContext<ITaxonomy[]>();
const { values, setFieldValue, setErrors, errors, submitCount } = useFormikContext<ITaxonomy[]>();

const selectedSpecies: ITaxonomy[] = get(values, 'species.focal_species') || [];

Expand All @@ -32,7 +32,7 @@ const FocalSpeciesComponent = () => {
required={true}
handleAddSpecies={handleAddSpecies}
/>
{errors && get(errors, 'species.focal_species') && (
{submitCount > 0 && errors && get(errors, 'species.focal_species') && (
<Box mt={3}>
<AlertBar
severity="error"
Expand Down
5 changes: 2 additions & 3 deletions app/src/features/projects/create/CreateProjectForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Theme } from '@mui/material';
import Box from '@mui/material/Box';
import Divider from '@mui/material/Divider';
import { makeStyles } from '@mui/styles';
import SnackbarComponent from 'components/alert/SnackbarComponent';
import HorizontalSplitFormComponent from 'components/fields/HorizontalSplitFormComponent';
import { ScrollToFormikError } from 'components/formik/ScrollToFormikError';
import { PROJECT_ROLE } from 'constants/roles';
import { Formik, FormikProps } from 'formik';
import { useAuthStateContext } from 'hooks/useAuthStateContext';
Expand Down Expand Up @@ -106,8 +106,7 @@ const CreateProjectForm: React.FC<ICreateProjectForm> = (props) => {
enableReinitialize={true}
onSubmit={handleSubmit}>
<>
<ScrollToFormikError fieldOrder={Object.keys(initialProjectFieldData)} />

<SnackbarComponent />
<HorizontalSplitFormComponent
title="General Information"
summary="Enter general information, objectives and timelines for the project."
Expand Down
2 changes: 2 additions & 0 deletions app/src/features/projects/edit/EditProjectForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Divider from '@mui/material/Divider';
import { makeStyles } from '@mui/styles';
import SnackbarComponent from 'components/alert/SnackbarComponent';
import HorizontalSplitFormComponent from 'components/fields/HorizontalSplitFormComponent';
import { Formik, FormikProps } from 'formik';
import { IGetAllCodeSetsResponse } from 'interfaces/useCodesApi.interface';
Expand Down Expand Up @@ -62,6 +63,7 @@ const EditProjectForm: React.FC<IEditProjectForm> = (props) => {
enableReinitialize={true}
onSubmit={handleSubmit}>
<>
<SnackbarComponent />
<HorizontalSplitFormComponent
title="General Information"
summary="Enter general information, objectives and timelines for the project."
Expand Down
4 changes: 2 additions & 2 deletions app/src/features/surveys/CreateSurveyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import Link from '@mui/material/Link';
import Paper from '@mui/material/Paper';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import SnackbarComponent from 'components/alert/SnackbarComponent';
import { IErrorDialogProps } from 'components/dialog/ErrorDialog';
import HorizontalSplitFormComponent from 'components/fields/HorizontalSplitFormComponent';
import { ScrollToFormikError } from 'components/formik/ScrollToFormikError';
import PageHeader from 'components/layout/PageHeader';
import { DATE_FORMAT, DATE_LIMIT } from 'constants/dateTimeFormats';
import { CreateSurveyI18N } from 'constants/i18n';
Expand Down Expand Up @@ -276,7 +276,7 @@ const CreateSurveyPage = () => {
validateOnChange={false}
onSubmit={handleSubmit}>
<>
<ScrollToFormikError fieldOrder={Object.keys(surveyInitialValues)} />
<SnackbarComponent />
<Stack gap={5} divider={<Divider flexItem />}>
<HorizontalSplitFormComponent
title="General Information"
Expand Down
5 changes: 2 additions & 3 deletions app/src/features/surveys/edit/EditSurveyForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Button from '@mui/material/Button';
import Divider from '@mui/material/Divider';
import Paper from '@mui/material/Paper';
import { makeStyles } from '@mui/styles';
import SnackbarComponent from 'components/alert/SnackbarComponent';
import HorizontalSplitFormComponent from 'components/fields/HorizontalSplitFormComponent';
import { ScrollToFormikError } from 'components/formik/ScrollToFormikError';
import { DATE_FORMAT, DATE_LIMIT } from 'constants/dateTimeFormats';
import { default as dayjs } from 'dayjs';
import SamplingStrategyForm from 'features/surveys/components/SamplingStrategyForm';
Expand Down Expand Up @@ -157,8 +157,7 @@ const EditSurveyForm: React.FC<IEditSurveyForm> = (props) => {
validateOnChange={false}
onSubmit={props.handleSubmit}>
<>
<ScrollToFormikError fieldOrder={Object.keys(surveyInitialValues)} />

<SnackbarComponent />
<HorizontalSplitFormComponent
title="General Information"
summary=""
Expand Down
Loading