In my react application I have Tabs done with Material UI. Some of those tabs are shown or hide depending on an option is true or false. The code is not a clean solution but I have no experience working with this and was wondering to see a better solution to what I have done.
We have 3 options:
- isEngagementPortal
- isRecruitmentPortal
- isConsenteesPortal
when one of the above options is true/false the corresponding tab is shown or hide.
I was thinking to make it in smaller components or dynamic but have difficulties doing it.
<div className={classes.root}>
<AppBar className={classes.appBar} position="static" elevation={0}>
<Toolbar disableGutters>
<Tabs value={tab} onChange={(e, newValue) => setTab(newValue)}>
<Tab label="Study Info" />
<Tab label="Study Tracks" />
<Tab label="User Admin" />
<Tab label="Study Locales" />
{isEngagementPortal && <Tab label="Resources" />}
{isRecruitmentPortal && <Tab label="Pre-screener" />}
{isRecruitmentPortal && <Tab label="Consent" />}
{isRecruitmentPortal && <Tab label="Manuscript" />}
<Tab label="Survey" />
{isRecruitmentPortal && <Tab label="Translations" />}
<Tab label="Sites" />
{isConsenteesPortal && <Tab label="eConsent" />}
{isRecruitmentPortal && <Tab label="Reports" />}
</Tabs>
</Toolbar>
</AppBar>
{console.log(tab)}
<Grid>
{tab === 0 && (
<StudyInfo
studyId={studyId}
refetchQueries={refetchQueries}
setAlert={setAlert}
/>
)}
{tab === 1 && <StudyTracks studyId={studyId} />}
{tab === 2 && <div>USER ADMIN</div>}
{tab === 3 && (
<StudyLocales
setAlert={setAlert}
studyId={studyId}
locales={locales}
currentLocales={currentLocales}
refetchQueries={refetchQueries}
/>
)}
{isEngagementPortal && tab === 4 && (
<StudyResources studyId={studyId} locales={locales} />
)}
{tab === 5 && isRecruitmentPortal && (
<Questionnaire
type="PRESCREENER"
preview
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 6 && (
<Questionnaire
type="CONSENT"
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 7 && (
<Questionnaire
type="MANUSCRIPT"
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{tab === 8 && (
<Questionnaire
type="SURVEY"
preview
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 9 && (
<StudyTranslation
studyId={studyId}
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
/>
)}
{tab === 10 && (
<Sites
studyId={studyId}
refetchQueries={refetchQueries}
setAlert={setAlert}
locales={locales}
/>
)}
{isConsenteesPortal && tab === 11 && <div>E-CONSENT</div>}
{isRecruitmentPortal && tab === 12 && <Reports studyId={studyId} />}
</Grid>
<Alert content={alert} closeDialog={setAlert} />
</div>
);
As requested the full component
/* eslint-disable react/prop-types */
import { useState } from 'react';
import {
AppBar,
Grid,
Tab,
Tabs,
Toolbar,
makeStyles,
} from '@material-ui/core';
import { get, map } from 'lodash';
import { useQuery } from '@apollo/react-hooks';
import Alert from '../../Dialogs/Alert';
import Loading from '../../Loading';
import StudyInfo from './StudyTabs/StudyInfo';
import StudyTranslation from './StudyTabs/StudyTranslations';
import Questionnaire from './StudyTabs/Questionnaire';
import Sites from './StudyTabs/Sites';
import studyRefetchQueries from './StudyTabs/utils/refetchQueries';
import StudyLocales from './StudyTabs/StudyLocales';
import StudyTracks from './StudyTabs/StudyTracks';
import { getLocales, getFeatureOptions } from './StudyTabs/utils/queries';
import Reports from './StudyTabs/Reports';
import StudyResources from './StudyTabs/StudyResources';
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
backgroundColor: theme.palette.background.paper,
color: 'white',
},
appBar: {
backgroundColor: theme.palette.background.paper,
},
}));
function Study({ studyId }) {
console.log(studyId);
const classes = useStyles();
const { loading, data } = useQuery(getLocales, { variables: { studyId } });
const { data: featureOptionsData } = useQuery(getFeatureOptions, {
variables: { studyId },
});
const [alert, setAlert] = useState();
const [tab, setTab] = useState(0);
const [showTab, setShowTab] = useState(false);
if (loading) {
return <Loading />;
}
const { refetchQueries } = studyRefetchQueries({ studyId });
const locales = get(data, 'locales.nodes') || [];
const currentLocales = map(locales, l => l.locale);
const options = get(featureOptionsData, 'featureOptions') || [];
console.log(options);
const isRecruitmentPortal = options.recruitmentPortal;
const isEngagementPortal = options.engagementPortal;
const isConsenteesPortal = options.consenteesPortal;
console.log(isRecruitmentPortal, isEngagementPortal, isConsenteesPortal);
return (
<div className={classes.root}>
<AppBar className={classes.appBar} position="static" elevation={0}>
<Toolbar disableGutters>
<Tabs value={tab} onChange={(e, newValue) => setTab(newValue)}>
<Tab value={0} label="Study Info" />
<Tab value={1} label="Study Tracks" />
<Tab value={2} label="User Admin" />
<Tab value={3} label="Study Locales" />
{isEngagementPortal && <Tab value={4} label="Resources" />}
{isRecruitmentPortal && <Tab value={5} label="Pre-screener" />}
{isRecruitmentPortal && <Tab value={6} label="Consent" />}
{isRecruitmentPortal && <Tab value={7} label="Manuscript" />}
<Tab value={8} label="Survey" />
{isRecruitmentPortal && <Tab value={9} label="Translations" />}
<Tab value={10} label="Sites" />
{isConsenteesPortal && <Tab value={11} label="eConsent" />}
{isRecruitmentPortal && <Tab value={12} label="Reports" />}
</Tabs>
</Toolbar>
</AppBar>
{console.log(tab)}
<Grid>
{tab === 0 && (
<StudyInfo
studyId={studyId}
refetchQueries={refetchQueries}
setAlert={setAlert}
/>
)}
{tab === 1 && <StudyTracks studyId={studyId} />}
{tab === 2 && <div>USER ADMIN</div>}
{tab === 3 && (
<StudyLocales
setAlert={setAlert}
studyId={studyId}
locales={locales}
currentLocales={currentLocales}
refetchQueries={refetchQueries}
/>
)}
{isEngagementPortal && tab === 4 && (
<StudyResources studyId={studyId} locales={locales} />
)}
{tab === 5 && isRecruitmentPortal && (
<Questionnaire
type="PRESCREENER"
preview
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 6 && (
<Questionnaire
type="CONSENT"
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 7 && (
<Questionnaire
type="MANUSCRIPT"
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{tab === 8 && (
<Questionnaire
type="SURVEY"
preview
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 9 && (
<StudyTranslation
studyId={studyId}
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
/>
)}
{tab === 10 && (
<Sites
studyId={studyId}
refetchQueries={refetchQueries}
setAlert={setAlert}
locales={locales}
/>
)}
{isConsenteesPortal && tab === 11 && <div>E-CONSENT</div>}
{isRecruitmentPortal && tab === 12 && <Reports studyId={studyId} />}
</Grid>
<Alert content={alert} closeDialog={setAlert} />
</div>
);
}
export default Study;