Skip to content

Commit

Permalink
Avoiding stateful VolumeControl component. Maintating muted in player…
Browse files Browse the repository at this point in the history
… global state.
  • Loading branch information
chandanrauniyar committed Feb 1, 2019
1 parent da1a5e8 commit d3b9ec8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 35 deletions.
4 changes: 3 additions & 1 deletion app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,12 @@ class App extends React.Component {
renderVolumeControl (settings) {
return (
<VolumeControls
fill={this.props.player.volume}
volume={this.props.player.volume}
updateVolume={this.props.actions.updateVolume}
toggleOption={this.props.actions.toggleOption}
settings={settings}
muted={this.props.player.muted}
toggleMute={this.props.actions.toggleMute}
/>
);
}
Expand Down
28 changes: 28 additions & 0 deletions app/actions/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export const PAUSE_PLAYBACK = 'PAUSE_PLAYBACK';
export const UPDATE_PLAYBACK_PROGRESS = 'UPDATE_PLAYBACK_PROGRESS';
export const UPDATE_SEEK = 'UPDATE_SEEK';
export const UPDATE_VOLUME = 'UPDATE_VOLUME';
export const TOGGLE_MUTE = 'TOGGLE_MUTE';
export const MUTE_ON = 'MUTE_ON';
export const MUTE_OFF = 'MUTE_OFF';
export const UPDATE_PLAYBACK_STREAM_LOADING = 'UPDATE_PLAYBACK_STREAM_LOADING';

export function startPlayback() {
Expand Down Expand Up @@ -58,6 +61,31 @@ export function updateVolume(volume) {
};
}

export function muteOn() {
return {
type: MUTE_ON,
payload: null
};
}

export function muteOff() {
return {
type: MUTE_OFF,
payload: null
};
}

export function toggleMute(muted) {
return dispatch => {
if (muted) {
dispatch(muteOn());
dispatch(updateVolume(0));
} else {
dispatch(muteOff());
}
};
}

export function updateStreamLoading(state) {
return {
type: UPDATE_PLAYBACK_STREAM_LOADING,
Expand Down
39 changes: 7 additions & 32 deletions app/components/VolumeControls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,15 @@ import VolumeSlider from './VolumeSlider';

class VolumeControls extends React.Component {

constructor(props) {
super(props);
this.state = {
isVolumeMuted: false,
volume: 100
};
}

handleClick(value) {
this.setState({
volume: value,
isVolumeMuted: this.state.isVolumeMuted && value === 0
});
if (value > 0){
this.props.toggleMute(false);
}
return this.props.updateVolume(value);
}

toggleMute = () => {
if (this.state.isVolumeMuted) {
this.handleMuteOff();
} else {
this.handleMuteOn();
}

this.setState({
isVolumeMuted: !this.state.isVolumeMuted
});
}

handleMuteOn() {
this.props.updateVolume(0);
}

handleMuteOff() {
this.handleClick(this.state.volume);
this.props.toggleMute(!this.props.muted);
}

render() {
Expand All @@ -52,12 +27,12 @@ class VolumeControls extends React.Component {
settings={this.props.settings}
/>
<div className={styles.volume_speaker_control}
onClick={this.toggleMute}>
<FontAwesome name={this.state.isVolumeMuted ? 'volume-off' : 'volume-up'} />
onClick={this.toggleMute.bind(this)}>
<FontAwesome name={this.props.muted ? 'volume-off' : 'volume-up'} />
</div>

<VolumeSlider
fill={this.state.volume}
fill={this.props.volume}
handleClick={this.handleClick.bind(this)}
/>
</div>
Expand Down
15 changes: 13 additions & 2 deletions app/reducers/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
UPDATE_PLAYBACK_PROGRESS,
UPDATE_SEEK,
UPDATE_VOLUME,
UPDATE_PLAYBACK_STREAM_LOADING
UPDATE_PLAYBACK_STREAM_LOADING,
MUTE_ON,
MUTE_OFF
} from '../actions/player';

import {
Expand All @@ -20,7 +22,8 @@ const initialState = {
playbackStreamLoading: false,
playbackProgress: 0,
seek: 0,
volume: 100
volume: 100,
muted: false
};

export default function PlayerReducer(state=initialState, action) {
Expand All @@ -46,6 +49,14 @@ export default function PlayerReducer(state=initialState, action) {
return Object.assign({}, state, {
volume: action.payload
});
case MUTE_ON:
return Object.assign({}, state, {
muted: true
});
case MUTE_OFF:
return Object.assign({}, state, {
muted: false
});
case NEXT_SONG:
case PREVIOUS_SONG:
case SELECT_SONG:
Expand Down

0 comments on commit d3b9ec8

Please sign in to comment.