@kiwicom/smart-faq
Version:
Smart FAQ
52 lines (43 loc) • 1.07 kB
JavaScript
// @flow
import * as React from 'react';
import { withUser, withSimpleToken, withKwAuthToken } from '../context/User';
import type { User } from '../../types';
type AccountOnlyProps = {
user: ?User,
kwAuthToken: ?string,
children?: React.Node,
};
type AnyAuthorizationProps = {
...AccountOnlyProps,
simpleToken: ?string,
};
const maybeRender = predicateFn => props => {
if (!predicateFn(props)) {
return null;
}
return props.children;
};
const hasAnyAuthorization = (props: AnyAuthorizationProps) =>
props.simpleToken || props.user || props.kwAuthToken;
const LoggedIn = withSimpleToken(
withUser(withKwAuthToken(maybeRender(hasAnyAuthorization))),
);
const LoggedOut = withSimpleToken(
withUser(
withKwAuthToken(
maybeRender(
(props: AnyAuthorizationProps) => !hasAnyAuthorization(props),
),
),
),
);
const LoggedInAccount = withUser(
withKwAuthToken(
maybeRender((props: AccountOnlyProps) => props.user || props.kwAuthToken),
),
);
export default {
LoggedIn,
LoggedOut,
LoggedInAccount,
};