Skip to content

Commit

Permalink
Merge pull request #81 from datasektionen/fix-error-page
Browse files Browse the repository at this point in the history
Fix error page
  • Loading branch information
Herkarl authored Jul 7, 2024
2 parents 0f6fd73 + 5940ab8 commit ced003d
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 140 deletions.
73 changes: 46 additions & 27 deletions src/components/Default/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment } from 'react'
import React, { Fragment, useEffect, useState } from 'react'
import { Link, useLocation } from 'react-router-dom'
import { Title } from 'react-head'

Expand All @@ -7,6 +7,7 @@ import ErrorPage from '../ErrorPage'
import { Translate, English, Swedish } from '../Translate'
import { comparePages } from '../../utility/compare'
import { addLangToUrl } from '../../utility/lang'
import { navigateBack } from '../../utility/nav'

const getPageNav = (nav) => {
const child = nav.find(item => item.nav);
Expand Down Expand Up @@ -55,12 +56,13 @@ const getActiveMainTabTitle = (nav) => {
return null;
};


const PageHeader = ({ title, location }) => (
<header key="header">
<div className="header-inner">
<div className="row">
<div className="header-left col-md-2">
<Link to="/">
<Link onClick={navigateBack(false)}>
{'« '}
<Translate>
<English>Back</English>
Expand Down Expand Up @@ -131,39 +133,56 @@ const RightSidebar = ({ sidebar, anchors }) => (
);

const taitanRenderer = (location, lang) =>
({ title, body, sidebar, nav, anchors }, error) =>
(error ?
<ErrorPage error={error} /> :
<Fragment>
<Title>
{title + ' - Konglig Datasektionen'}
</Title>
<PageHeader title={title} location={location} />

<div id="content" key="content">
<div className="row">
<LeftSidebar nav={nav} lang={lang}/>

<div className="col-sm-8 col-md-9">
<div className="row">
<div
className="col-md-9"
dangerouslySetInnerHTML={{ __html: body }}
/>
<RightSidebar sidebar={sidebar} anchors={anchors} />
</div>
({ title, body, sidebar, nav, anchors, error }) => {

// This useEffect solution is a workaround to prevent hydration errors when loading the Error page.
// useEffect does not run when doing serverside rendering, so this prevents the error page from
// rendering serverside. Rendering it serverside caused the page to crash when rendering clientside,
// since the client always tries to render the normal page first, and getting angry that it does not
// match what the server produced. (This is due to error being undefined first due to latecncy).
const [errorPage, setErrorPage] = useState(null);
useEffect(() => {
if (error) {
setErrorPage(error)
} else {
setErrorPage(null)
}
}, [location, error]);

return (errorPage ?
<ErrorPage error={errorPage} /> :
<>
<Title>
{title + ' - Konglig Datasektionen'}
</Title>
<PageHeader title={title} location={location} />

<div id="content" key="content">
<div className="row">
<LeftSidebar nav={nav} lang={lang} />

<div className="col-sm-8 col-md-9">
<div className="row">
<div
className="col-md-9"
dangerouslySetInnerHTML={{ __html: body }}
/>
<RightSidebar sidebar={sidebar} anchors={anchors} />
</div>
</div>
</div>
</div>
</div>
</Fragment>
);
</>
);
}

export const Default = ({ lang }) => {
const location = useLocation();
const Page = taitanRenderer(location, lang);

return (
<Taitan pathname={location.pathname} lang={lang}>
{taitanRenderer(location, lang)}
{(props, err) => <Page {...props} error={err} />}
</Taitan>
)
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/ErrorPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Fragment } from 'react'
import { Title } from 'react-head'
import { Link } from 'react-router-dom'

import { navigateBack } from '../../utility/nav'
import { Translate, English, Swedish } from '../Translate'

export const ErrorPage = ({ error }) => (
Expand All @@ -13,7 +14,7 @@ export const ErrorPage = ({ error }) => (
<div className="header-inner">
<div className="row">
<div className="header-left col-md-2">
<Link to="/">
<Link onClick={navigateBack(true)}>
{'« '}
<Translate>
<English>Back</English>
Expand Down
1 change: 1 addition & 0 deletions src/components/EventCalendar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ export default function EventCalendar({ events, location, lang, onUpdateTimeSpan

setSelectedEventIndex(-1);
if (events.length === 0) return;
if (!Array.isArray(events)) return;
// Do not re-calculate the week if the user manually changes the viewed week.
if (hasCalculatedWeek) return;

Expand Down
Loading

0 comments on commit ced003d

Please sign in to comment.