Skip to content

Commit

Permalink
Update Redux state when video has loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
donnapep committed Mar 14, 2017
1 parent ca1dc4e commit 68a9d5f
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import debug from 'debug';
import { loadScript, removeScriptCallback } from 'lib/load-script';
import {
setVideoEditorHasScriptLoadError,
setVideoEditorVideoHasLoaded,
} from 'state/ui/editor/video-editor/actions';

/**
Expand All @@ -36,6 +37,7 @@ class EditorMediaModalDetailPreviewVideoPress extends Component {

componentDidMount() {
this.loadInitializeScript();
window.addEventListener( 'message', this.receiveMessage, false );
}

componentWillUnmount() {
Expand Down Expand Up @@ -93,7 +95,27 @@ class EditorMediaModalDetailPreviewVideoPress extends Component {
}
};

receiveMessage = ( event ) => {
if ( event.origin && event.origin !== location.origin ) {
return;
}

const data = 'string' === typeof event.data
? JSON.parse( event.data )
: event.data;

if ( ! data || 'videopress_loading_state' !== data.event || ! ( 'state' in data ) ) {
return;
}

if ( 'loaded' === data.state ) {
this.props.setVideoHasLoaded();
}
}

destroy() {
window.removeEventListener( 'message', this.receiveMessage );

if ( ! this.player ) {
return;
}
Expand Down Expand Up @@ -140,5 +162,6 @@ export default connect(
null,
{
setHasScriptLoadError: setVideoEditorHasScriptLoadError,
setVideoHasLoaded: setVideoEditorVideoHasLoaded,
}
)( EditorMediaModalDetailPreviewVideoPress );

0 comments on commit 68a9d5f

Please sign in to comment.