Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint-disable react/prop-types */
  2. import { useState } from 'react';
  3. import {
  4.   AppBar,
  5.   Grid,
  6.   Tab,
  7.   Tabs,
  8.   Toolbar,
  9.   makeStyles,
  10. } from '@material-ui/core';
  11. import { get, map } from 'lodash';
  12. import { useQuery } from '@apollo/react-hooks';
  13. import Alert from '../../Dialogs/Alert';
  14. import Loading from '../../Loading';
  15. import StudyInfo from './StudyTabs/StudyInfo';
  16. import StudyTranslation from './StudyTabs/StudyTranslations';
  17. import Questionnaire from './StudyTabs/Questionnaire';
  18. import Sites from './StudyTabs/Sites';
  19. import studyRefetchQueries from './StudyTabs/utils/refetchQueries';
  20. import StudyLocales from './StudyTabs/StudyLocales';
  21. import StudyTracks from './StudyTabs/StudyTracks';
  22. import { getLocales, getFeatureOptions } from './StudyTabs/utils/queries';
  23. import Reports from './StudyTabs/Reports';
  24. import StudyResources from './StudyTabs/StudyResources';
  25.  
  26. const useStyles = makeStyles(theme => ({
  27.   root: {
  28.     flexGrow: 1,
  29.     backgroundColor: theme.palette.background.paper,
  30.     color: 'white',
  31.   },
  32.   appBar: {
  33.     backgroundColor: theme.palette.background.paper,
  34.   },
  35. }));
  36.  
  37. function Study({ studyId }) {
  38.   console.log(studyId);
  39.   const { loading, data } = useQuery(getLocales, { variables: { studyId } });
  40.   const { data: featureOptionsData } = useQuery(getFeatureOptions, {
  41.     variables: { studyId },
  42.   });
  43.   // const [alert, setAlert] = useState();
  44.   // const [tab, setTab] = useState(0);
  45.  
  46.   if (loading) {
  47.     return <Loading />;
  48.   }
  49.  
  50.   const { refetchQueries } = studyRefetchQueries({ studyId });
  51.   const locales = get(data, 'locales.nodes') || [];
  52.   const currentLocales = map(locales, l => l.locale);
  53.  
  54.   const options = get(featureOptionsData, 'featureOptions') || [];
  55.  
  56.   console.log(options);
  57.  
  58.   return (
  59.     <StudyTabs
  60.       portalTypes={options}
  61.       refetchQueries={refetchQueries}
  62.       currentLocales={currentLocales}
  63.       studyId={studyId}
  64.       locales={locales}
  65.     />
  66.   );
  67. }
  68.  
  69. const StudyTabs = ({
  70.   portalTypes,
  71.   refetchQueries,
  72.   currentLocales,
  73.   studyId,
  74.   locales,
  75. }) => {
  76.   const classes = useStyles();
  77.   const [alert, setAlert] = useState();
  78.   const [selectedTabId, setSelectedTabId] = useState(0);
  79.   const selectedTab = tabs[selectedTab];
  80.  
  81.   console.log('SELECTED TAB', selectedTab);
  82.  
  83.   console.log('PORTAL_TYPES', portalTypes);
  84.  
  85.   const props = { refetchQueries, currentLocales, setAlert, studyId, locales };
  86.   return (
  87.     <div className={classes.root}>
  88.       <AppBar className={classes.appBar} position="static" elevation={0}>
  89.         <Toolbar disableGutters>
  90.           <Tabs
  91.             value={selectedTabId}
  92.             onChange={(e, newValue) => setSelectedTabId(newValue)}
  93.           >
  94.             {console.log('BEFORE IS VISIBLE', portalTypes)}
  95.             {tabs.map(
  96.               ({ isVisible, label }, i) =>
  97.                 isVisible(portalTypes) && <Tab key={i} label={label} />,
  98.             )}
  99.           </Tabs>
  100.         </Toolbar>
  101.       </AppBar>
  102.       <Grid>
  103.         {selectedTab.isVisible(portalTypes) && (
  104.           <selectedTab.Content {...props} />
  105.         )}
  106.       </Grid>
  107.       <Alert content={alert} closeDialog={setAlert} />
  108.     </div>
  109.   );
  110. };
  111.  
  112. const tabs = [
  113.   {
  114.     label: 'Study Info',
  115.     isVisible: () => true,
  116.     Content: ({ studyId, refetchQueries, setAlert }) => (
  117.       <StudyInfo
  118.         studyId={studyId}
  119.         refetchQueries={refetchQueries}
  120.         setAlert={setAlert}
  121.       />
  122.     ),
  123.   },
  124.   {
  125.     label: 'Study Tracks',
  126.     isVisible: () => true,
  127.     Content: ({ studyId }) => <StudyTracks studyId={studyId} />,
  128.   },
  129.   {
  130.     label: 'User Admin',
  131.     isVisible: () => true,
  132.     Content: () => <div>USER ADMIN</div>,
  133.   },
  134.   {
  135.     label: 'Study Locales',
  136.     isVisible: () => true,
  137.     Content: ({
  138.       setAlert,
  139.       studyId,
  140.       locales,
  141.       currentLocales,
  142.       refetchQueries,
  143.     }) => (
  144.       <StudyLocales
  145.         setAlert={setAlert}
  146.         studyId={studyId}
  147.         locales={locales}
  148.         currentLocales={currentLocales}
  149.         refetchQueries={refetchQueries}
  150.       />
  151.     ),
  152.   },
  153.   {
  154.     label: 'Resources',
  155.     isVisible: portalType => {
  156.       console.log(portalType.engagementPortal);
  157.     },
  158.  
  159.     Content: ({ studyId, locales }) => (
  160.       <StudyResources studyId={studyId} locales={locales} />
  161.     ),
  162.   },
  163.   {
  164.     label: 'Pre-screener',
  165.     isVisible: portalType => portalType.recruitmentPortal,
  166.     Content: ({ currentLocales, refetchQueries, setAlert, studyId }) => (
  167.       <Questionnaire
  168.         type="PRESCREENER"
  169.         preview
  170.         locales={currentLocales}
  171.         refetchQueries={refetchQueries}
  172.         setAlert={setAlert}
  173.         studyId={studyId}
  174.       />
  175.     ),
  176.   },
  177.   {
  178.     label: 'Consent',
  179.     isVisible: portalType => portalType.recruitmentPortal,
  180.     Content: ({ currentLocales, refetchQueries, setAlert, studyId }) => (
  181.       <Questionnaire
  182.         type="CONSENT"
  183.         locales={currentLocales}
  184.         refetchQueries={refetchQueries}
  185.         setAlert={setAlert}
  186.         studyId={studyId}
  187.       />
  188.     ),
  189.   },
  190.   {
  191.     label: 'Manuscript',
  192.     isVisible: portalType => portalType.recruitmentPortal,
  193.     Content: ({ currentLocales, refetchQueries, setAlert, studyId }) => (
  194.       <Questionnaire
  195.         type="MANUSCRIPT"
  196.         locales={currentLocales}
  197.         refetchQueries={refetchQueries}
  198.         setAlert={setAlert}
  199.         studyId={studyId}
  200.       />
  201.     ),
  202.   },
  203.   {
  204.     label: 'Survey',
  205.     isVisible: () => true,
  206.     Content: ({ currentLocales, refetchQueries, setAlert, studyId }) => (
  207.       <Questionnaire
  208.         type="SURVEY"
  209.         preview
  210.         locales={currentLocales}
  211.         refetchQueries={refetchQueries}
  212.         setAlert={setAlert}
  213.         studyId={studyId}
  214.       />
  215.     ),
  216.   },
  217.   {
  218.     label: 'Translations',
  219.     isVisible: portalType => portalType.recruitmentPortal,
  220.     Content: ({ studyId, currentLocales, refetchQueries, setAlert }) => (
  221.       <StudyTranslation
  222.         studyId={studyId}
  223.         locales={currentLocales}
  224.         refetchQueries={refetchQueries}
  225.         setAlert={setAlert}
  226.       />
  227.     ),
  228.   },
  229.   {
  230.     label: 'Sites',
  231.     isVisible: () => true,
  232.     Content: ({ studyId, refetchQueries, setAlert, locales }) => (
  233.       <Sites
  234.         studyId={studyId}
  235.         refetchQueries={refetchQueries}
  236.         setAlert={setAlert}
  237.         locales={locales}
  238.       />
  239.     ),
  240.   },
  241.   {
  242.     label: 'eConsent',
  243.     isVisible: portalType => portalType.consenteesPortal,
  244.     Content: () => <div>E-CONSENT</div>,
  245.   },
  246.   {
  247.     label: 'Reports',
  248.     isVisible: portalType => portalType.recruitmentPortal,
  249.     Content: ({ studyId }) => <Reports studyId={studyId} />,
  250.   },
  251. ];
  252.  
  253. export default Study;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement