import React, { PropTypes } from 'react' import { connect } from 'react-redux' import { bindActionCreators } from 'redux' import { Link, hashHistory } from 'react-router' import FontAwesome from 'react-fontawesome' import ArtistSentence from './ArtistSentence' import VolumeControl from './VolumeControl' import * as helpers from '../helpers' import * as coreActions from '../services/core/actions' import * as uiActions from '../services/ui/actions' import * as pusherActions from '../services/pusher/actions' class Snapcast extends React.Component{ constructor(props){ super(props); this.state = { editing_client: null } } componentDidMount(){ if (this.props.pusher_connected && this.props.snapcast_enabled){ this.props.pusherActions.getSnapcast(); } } componentWillReceiveProps(newProps){ // Just connected if (!this.props.pusher_connected && newProps.pusher_connected){ this.props.pusherActions.getSnapcast(); // Just enabled (well, identified as being enabled) } else if (!this.props.pusher_enabled && newProps.pusher_enabled && newProps.pusher_connected){ this.props.pusherActions.getSnapcast(); } } saveEditingClient(){ this.props.pusherActions.setSnapcastClientName(this.state.editing_client.id, this.state.editing_client.name); this.setState({editing_client: null}); } render(){ if (!this.props.snapcast_clients || !this.props.snapcast_groups){ return null; } // Construct a simple array of our groups index var groups = []; for (var group_id in this.props.snapcast_groups){ if (this.props.snapcast_groups.hasOwnProperty(group_id)){ var group = this.props.snapcast_groups[group_id]; // Merge the group's clients into this group (also as a simple array) var clients = []; for (var i = 0; i < group.clients_ids.length; i++){ if (this.props.snapcast_clients.hasOwnProperty(group.clients_ids[i])){ clients.push(this.props.snapcast_clients[group.clients_ids[i]]); } } groups.push(Object.assign( {}, group, { clients: clients } )); } } return (
{ groups.map(group => { return (
{group.name ? group.name : 'Untitled group'}
{ group.clients.map(client => { var name = client.config.name ? client.config.name : client.host.name; return (
{name} {!client.connected ? '(disconnected)' : null}
this.props.uiActions.openModal('edit_snapcast_client',{id: client.id})}>
this.props.pusherActions.setSnapcastClientVolume(client.id, client.config.volume.muted, percent)} onMuteChange={mute => this.props.pusherActions.setSnapcastClientVolume(client.id, mute, client.config.volume.percent)} />
); }) }
); }) }
); } } const mapStateToProps = (state, ownProps) => { return { snapcast_enabled: state.pusher.config.snapcast_enabled, pusher_connected: state.pusher.connected, snapcast_groups: state.pusher.snapcast_groups, snapcast_clients: state.pusher.snapcast_clients } } const mapDispatchToProps = (dispatch) => { return { coreActions: bindActionCreators(coreActions, dispatch), uiActions: bindActionCreators(uiActions, dispatch), pusherActions: bindActionCreators(pusherActions, dispatch) } } export default connect(mapStateToProps, mapDispatchToProps)(Snapcast)