Changeset 5024 for sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/client/components/plugin/favorite-button/index.jsx
- Timestamp:
- 03/01/2017 06:08:54 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/client/components/plugin/favorite-button/index.jsx
r4467 r5024 1 import React from 'react'; 1 /** 2 * External dependencies. 3 */ 4 import React, { Component, PropTypes } from 'react'; 5 import { connect } from 'react-redux'; 6 import { identity } from 'lodash'; 7 import { localize } from 'i18n-calypso'; 2 8 3 import FavoriteButton from './button'; 4 import { 5 getFavorites, 6 favoritePlugin, 7 unfavoritePlugin 8 } from 'actions/index'; 9 /** 10 * Internal dependencies. 11 */ 12 import { favoritePlugin } from 'state/favorites/actions'; 13 import { isFavorite } from 'state/selectors'; 9 14 10 export default React.createClass( { 11 componentDidMount() { 12 this.getFavorites(); 13 }, 15 export class FavoriteButton extends Component { 16 static propTypes = { 17 favorite: PropTypes.bool, 18 favoritePlugin: PropTypes.func, 19 plugin: PropTypes.object, 20 translate: PropTypes.func, 21 }; 14 22 15 componentDidUpdate( previousProps ) { 16 if ( this.props.plugin.slug !== previousProps.plugin.slug ) { 17 this.getFavorites(); 18 } 19 }, 23 static defaultProps = { 24 favorite: false, 25 favoritePlugin: () => {}, 26 plugin: {}, 27 translate: identity, 28 }; 20 29 21 getFavorites() { 22 this.props.dispatch( getFavorites( this.props.plugin.slug ) ); 23 }, 30 toggleFavorite = ( event ) => { 31 const $button = jQuery( event.target ); 24 32 25 toggleFavorite( event ) { 26 if ( event.target.classList.contains( 'favorited' ) ) { 27 this.props.dispatch( unfavoritePlugin( this.props.plugin.slug ) ); 28 } else { 29 this.props.dispatch( favoritePlugin( this.props.plugin.slug ) ); 30 } 31 }, 33 this.props.favoritePlugin( this.props.plugin ); 34 35 $button.addClass( 'is-animating' ).one( 'animationend', () => { 36 $button.toggleClass( 'is-animating favorited' ); 37 } ); 38 }; 32 39 33 40 render() { 34 return <FavoriteButton toggleFavorite={ this.toggleFavorite } />; 41 if ( 0 === pluginDirectory.userId ) { 42 return null; 43 } 44 45 const { favorite, plugin, translate } = this.props; 46 const classNames = [ 'plugin-favorite-heart' ]; 47 48 if ( favorite ) { 49 classNames.push( 'favorited' ); 50 } 51 52 return ( 53 <div className="plugin-favorite"> 54 <button type="button" className={ classNames.join( ' ' ) } onClick={ this.toggleFavorite } > 55 <span className="screen-reader-text"> 56 { favorite 57 ? translate( 'Unfavorite %(name)s', { components: { name: plugin.name } } ) 58 : translate( 'Favorite %(name)s', { components: { name: plugin.name } } ) 59 } 60 </span> 61 </button> 62 </div> 63 ); 35 64 } 36 } ); 65 } 66 67 export default connect( 68 ( state ) => ( { 69 favorite: isFavorite( state ), 70 } ), 71 { 72 favoritePlugin, 73 }, 74 )( localize( FavoriteButton ) );
Note: See TracChangeset
for help on using the changeset viewer.