Making WordPress.org


Ignore:
Timestamp:
03/01/2017 06:08:54 PM (7 years ago)
Author:
obenland
Message:

Plugin Directory: Update React client with latest changes.

This is largely a cleanup commit with some WIP around switching to node-wpapi.

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 */
     4import React, { Component, PropTypes } from 'react';
     5import { connect } from 'react-redux';
     6import { identity } from 'lodash';
     7import { localize } from 'i18n-calypso';
    28
    3 import FavoriteButton from './button';
    4 import {
    5     getFavorites,
    6     favoritePlugin,
    7     unfavoritePlugin
    8 } from 'actions/index';
     9/**
     10 * Internal dependencies.
     11 */
     12import { favoritePlugin } from 'state/favorites/actions';
     13import { isFavorite } from 'state/selectors';
    914
    10 export default React.createClass( {
    11     componentDidMount() {
    12         this.getFavorites();
    13     },
     15export class FavoriteButton extends Component {
     16    static propTypes = {
     17        favorite: PropTypes.bool,
     18        favoritePlugin: PropTypes.func,
     19        plugin: PropTypes.object,
     20        translate: PropTypes.func,
     21    };
    1422
    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    };
    2029
    21     getFavorites() {
    22         this.props.dispatch( getFavorites( this.props.plugin.slug ) );
    23     },
     30    toggleFavorite = ( event ) => {
     31        const $button = jQuery( event.target );
    2432
    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    };
    3239
    3340    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        );
    3564    }
    36 } );
     65}
     66
     67export 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.