Files
redash/client/app/components/ApplicationArea/AuthenticatedPageWrapper.jsx
Levko Kravets cbc56264ea React migration cleanup (#4572)
* Revisit ANGULAR_REMOVE_ME things

* Remove styles related to 3rd-party Angular and jQuery libraries

* Remove some more unused styles

* Revisit error handling (app-wide)

* Remove unused file

* CR1
2020-01-22 17:15:25 +02:00

60 lines
1.5 KiB
JavaScript

import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import ErrorBoundary from "@/components/ErrorBoundary";
import { Auth } from "@/services/auth";
import organizationStatus from "@/services/organizationStatus";
import ApplicationHeader from "./ApplicationHeader";
import ErrorMessage from "./ErrorMessage";
export default function AuthenticatedPageWrapper({ bodyClass, children }) {
const [isAuthenticated, setIsAuthenticated] = useState(!!Auth.isAuthenticated());
useEffect(() => {
let isCancelled = false;
Promise.all([Auth.requireSession(), organizationStatus.refresh()])
.then(() => {
if (!isCancelled) {
setIsAuthenticated(!!Auth.isAuthenticated());
}
})
.catch(() => {
if (!isCancelled) {
setIsAuthenticated(false);
}
});
return () => {
isCancelled = true;
};
}, []);
useEffect(() => {
if (bodyClass) {
document.body.classList.toggle(bodyClass, true);
return () => {
document.body.classList.toggle(bodyClass, false);
};
}
}, [bodyClass]);
if (!isAuthenticated) {
return null;
}
return (
<>
<ApplicationHeader />
<ErrorBoundary renderError={error => <ErrorMessage error={error} />}>{children}</ErrorBoundary>
</>
);
}
AuthenticatedPageWrapper.propTypes = {
bodyClass: PropTypes.string,
children: PropTypes.node,
};
AuthenticatedPageWrapper.defaultProps = {
bodyClass: null,
children: null,
};