1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 09:23:13 +01:00

Inject isLoggedIn through withCurrentUser

Makes checking for logged in status less error prone than always using
`currentUser.email` or `currentUser.username`.
This commit is contained in:
Brett Sun 2016-06-07 12:05:07 +02:00
parent b99782455e
commit b5eda1cdd2
10 changed files with 76 additions and 52 deletions

View File

@ -23,8 +23,6 @@ import Property from '../ascribe_forms/property';
import AclProxy from '../acl_proxy';
import { currentUserShape } from '../prop_types';
import ApiUrls from '../../constants/api_urls';
import AscribeSpinner from '../ascribe_spinner';
@ -37,14 +35,16 @@ import { withCurrentUser } from '../../utils/react_utils';
*/
const Edition = React.createClass({
propTypes: {
currentUser: currentUserShape.isRequired,
edition: React.PropTypes.object.isRequired,
whitelabel: React.PropTypes.object.isRequired,
actionPanelButtonListType: React.PropTypes.func,
coaError: React.PropTypes.object,
furtherDetailsType: React.PropTypes.func,
loadEdition: React.PropTypes.func
loadEdition: React.PropTypes.func,
// Injected through HOCs
isLoggedIn: React.PropTypes.bool.isRequired // eslint-disable-line react/sort-prop-types
},
getDefaultProps() {
@ -54,13 +54,15 @@ const Edition = React.createClass({
},
render() {
const { actionPanelButtonListType,
coaError,
currentUser,
edition,
furtherDetailsType: FurtherDetailsType,
loadEdition,
whitelabel } = this.props;
const {
actionPanelButtonListType,
coaError,
edition,
isLoggedIn,
loadEdition,
whitelabel,
furtherDetailsType: FurtherDetailsType
} = this.props;
return (
<Row>
@ -113,7 +115,7 @@ const Edition = React.createClass({
<CollapsibleParagraph
title={getLangText('Notes')}
show={!!(currentUser.username || edition.acl.acl_edit || edition.public_note)}>
show={!!(isLoggedIn || edition.acl.acl_edit || edition.public_note)}>
<Note
id={() => {return {'bitcoin_id': edition.bitcoin_id}; }}
label={getLangText('Personal note (private)')}
@ -154,12 +156,14 @@ const Edition = React.createClass({
let EditionSummary = withCurrentUser(React.createClass({
propTypes: {
currentUser: currentUserShape.isRequired,
edition: React.PropTypes.object.isRequired,
whitelabel: React.PropTypes.object.isRequired,
actionPanelButtonListType: React.PropTypes.func,
handleSuccess: React.PropTypes.func
handleSuccess: React.PropTypes.func,
// Injected through HOCs
isLoggedIn: React.PropTypes.bool.isRequired, // eslint-disable-line react/sort-prop-types
},
getStatus() {
@ -173,7 +177,13 @@ let EditionSummary = withCurrentUser(React.createClass({
},
render() {
const { actionPanelButtonListType, currentUser, edition, handleSuccess, whitelabel } = this.props;
const {
actionPanelButtonListType,
edition,
handleSuccess,
isLoggedIn,
whitelabel
} = this.props;
return (
<div className="ascribe-detail-header">
@ -197,7 +207,7 @@ let EditionSummary = withCurrentUser(React.createClass({
no more than 1 key, we're hiding the `DetailProperty` actions as otherwise
`AclInformation` would show up
*/}
<AclProxy show={currentUser.email && Object.keys(edition.acl).length > 1}>
<AclProxy show={isLoggedIn && Object.keys(edition.acl).length > 1}>
<DetailProperty
label={getLangText('ACTIONS')}
className="hidden-print">

View File

@ -9,14 +9,11 @@ import Form from '../ascribe_forms/form';
import Property from '../ascribe_forms/property';
import InputTextAreaToggable from '../ascribe_forms/input_textarea_toggable';
import { currentUserShape } from '../prop_types';
import { getLangText } from '../../utils/lang_utils';
import { withCurrentUser } from '../../utils/react_utils';
let Note = React.createClass({
propTypes: {
currentUser: currentUserShape.isRequired,
id: React.PropTypes.func.isRequired,
url: React.PropTypes.string.isRequired,
@ -25,7 +22,10 @@ let Note = React.createClass({
label: React.PropTypes.string,
placeholder: React.PropTypes.string,
show: React.PropTypes.bool,
successMessage: React.PropTypes.string
successMessage: React.PropTypes.string,
// Injected through HOCs
isLoggedIn: React.PropTypes.bool.isRequired // eslint-disable-line react/sort-prop-types
},
getDefaultProps() {
@ -43,9 +43,18 @@ let Note = React.createClass({
},
render() {
const { currentUser, defaultValue, editable, id, label, placeholder, show, url } = this.props;
const {
defaultValue,
editable,
id,
isLoggedIn,
label,
placeholder,
show,
url
} = this.props;
if ((currentUser.username && editable || !editable) && show) {
if ((isLoggedIn && editable || !editable) && show) {
return (
<Form
url={url}

View File

@ -38,8 +38,6 @@ import ListRequestActions from '../ascribe_forms/list_form_request_actions';
import AclProxy from '../acl_proxy';
import AscribeSpinner from '../ascribe_spinner';
import { currentUserShape } from '../prop_types';
import ApiUrls from '../../constants/api_urls';
import { setDocumentTitle } from '../../utils/dom_utils';
@ -52,11 +50,13 @@ import { withCurrentUser } from '../../utils/react_utils';
*/
const PieceContainer = React.createClass({
propTypes: {
currentUser: currentUserShape.isRequired,
router: React.PropTypes.object.isRequired,
furtherDetailsType: React.PropTypes.func,
// Injected through HOCs
isLoggedIn: React.PropTypes.bool.isRequired, // eslint-disable-line react/sort-prop-types
// Provided from AscribeApp
whitelabel: React.PropTypes.object,
@ -212,7 +212,7 @@ const PieceContainer = React.createClass({
getActions() {
const { piece } = this.state;
const { currentUser } = this.props;
const { isLoggedIn } = this.props;
if (piece.notifications && piece.notifications.length > 0) {
return (
@ -224,7 +224,7 @@ const PieceContainer = React.createClass({
} else {
return (
<AclProxy
show={currentUser.email && Object.keys(piece.acl).length > 1}>
show={isLoggedIn && Object.keys(piece.acl).length > 1}>
{/*
`acl_view` is always available in `edition.acl`, therefore if it has
no more than 1 key, we're hiding the `DetailProperty` actions as otherwise
@ -260,7 +260,7 @@ const PieceContainer = React.createClass({
},
render() {
const { furtherDetailsType: FurtherDetailsType } = this.props;
const { isLoggedIn, furtherDetailsType: FurtherDetailsType } = this.props;
const { piece } = this.state;
if (piece.id) {
@ -299,7 +299,7 @@ const PieceContainer = React.createClass({
</CollapsibleParagraph>
<CollapsibleParagraph
title={getLangText('Notes')}
show={!!(currentUser.username || piece.acl.acl_edit || piece.public_note)}>
show={!!(isLoggedIn || piece.acl.acl_edit || piece.public_note)}>
<Note
id={this.getId}
label={getLangText('Personal note (private)')}

View File

@ -23,6 +23,7 @@ const CopyrightAssociationForm = React.createClass({
propTypes: {
// Injected through HOCs
currentUser: currentUserShape.isRequired,
isLoggedIn: bool.isRequired
},
handleSubmitSuccess() {
@ -35,7 +36,7 @@ const CopyrightAssociationForm = React.createClass({
},
render() {
const { currentUser } = this.props;
const { currentUser, isLoggedIn } = this.props;
const selectDefaultValue = ' -- ' + getLangText('select an association') + ' -- ';
let selectedState = selectDefaultValue;
@ -45,7 +46,7 @@ const CopyrightAssociationForm = React.createClass({
}
}
if (currentUser.email) {
if (isLoggedIn) {
return (
<Form
ref='form'

View File

@ -10,7 +10,6 @@ import BitcoinWalletSettings from './bitcoin_wallet_settings';
import WebhookSettings from './webhook_settings';
import AclProxy from '../acl_proxy';
import { currentUserShape } from '../prop_types';
import { setDocumentTitle } from '../../utils/dom_utils';
import { getLangText } from '../../utils/lang_utils';
@ -19,13 +18,14 @@ import { withCurrentUser } from '../../utils/react_utils';
let SettingsContainer = React.createClass({
propTypes: {
currentUser: currentUserShape.isRequired,
children: React.PropTypes.oneOfType([
React.PropTypes.arrayOf(React.PropTypes.element),
React.PropTypes.element
]),
// Injected through HOCs
isLoggedIn: React.PropTypes.bool.isRequired, // eslint-disable-line react/sort-prop-types
// Provided from AscribeApp
whitelabel: React.PropTypes.object.isRequired,
@ -38,11 +38,11 @@ let SettingsContainer = React.createClass({
},
render() {
const { children, currentUser, whitelabel } = this.props;
const { children, isLoggedIn, whitelabel } = this.props;
setDocumentTitle(getLangText('Account settings'));
if (currentUser.username) {
if (isLoggedIn) {
return (
<div className="settings-container">
<AccountSettings

View File

@ -31,6 +31,7 @@ let Header = React.createClass({
// Injected through HOCs
currentUser: currentUserShape.isRequired, // eslint-disable-line react/sort-prop-types
isLoggedIn: React.PropTypes.bool.isRequired, // eslint-disable-line react/sort-prop-types
},
getInitialState() {
@ -110,14 +111,14 @@ let Header = React.createClass({
},
render() {
const { currentUser, routes, whitelabel } = this.props;
const { currentUser, isLoggedIn, routes, whitelabel } = this.props;
const { unfilteredPieceListCount } = this.state;
let account;
let signup;
let navRoutesLinks;
if (currentUser.username) {
if (isLoggedIn) {
account = (
<DropdownButton
ref='dropdownbutton'

View File

@ -20,6 +20,7 @@ let HeaderNotifications = React.createClass({
propTypes: {
// Injected through HOCs
currentUser: currentUserShape.isRequired, // eslint-disable-line react/sort-prop-types
isLoggedIn: React.PropTypes.bool.isRequired // eslint-disable-line react/sort-prop-types
},
getInitialState() {
@ -29,7 +30,7 @@ let HeaderNotifications = React.createClass({
componentDidMount() {
NotificationStore.listen(this.onChange);
if (this.props.currentUser.email) {
if (this.props.isLoggedIn) {
this.refreshNotifications();
}
},

View File

@ -13,7 +13,6 @@ import Note from '../../../../ascribe_detail/note';
import Piece from '../../../../../components/ascribe_detail/piece';
import AscribeSpinner from '../../../../ascribe_spinner';
import { currentUserShape } from '../../../../prop_types';
import ApiUrls from '../../../../../constants/api_urls';
@ -24,7 +23,6 @@ import { withCurrentUser } from '../../../../../utils/react_utils';
let WalletPieceContainer = React.createClass({
propTypes: {
piece: React.PropTypes.object.isRequired,
currentUser: currentUserShape.isRequired,
handleDeleteSuccess: React.PropTypes.func.isRequired,
loadPiece: React.PropTypes.func.isRequired,
submitButtonType: React.PropTypes.func.isRequired,
@ -32,13 +30,16 @@ let WalletPieceContainer = React.createClass({
children: React.PropTypes.oneOfType([
React.PropTypes.object,
React.PropTypes.array
])
]),
// Injected through HOCs
isLoggedIn: React.PropTypes.bool.isRequired // eslint-disable-line react/sort-prop-types
},
render() {
const { children,
currentUser,
handleDeleteSuccess,
isLoggedIn,
loadPiece,
piece,
submitButtonType } = this.props;
@ -76,7 +77,7 @@ let WalletPieceContainer = React.createClass({
</CollapsibleParagraph>
<CollapsibleParagraph
title={getLangText('Notes')}
show={!!(currentUser.username || piece.public_note)}>
show={!!(isLoggedIn || piece.public_note)}>
<Note
id={() => {return {'id': piece.id}; }}
label={getLangText('Personal note (private)')}

View File

@ -6,8 +6,6 @@ import Button from 'react-bootstrap/lib/Button';
import LinkContainer from 'react-router-bootstrap/lib/LinkContainer';
import { currentUserShape } from '../../../../prop_types';
import { setDocumentTitle } from '../../../../../utils/dom_utils';
import { getLangText } from '../../../../../utils/lang_utils';
import { withCurrentUser } from '../../../../../utils/react_utils';
@ -15,8 +13,10 @@ import { withCurrentUser } from '../../../../../utils/react_utils';
let IkonotvLanding = React.createClass({
propTypes: {
// Injected through HOCs
isLoggedIn: React.PropTypes.bool.isRequired, // eslint-disable-line react/sort-prop-types
// Provided from WalletApp
currentUser: currentUserShape.isRequired,
whitelabel: React.PropTypes.object,
// Provided from router
@ -24,10 +24,10 @@ let IkonotvLanding = React.createClass({
},
getEnterButton() {
const { currentUser, location } = this.props;
const { isLoggedIn, location } = this.props;
let redirect = '/login';
if (currentUser.email) {
if (isLoggedIn) {
redirect = '/collection';
} else if (location.query.redirect) {
redirect = '/' + location.query.redirect;

View File

@ -12,9 +12,10 @@ export function getDisplayName(WrappedComponent) {
/**
* Similar to react-router's `withRouter`, this injects the `currentUser` from the Component's
* context into the Component as a prop.
* context into the Component as a prop. It also injects whether the user's logged in or not as
* `isLoggedIn`.
*
* @param {Component} Component Component to inject `context.currentUser` into
* @param {Component} Component Component to inject `context.currentUser` and `isLoggedIn` into
* @return {Component} Wrapped component
*/
export function withCurrentUser(Component) {
@ -23,7 +24,7 @@ export function withCurrentUser(Component) {
};
const WithCurrentUser = (props, { currentUser }) => (
<Component {...props} currentUser={currentUser} />
<Component {...props} currentUser={currentUser} isLoggedIn={!!currentUser.email} />
);
WithCurrentUser.displayName = `WithCurrentUser(${getDisplayName(Component)})`;