-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11633 from Automattic/add/video-editor-redux-state
Video: Redux state changes for custom video poster
- Loading branch information
Showing
21 changed files
with
584 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { mergeHandlers } from 'state/data-layer/utils'; | ||
import poster from './poster'; | ||
|
||
export default mergeHandlers( | ||
poster, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { http } from 'state/data-layer/wpcom-http/actions'; | ||
import { dispatchRequest } from 'state/data-layer/wpcom-http/utils'; | ||
import { VIDEO_EDITOR_UPDATE_POSTER } from 'state/action-types'; | ||
import { | ||
closeModal, | ||
setPosterUrl, | ||
showError, | ||
showUploadProgress, | ||
} from 'state/ui/editor/video-editor/actions'; | ||
|
||
/** | ||
* Updates the poster for a video. | ||
* | ||
* @param {Object} store Redux store | ||
* @param {Object} action Action object | ||
* @param {Function} next Dispatches to next middleware in chain | ||
* @returns {Object} original action | ||
*/ | ||
export const updatePoster = ( { dispatch }, action, next ) => { | ||
if ( ! ( 'file' in action.params || 'atTime' in action.params ) ) { | ||
return next( action ); | ||
} | ||
|
||
const { atTime, file } = action.params; | ||
const params = Object.assign( | ||
{ | ||
apiVersion: '1.1', | ||
method: 'POST', | ||
path: `/videos/${ action.videoId }/poster`, | ||
}, | ||
file && { formData: [ [ 'poster', file ] ] }, | ||
atTime !== undefined && { body: { at_time: atTime } }, | ||
); | ||
|
||
dispatch( http( params, action ) ); | ||
|
||
return next( action ); | ||
}; | ||
|
||
export const receivePosterUrl = ( { dispatch }, action, next, { poster: posterUrl } ) => { | ||
dispatch( setPosterUrl( posterUrl ) ); | ||
dispatch( closeModal() ); | ||
}; | ||
|
||
export const receivePosterError = ( { dispatch } ) => { | ||
dispatch( showError() ); | ||
}; | ||
|
||
export const receiveUploadProgress = ( { dispatch }, action, next, progress ) => { | ||
let percentage = 0; | ||
|
||
if ( 'loaded' in progress && 'total' in progress ) { | ||
percentage = Math.min( Math.round( progress.loaded / progress.total * 100 ), 100 ); | ||
} | ||
|
||
dispatch( showUploadProgress( percentage ) ); | ||
}; | ||
|
||
export const dispatchPosterRequest = | ||
dispatchRequest( updatePoster, receivePosterUrl, receivePosterError, receiveUploadProgress ); | ||
|
||
export default { | ||
[ VIDEO_EDITOR_UPDATE_POSTER ]: [ dispatchPosterRequest ], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Returns the poster upload progress. | ||
* | ||
* @param {Object} state Global state tree | ||
* @return {Number} Poster upload progress percentage. | ||
*/ | ||
export default function getPosterUploadProgress( state ) { | ||
return state.ui.editor.videoEditor.uploadProgress; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Returns the poster URL of the video. | ||
* | ||
* @param {Object} state Global state tree | ||
* @return {String} URL of the poster. | ||
*/ | ||
export default function getPosterUrl( state ) { | ||
return state.ui.editor.videoEditor.url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Returns true if the video editor modal should be closed. | ||
* | ||
* @param {Object} state Global state tree | ||
* @return {Boolean} true if the modal should be closed. | ||
* | ||
*/ | ||
export default function shouldCloseVideoEditorModal( state ) { | ||
return state.ui.editor.videoEditor.closeModal; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Returns true if an error should be shown in the video editor. | ||
* | ||
* @param {Object} state Global state tree | ||
* @return {Boolean} true if an error should be shown. | ||
* | ||
*/ | ||
export default function shouldShowVideoEditorError( state ) { | ||
return state.ui.editor.videoEditor.showError; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getPosterUploadProgress } from '../'; | ||
|
||
describe( 'getPosterUploadProgress()', () => { | ||
it( 'should return the upload progress', () => { | ||
const percentage = 50; | ||
const uploadProgress = getPosterUploadProgress( { | ||
ui: { | ||
editor: { | ||
videoEditor: { | ||
uploadProgress: percentage | ||
} | ||
} | ||
} | ||
} ); | ||
|
||
expect( uploadProgress ).to.eql( percentage ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getPosterUrl } from '../'; | ||
|
||
describe( 'getPosterUrl()', () => { | ||
it( 'should return the current video editor poster', () => { | ||
const url = 'https://i1.wp.com/videos.files.wordpress.com/dummy-guid/thumbnail.jpg?ssl=1'; | ||
const poster = getPosterUrl( { | ||
ui: { | ||
editor: { | ||
videoEditor: { | ||
url | ||
} | ||
} | ||
} | ||
} ); | ||
|
||
expect( poster ).to.eql( url ); | ||
} ); | ||
} ); |
25 changes: 25 additions & 0 deletions
25
client/state/selectors/test/should-close-video-editor-modal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { shouldCloseVideoEditorModal } from '../'; | ||
|
||
describe( 'shouldCloseVideoEditorModal()', () => { | ||
it( 'should return whether or not the video editor modal should be closed', () => { | ||
const shouldClose = shouldCloseVideoEditorModal( { | ||
ui: { | ||
editor: { | ||
videoEditor: { | ||
closeModal: false | ||
} | ||
} | ||
} | ||
} ); | ||
|
||
expect( shouldClose ).to.be.false; | ||
} ); | ||
} ); |
25 changes: 25 additions & 0 deletions
25
client/state/selectors/test/should-show-video-editor-error.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { shouldShowVideoEditorError } from '../'; | ||
|
||
describe( 'shouldShowVideoEditorError()', () => { | ||
it( 'should return the poster error state', () => { | ||
const showError = shouldShowVideoEditorError( { | ||
ui: { | ||
editor: { | ||
videoEditor: { | ||
showError: true | ||
} | ||
} | ||
} | ||
} ); | ||
|
||
expect( showError ).to.be.true; | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
VIDEO_EDITOR_CLOSE_MODAL, | ||
VIDEO_EDITOR_SET_POSTER_URL, | ||
VIDEO_EDITOR_SHOW_ERROR, | ||
VIDEO_EDITOR_SHOW_UPLOAD_PROGRESS, | ||
VIDEO_EDITOR_UPDATE_POSTER, | ||
} from 'state/action-types'; | ||
|
||
/** | ||
* Returns an action object to indicate that a request has been made to update the video poster. | ||
* | ||
* @param {String} videoId ID of the video | ||
* @param {Object} params Poster data | ||
* @param {Number} [params.atTime] Number of seconds into the video at which to get the poster | ||
* @param {Object} [params.file] An image to attach to the video | ||
* @return {Object} Action object | ||
*/ | ||
export const updatePoster = ( videoId, params ) => | ||
( { type: VIDEO_EDITOR_UPDATE_POSTER, videoId, params } ); | ||
|
||
/** | ||
* Returns an action object to indicate that the poster for the video has been updated successfully. | ||
* | ||
* @param {String} posterUrl Poster URL | ||
* @return {Object} Action object | ||
*/ | ||
export const setPosterUrl = posterUrl => ( { type: VIDEO_EDITOR_SET_POSTER_URL, posterUrl } ); | ||
|
||
/** | ||
* Returns an action object to indicate that the video editor modal should close. | ||
* | ||
* @return {Object} Action object | ||
*/ | ||
export const closeModal = () => ( { type: VIDEO_EDITOR_CLOSE_MODAL } ); | ||
|
||
/** | ||
* Returns an action object to indicate that the poster for the video failed to update. | ||
* | ||
* @return {Object} Action object | ||
*/ | ||
export const showError = () => ( { type: VIDEO_EDITOR_SHOW_ERROR } ); | ||
|
||
/** | ||
* Returns an action object to indicate the poster upload progress. | ||
* | ||
* @param {String} percentage Upload progress percentage | ||
* @return {Object} Action object | ||
*/ | ||
export const showUploadProgress = percentage => | ||
( { type: VIDEO_EDITOR_SHOW_UPLOAD_PROGRESS, percentage } ); |
Oops, something went wrong.