short-d/short

View on GitHub
frontend/src/component/hoc/withPageAuth.tsx

Summary

Maintainability
A
25 mins
Test Coverage
import React from 'react';
import { NotFoundPage } from '../pages/NotFoundPage';
 
Function `default` has 46 lines of code (exceeds 25 allowed). Consider refactoring.
Function `default` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
export default function(
WrappedComponent: React.ComponentType<any>,
makeAuthDecision: () => Promise<boolean>
) {
enum AuthDecision {
AUTHORIZED,
UNAUTHORIZED,
PENDING
}
 
interface IState {
decision: AuthDecision;
}
 
return class extends React.Component<any, IState> {
private isComponentMounted: boolean;
 
constructor(props: any) {
super(props);
this.state = {
decision: AuthDecision.PENDING
};
this.isComponentMounted = false;
}
 
componentDidMount(): void {
this.isComponentMounted = true;
 
makeAuthDecision().then(decision => {
if (!this.isComponentMounted) {
return;
}
this.setState({ decision: this.toAuthDecision(decision) });
});
}
 
componentWillUnmount(): void {
this.isComponentMounted = false;
}
 
render() {
const { decision } = this.state;
if (decision === AuthDecision.AUTHORIZED) {
return <WrappedComponent {...this.props} />;
}
 
if (decision === AuthDecision.UNAUTHORIZED) {
return <NotFoundPage />;
}
 
// TODO(issue#816): add loading page component
return <div />;
}
 
private toAuthDecision(featureDecision: boolean): AuthDecision {
if (!featureDecision) {
return AuthDecision.UNAUTHORIZED;
}
return AuthDecision.AUTHORIZED;
}
};
}