Skip to content

Commit

Permalink
[Modals] Add modals layout
Browse files Browse the repository at this point in the history
  • Loading branch information
thenamankumar committed Jun 4, 2020
1 parent edee382 commit 1114278
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 70 deletions.
3 changes: 2 additions & 1 deletion app/containers/Event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const Event = ({ router }) => {
const Modals = useModals();

useEffect(() => {
Modals.ViewEvent.open({ id: id_or_slug, slug: id_or_slug });
!Modals.ViewEvent.isOpen() &&
Modals.ViewEvent.open({ id: id_or_slug, slug: id_or_slug });
}, [id_or_slug]);

return (
Expand Down
2 changes: 1 addition & 1 deletion app/layouts/Dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import classNames from 'classnames';
import { withRouter } from 'next/router';
import './style.scss';

const Dashboard = ({ children, data, router, query, isPublic }) => {
const Dashboard = ({ children, data, router, isPublic }) => {
const [showSideBar, setSideBar] = useState(true);

if (!data) return null;
Expand Down
4 changes: 2 additions & 2 deletions app/modals/AddEvent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const AddEvent = ({ dateTimeRange, types, onClose }) => {
};

return (
<Modal isOpen={true} size="md">
<>
<ModalHeader>Add Event</ModalHeader>
<ModalBody>
<Form {...formProps} />
Expand All @@ -145,7 +145,7 @@ const AddEvent = ({ dateTimeRange, types, onClose }) => {
Add
</Button>
</ModalFooter>
</Modal>
</>
);
};

Expand Down
4 changes: 2 additions & 2 deletions app/modals/AddZoomAccount/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const AddZoomAccount = ({ onClose, onSuccess }) => {
const errors = formatErrors(rawErrors);

return (
<Modal isOpen={true} size="sm">
<>
<ModalHeader>Add Zoom Account</ModalHeader>
<ModalBody>
<Form className="row" errors={errors}>
Expand All @@ -45,7 +45,7 @@ const AddZoomAccount = ({ onClose, onSuccess }) => {
Add
</Button>
</ModalFooter>
</Modal>
</>
);
};

Expand Down
4 changes: 2 additions & 2 deletions app/modals/EditEvent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ const EditEvent = ({ loading, onClose, types, event }) => {
};

return (
<Modal isOpen={true} size="md">
<>
<ModalHeader>Edit Event</ModalHeader>
<ModalBody>{!loading && <Form {...formProps} />}</ModalBody>
<ModalFooter>
Expand All @@ -226,7 +226,7 @@ const EditEvent = ({ loading, onClose, types, event }) => {
Save
</Button>
</ModalFooter>
</Modal>
</>
);
};

Expand Down
6 changes: 3 additions & 3 deletions app/modals/Impersonate/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useMemo } from 'react';
import Form from 'Components/Form';
import UserSelect from 'Components/UserSelect';
import { Modal, ModalHeader, ModalBody, ModalFooter, Button } from 'reactstrap';
import { ModalHeader, ModalBody, ModalFooter, Button } from 'reactstrap';
import { useRouter } from 'next/router';

const Impersonate = ({ onClose }) => {
Expand All @@ -15,7 +15,7 @@ const Impersonate = ({ onClose }) => {
};

return (
<Modal isOpen={true} size="md">
<>
<ModalHeader>Impersonate</ModalHeader>
<ModalBody>
<Form className="row">
Expand All @@ -38,7 +38,7 @@ const Impersonate = ({ onClose }) => {
Impersonate
</Button>
</ModalFooter>
</Modal>
</>
);
};

Expand Down
122 changes: 79 additions & 43 deletions app/modals/Manager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useReducer, createContext } from 'react';
import dynamic from 'next/dynamic';
import { Modal } from 'reactstrap';
import { withRouter } from 'next/router';
import classNames from 'classnames';

const Context = createContext();

Expand All @@ -19,51 +22,84 @@ const reducer = (state, { type, payload }) => {
};

const modals = {
AddEvent: dynamic(() => import('./AddEvent')),
ViewEvent: dynamic(() => import('./ViewEvent')),
EditEvent: dynamic(() => import('./EditEvent')),
AddZoomAccount: dynamic(() => import('./AddZoomAccount')),
Impersonate: dynamic(() => import('./Impersonate')),
AddEvent: {
size: 'md',
Component: dynamic(() => import('./AddEvent')),
},
ViewEvent: {
size: 'sm',
autoClose: true,
Component: dynamic(() => import('./ViewEvent')),
},
EditEvent: {
size: 'md',
Component: dynamic(() => import('./EditEvent')),
},
AddZoomAccount: {
size: 'sm',
Component: dynamic(() => import('./AddZoomAccount')),
},
Impersonate: {
size: 'md',
Component: dynamic(() => import('./Impersonate')),
},
};

const ModalsManager = React.memo(({ children }) => {
const [state, dispatch] = useReducer(reducer, []);

const actions = Object.keys(modals).reduce((acm, name) => {
acm[name] = {
open: (props) =>
dispatch({ type: 'openModal', payload: { name, props } }),
close: () => dispatch({ type: 'closeModal', payload: { name } }),
};

return acm;
}, {});

return (
<Context.Provider value={actions}>
{children}
{state.map(({ name, props }, index) => {
const Modal = modals[name];

Modal.displayName = name;

return (
<Modal
key={`${name}-${index}`}
{...props}
onClose={async (...args) => {
const { onClose } = props;

if (typeof onClose === 'function') await onClose(...args);

return actions[name].close();
}}
/>
);
})}
</Context.Provider>
);
});
const ModalsManager = withRouter(
React.memo(({ children, router }) => {
const [state, dispatch] = useReducer(reducer, []);

const actions = Object.keys(modals).reduce((acm, name) => {
acm[name] = {
open: (props) =>
dispatch({ type: 'openModal', payload: { name, props } }),
close: () => dispatch({ type: 'closeModal', payload: { name } }),
isOpen: () => !!state.find((modal) => modal.name === name),
};

return acm;
}, {});

const embed = router?.query?.hasOwnProperty('embed');

return (
<Context.Provider value={actions}>
{children}
{state.map(({ name, props }, index) => {
const config = modals[name];
const ModalComponent = config.Component;

ModalComponent.displayName = name;

const onClose = async (...args) => {
const { onClose } = props;

if (typeof onClose === 'function') await onClose(...args);

return actions[name].close();
};

return (
<Modal
key={`${name}-${index}`}
className={classNames({ embed })}
isOpen={true}
size={props.size || config.size}
backdrop={!embed}
toggle={() => !embed && config.autoClose && onClose()}>
<ModalComponent
{...props}
embed={embed}
autoClose={config.autoClose}
onClose={onClose}
/>
</Modal>
);
})}
</Context.Provider>
);
}),
);

ModalsManager.Context = Context;

Expand Down
23 changes: 8 additions & 15 deletions app/modals/ViewEvent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,15 @@ const ViewEvent = ({ id, slug, onClose, event, startPolling, stopPolling }) => {

if (!event) {
return (
<Modal isOpen={true} size="sm" toggle={() => onClose()}>
<ModalBody>
<p className="text-center">No Event Found.</p>
</ModalBody>
</Modal>
<ModalBody>
<p className="text-center">No Event Found.</p>
</ModalBody>
);
}

const queryData = { query: QUERY, variables: { id: id, slug: slug } };

return (
<Modal isOpen={true} size="sm" toggle={() => onClose()}>
<Content event={{ ...event }} onClose={onClose} />
</Modal>
);
return <Content event={{ ...event }} onClose={onClose} />;
};

export default createPage({
Expand All @@ -40,11 +34,10 @@ export default createPage({
variables: ({ id, slug }) => ({ id, slug }),
isPublic: true,
requireLogin: false,
Layout: null,
LoaderComponent: ({ onClose }) => (
<Modal isOpen={true} size="sm" toggle={() => onClose()}>
<ModalBody>
<Loader relative />
</ModalBody>
</Modal>
<ModalBody>
<Loader relative />
</ModalBody>
),
});
3 changes: 2 additions & 1 deletion app/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class App extends NextApp {
<Head>
<link rel="icon" href="/favicon.png" />
</Head>
<ModalsManager>
<ModalsManager
router={{ query: pageProps.query, params: pageProps.params }}>
{hasError ? <ErrorLayout /> : <Page {...pageProps}></Page>}
</ModalsManager>
</ApolloProvider>
Expand Down
10 changes: 10 additions & 0 deletions app/styles/_custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,13 @@
top: 5px;
}
}

.modal {
.modal-dialog {
&.embed {
.modal-content {
min-height: calc(100vh - 20px);
}
}
}
}
2 changes: 2 additions & 0 deletions app/utils/createPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const createPage = ({
content = <Component loading={loading} {...data} {...rest} {...props} />;
}

if (!Layout) return content;

return (
<Layout
loading={loading}
Expand Down

0 comments on commit 1114278

Please sign in to comment.