Skip to content

Commit 2c702cd

Browse files
author
Dan Ziv
authored
feat(FEC-8084): add loading spinner while preforming change media (#209)
Build a reset preset which will allow us to change and extend the player UI when stopped - for now only the loading component appear there. Fix logic in loading & pre playback components. create util methods in component-config.js to allow components reducers to handle config actions. Add listeners in loading reducer to config change actions.
1 parent 91df91d commit 2c702cd

File tree

9 files changed

+74
-29
lines changed

9 files changed

+74
-29
lines changed

src/components/engine-connector/engine-connector.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class EngineConnector extends BaseComponent {
3434
const TrackType = this.player.Track;
3535

3636
this.player.addEventListener(this.player.Event.PLAYER_RESET, () => {
37-
this.props.updateIsStopped(true);
37+
this.props.updateIsIdle(true);
3838
});
3939

4040
this.player.addEventListener(this.player.Event.SOURCE_SELECTED, () => {
@@ -49,7 +49,7 @@ class EngineConnector extends BaseComponent {
4949

5050
this.player.addEventListener(this.player.Event.CHANGE_SOURCE_ENDED, () => {
5151
this.props.updatePlayerPoster(this.player.poster);
52-
this.props.updateIsStopped(false);
52+
this.props.updateIsIdle(false);
5353
});
5454

5555
this.player.addEventListener(this.player.Event.PLAYER_STATE_CHANGED, (e) => {

src/components/loading/loading.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,14 @@ class Loading extends BaseComponent {
3838

3939
/**
4040
* after component mounted, set event listener to player state change and update the state of loading spinner accordingly.
41-
* initially, if not mobile and autoplay is on, show the loading spinner without dependency on the player state.
42-
* if is mobile and mobile autoplay is on, show the loading spinner without dependency on the player state.
4341
*
4442
* @returns {void}
4543
* @memberof Loading
4644
*/
4745
componentDidMount() {
4846
this.player.addEventListener(this.player.Event.PLAYER_STATE_CHANGED, e => {
47+
if (!this.state.afterPlayingEvent) return;
4948
const StateType = this.player.State;
50-
if (!this.state.afterPlayingEvent) {
51-
return;
52-
}
5349
if (e.payload.newState.type === StateType.IDLE
5450
|| e.payload.newState.type === StateType.PLAYING
5551
|| e.payload.newState.type === StateType.PAUSED) {
@@ -60,8 +56,10 @@ class Loading extends BaseComponent {
6056
});
6157

6258
this.player.addEventListener(this.player.Event.SOURCE_SELECTED, () => {
63-
if (this.player.config.autoplay) {
59+
if (this.player.config.playback.autoplay) {
6460
this.props.updateLoadingSpinnerState(true);
61+
} else {
62+
this.props.updateLoadingSpinnerState(false);
6563
}
6664
});
6765

src/components/pre-playback-play-overlay/pre-playback-play-overlay.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const mapStateToProps = state => ({
1919
prePlayback: state.shell.prePlayback,
2020
poster: state.engine.poster,
2121
isMobile: state.shell.isMobile,
22-
isEnded: state.engine.isEnded
22+
isEnded: state.engine.isEnded,
23+
loading: state.loading.show
2324
});
2425

2526
@connect(mapStateToProps, bindActions(Object.assign(actions, loadingActions)))
@@ -125,7 +126,7 @@ class PrePlaybackPlayOverlay extends BaseComponent {
125126
* @memberof PrePlaybackPlayOverlay
126127
*/
127128
render(props: any): React$Element<any> | void {
128-
if ((!props.isEnded && !props.prePlayback) || (!props.isEnded && this.autoplay)) {
129+
if ((!props.isEnded && !props.prePlayback) || (!props.isEnded && this.autoplay) || props.loading) {
129130
return undefined;
130131
}
131132
let rootStyle = {},

src/player-gui.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ const mapStateToProps = state => ({
1313
engine: {
1414
adBreak: state.engine.adBreak,
1515
isLive: state.engine.isLive,
16-
hasError: state.engine.hasError,
17-
isStopped: state.engine.isStopped
16+
hasError: state.engine.hasError
1817
}
1918
},
2019
config: state.config
@@ -57,7 +56,7 @@ class PlayerGUI extends Component {
5756
*/
5857
render(props: any): React$Element<any> | void {
5958
let uiToRender;
60-
if (this.props.uis.length > 0 && !props.state.engine.isStopped) {
59+
if (this.props.uis.length > 0) {
6160
uiToRender = this.getMatchedUI(props.uis, props.state);
6261
return uiToRender ? uiToRender.template(props) : this.props.uis[0].template(props);
6362
} else {

src/reducers/engine.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export const types = {
2222
UPDATE_IS_LIVE: 'engine/UPDATE_IS_LIVE',
2323
UPDATE_IS_DVR: 'engine/UPDATE_IS_DVR',
2424
UPDATE_ERROR: 'engine/ERROR',
25-
UPDATE_IS_STOPPED: 'engine/UPDATE_IS_STOPPED'
25+
UPDATE_IS_IDLE: 'engine/UPDATE_IS_IDLE'
2626
};
2727

2828
export const initialState = {
29-
isStopped: false,
29+
isIdle: false,
3030
isPlaying: false,
3131
isEnded: false,
3232
metadataLoaded: false,
@@ -193,10 +193,10 @@ export default (state: Object = initialState, action: Object) => {
193193
isDvr: action.isDvr
194194
};
195195

196-
case types.UPDATE_IS_STOPPED:
196+
case types.UPDATE_IS_IDLE:
197197
return {
198198
...state,
199-
isStopped: action.stopped
199+
isIdle: action.IsIdle
200200
};
201201

202202

@@ -237,5 +237,5 @@ export const actions = {
237237
updatePlayerPoster: (poster: string) => ({type: types.UPDATE_PLAYER_POSTER, poster}),
238238
updateIsLive: (isLive: boolean) => ({type: types.UPDATE_IS_LIVE, isLive}),
239239
updateIsDvr: (isDvr: boolean) => ({type: types.UPDATE_IS_DVR, isDvr}),
240-
updateIsStopped: (stopped: boolean) => ({type: types.UPDATE_IS_STOPPED, stopped})
240+
updateIsIdle: (IsIdle: boolean) => ({type: types.UPDATE_IS_IDLE, IsIdle: IsIdle})
241241
};

src/reducers/loading.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
//@flow
2+
import {types as configReducerTypes} from './config'
3+
import {getComponentStateFromConfig, getComponentStateFromComponentConfig} from '../utils/component-config'
4+
5+
const component = 'loading';
6+
27
export const types = {
3-
UPDATE_LOADING_SPINNER_STATE: 'loading/UPDATE_LOADING_SPINNER_STATE'
8+
UPDATE_LOADING_SPINNER_STATE: `${component}/UPDATE_LOADING_SPINNER_STATE`
49
};
510

611
export const initialState = {
@@ -9,6 +14,12 @@ export const initialState = {
914

1015
export default (state: Object = initialState, action: Object) => {
1116
switch (action.type) {
17+
case configReducerTypes.UPDATE:
18+
return getComponentStateFromConfig(component, state, action);
19+
20+
case configReducerTypes.UPDATE_COMPONENT:
21+
return getComponentStateFromComponentConfig(component, state, action);
22+
1223
case types.UPDATE_LOADING_SPINNER_STATE:
1324
return {
1425
...state,

src/ui-manager.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import adsUI from './ui-presets/ads';
2525
import playbackUI from './ui-presets/playback';
2626
import liveUI from './ui-presets/live';
2727
import errorUI from './ui-presets/error';
28+
import idleUI from './ui-presets/idle'
2829

2930
import './styles/style.scss';
3031

@@ -95,6 +96,7 @@ export default class UIManager {
9596
*/
9697
buildDefaultUI(): void {
9798
const uis = [
99+
{template: props => idleUI(props), condition: state => state.engine.isIdle},
98100
{template: props => errorUI(props), condition: state => state.engine.hasError},
99101
{template: props => adsUI(props), condition: state => state.engine.adBreak},
100102
{template: props => liveUI(props), condition: state => state.engine.isLive},

src/ui-presets/idle.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@flow
2+
import {h} from 'preact';
3+
import style from '../styles/style.scss';
4+
import Loading from '../components/loading';
5+
6+
/**
7+
* Idle ui interface
8+
*
9+
* @export
10+
* @param {*} props component props
11+
* @returns {React$Element} player ui tree
12+
*/
13+
export default function idleUI(props: any): React$Element<any> {
14+
return (
15+
<div className={style.playbackGuiWWrapper}>
16+
<Loading player={props.player}/>
17+
</div>
18+
)
19+
}

src/utils/component-config.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
//@flow
2+
import {Utils} from 'playkit-js'
3+
4+
/**
5+
* @param {string} component - The component name.
6+
* @param {string} oldState - The component old state.
7+
* @param {string} action - The action object.
8+
* @returns {Object} - The component updated state.
9+
*/
10+
function getComponentStateFromConfig(component: string, oldState: Object, action: Object): Object {
11+
const componentConfig = action.config.components && action.config.components[component];
12+
if (componentConfig) {
13+
return Utils.Object.mergeDeep(oldState, componentConfig);
14+
}
15+
return oldState;
16+
}
17+
218
/**
3-
* Gets config param value
4-
* @param {*} config property name
5-
* @param {string} alias component name alias
6-
* @returns {Object} component config object
19+
* @param {string} component - The component name.
20+
* @param {string} oldState - The component old state.
21+
* @param {string} action - The action object.
22+
* @returns {Object} - The component updated state.
723
*/
8-
function getComponentConfig(config: any, alias: string): Object {
9-
try {
10-
return config.components[alias];
11-
} catch (error) {
12-
return {};
24+
function getComponentStateFromComponentConfig(component: string, oldState: Object, action: Object): Object {
25+
if (action.componentAlias === component) {
26+
return Utils.Object.mergeDeep(oldState, action.config);
1327
}
28+
return oldState;
1429
}
1530

1631
/**
@@ -25,4 +40,4 @@ function shouldRenderComponent(config: Object, alias: string) {
2540
componentConfig.constructor === Object);
2641
}
2742

28-
export {shouldRenderComponent, getComponentConfig};
43+
export {shouldRenderComponent, getComponentStateFromConfig, getComponentStateFromComponentConfig};

0 commit comments

Comments
 (0)