Simplify StoreAlerts component logic

This commit is contained in:
Tiago Noronha 2019-03-08 16:17:48 +00:00
parent 2e2fb90cde
commit c3ff6fca49
1 changed files with 13 additions and 15 deletions

View File

@ -61,30 +61,30 @@ class StoreAlerts extends Component {
} }
render() { render() {
const hasAlerts = Boolean( wcSettings.alertCount ) || null; const alerts = this.props.alerts || [];
const preloadAlertCount = wcSettings.alertCount && parseInt( wcSettings.alertCount );
if ( ! hasAlerts ) { if ( preloadAlertCount > 0 && 0 === alerts.length ) {
return <StoreAlertsPlaceholder hasMultipleAlerts={ preloadAlertCount > 1 } />;
} else if ( 0 === alerts.length ) {
return null; return null;
} }
const { alerts } = this.props;
const { currentIndex } = this.state; const { currentIndex } = this.state;
const numberOfAlerts = alerts.length; const numberOfAlerts = alerts.length;
const alert = alerts[ currentIndex ] ? alerts[ currentIndex ] : null; const alert = alerts[ currentIndex ];
const type = alert && alert.type ? alert.type : null; const type = alert.type;
const className = classnames( 'woocommerce-store-alerts', { const className = classnames( 'woocommerce-store-alerts', {
'is-alert-error': 'error' === type, 'is-alert-error': 'error' === type,
'is-alert-update': 'update' === type, 'is-alert-update': 'update' === type,
} ); } );
const actions = const actions = alert.actions.map( action => (
alert && <Button key={ action.name } isDefault href={ action.url }>
alert.actions.map( action => ( { action.label }
<Button key={ action.name } isDefault href={ action.url }> </Button>
{ action.label } ) );
</Button>
) );
return alert ? ( return (
<Card <Card
title={ [ title={ [
alert.icon && <Dashicon key="icon" icon={ alert.icon } />, alert.icon && <Dashicon key="icon" icon={ alert.icon } />,
@ -129,8 +129,6 @@ class StoreAlerts extends Component {
/> />
{ actions && <div className="woocommerce-store-alerts__actions">{ actions }</div> } { actions && <div className="woocommerce-store-alerts__actions">{ actions }</div> }
</Card> </Card>
) : (
<StoreAlertsPlaceholder hasMultipleAlerts={ wcSettings.alertCount > 1 } />
); );
} }
} }