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

Fix layout for pages which don't require auth #468

Merged
merged 1 commit into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 21 additions & 18 deletions components/application/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,40 @@ const Application = ({
children,
dataProvider,
pathname,
variant = 'public', // 'public' or 'internal'
isAuthenticated = false,
...restProps
}) => (
<>
<Head />
<Container {...restProps}>
<Container variant={variant} {...restProps}>
<LoadingBar fixed />
<AppBar variant={isAuthenticated ? 'internal' : 'public'}>
<AppBar>
{isAuthenticated ? (
<>
{dataProvider && <RepositorySelect value={dataProvider} />}
<Logout />
</>
) : null}
</AppBar>
<SideBar tag="nav">
<h2 className="sr-only">Navigate your data</h2>
{dataProvider && (
<ActivitySelect>
{activities.routes
.filter((activity) => activity.parent == null)
.map(({ path, test }) => (
<ActivitySelect.Option
key={path}
value={path}
selected={test(pathname)}
/>
))}
</ActivitySelect>
)}
</SideBar>
{variant === 'internal' && (
<SideBar tag="nav">
<h2 className="sr-only">Navigate your data</h2>
{dataProvider && (
<ActivitySelect>
{activities.routes
.filter((activity) => activity.parent == null)
.map(({ path, test }) => (
<ActivitySelect.Option
key={path}
value={path}
selected={test(pathname)}
/>
))}
</ActivitySelect>
)}
</SideBar>
)}
{children && <Main>{children}</Main>}
</Container>
</>
Expand Down
21 changes: 14 additions & 7 deletions components/forms/iframe-form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@ import { Card } from 'design'

const IframeForm = ({ className, title, ...passProps }) => {
const ref = useRef(null)
const observer = useRef(null)

const resize = useCallback(() => {
ref.current.style.height = `${ref.current.contentWindow.document.body.offsetHeight}px`
}, [])

useEffect(() => {
observer.current = new ResizeObserver(resize)
return () => observer.current.disconnect()
}, [])
const observer = useRef(new ResizeObserver(resize))

const handleOnLoad = useCallback(() => {
observer.current.observe(ref.current.contentWindow.document.body)
resize()
if (ref.current.contentWindow.document.body) {
observer.current.unobserve(ref.current.contentWindow.document.body)
observer.current.observe(ref.current.contentWindow.document.body)
resize()
}
}, [])

useEffect(() => {
// onLoad event may be fired before the JS is completely loaded
// ensure the element gets observed
// https://github.com/facebook/react/issues/15446
handleOnLoad()
return () => observer.current.disconnect()
}, [])

return (
Expand Down
11 changes: 9 additions & 2 deletions components/layout/container.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { classNames } from '@oacore/design/lib/utils'
import { Provider as LayoutContext } from './context'
import styles from './styles.module.css'

const Container = ({ children, className, tag: Tag = 'div', ...restProps }) => {
const Container = ({
children,
className,
variant,
tag: Tag = 'div',
...restProps
}) => {
const [state, update] = useState({
sidebarId: 'sidebar',
sidebarExpanded: false,
Expand All @@ -13,7 +19,8 @@ const Container = ({ children, className, tag: Tag = 'div', ...restProps }) => {
return (
<Tag
className={classNames
.use(styles.container, state.sidebarExpanded && styles.overlay)
.use('container', variant, state.sidebarExpanded)
.from(styles)
.join(className)}
{...restProps}
>
Expand Down
2 changes: 1 addition & 1 deletion components/layout/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Main = ({ children }) => {
const child = React.Children.only(children)
const { className, tag } = child.props
return React.cloneElement(child, {
className: classNames.use(styles.main).join(className),
className: classNames.use(styles.main).join(className).toString(),
tag: tag || 'main',
})
}
Expand Down
4 changes: 3 additions & 1 deletion components/layout/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
min-height: 100%;
padding-top: var(--app-bar-skip);
background: var(--gray-200);
}

@media (--screen-at-least-desktop) {
@media (--screen-at-least-desktop) {
.internal.container {
padding-left: var(--side-bar-width);
}
}
Expand Down
20 changes: 12 additions & 8 deletions pages/_app/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ process.on('uncaughtException', (err) => {
const ROUTES_WITHOUT_STORE = ['/login', '/reset', '/invitation']

const isRouteWithoutStore = (pathname) =>
ROUTES_WITHOUT_STORE.find((prefix) => pathname.startsWith(prefix))
ROUTES_WITHOUT_STORE.some((prefix) => pathname.startsWith(prefix))

export async function getStaticProps({ res }) {
res.setHeader(
Expand Down Expand Up @@ -170,23 +170,27 @@ class App extends NextApp {
const { store } = this
const { Component, pageProps, router } = this.props
const { isAuthorized } = this.state
const pathname = this.props.router.asPath
const pathname = router.asPath
const variant =
isAuthorized && !isRouteWithoutStore(pathname) ? 'internal' : 'public'

if (!isAuthorized) {
return (
<>
<Application dataProvider={undefined} pathname={pathname} />
{isRouteWithoutStore(router.asPath) ? (
<Component {...pageProps} />
) : null}
</>
<Application
dataProvider={undefined}
pathname={pathname}
variant={variant}
>
{isRouteWithoutStore(pathname) ? <Component {...pageProps} /> : null}
</Application>
)
}

return (
<Application
dataProvider={store.dataProvider}
pathname={pathname}
variant={variant}
isAuthenticated
>
<Component {...pageProps} />
Expand Down
2 changes: 1 addition & 1 deletion pages/invitation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { InvitationRegister } from 'components/forms'

const Invitation = ({ className, router, ...restProps }) => (
<Card
className={classNames.use(styles.invitationContainer, className)}
className={classNames.use(styles.invitationContainer, className).toString()}
{...restProps}
>
<Title hidden>Register</Title>
Expand Down