-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Media Upload Progress component converting from Class to Function #56256
Open
dantovbein
wants to merge
1
commit into
WordPress:trunk
Choose a base branch
from
dantovbein:media-upload-progress-from-class-to-function
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import { View } from 'react-native'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { useCallback, useEffect, useMemo, useState } from '@wordpress/element'; | ||
import { Spinner } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { subscribeMediaUpload } from '@wordpress/react-native-bridge'; | ||
|
@@ -21,129 +21,143 @@ export const MEDIA_UPLOAD_STATE_SUCCEEDED = 2; | |
export const MEDIA_UPLOAD_STATE_FAILED = 3; | ||
export const MEDIA_UPLOAD_STATE_RESET = 4; | ||
|
||
export class MediaUploadProgress extends Component { | ||
constructor( props ) { | ||
super( props ); | ||
// eslint-disable-next-line @wordpress/i18n-no-collapsible-whitespace | ||
const retryMessage = __( 'Failed to insert media.\nTap for more info.' ); | ||
|
||
this.state = { | ||
progress: 0, | ||
isUploadInProgress: false, | ||
isUploadFailed: false, | ||
}; | ||
|
||
this.mediaUpload = this.mediaUpload.bind( this ); | ||
} | ||
export function MediaUploadProgress( props ) { | ||
const [ progress, setProgress ] = useState( 0 ); | ||
const [ isUploadInProgress, setIsUploadInProgress ] = useState( false ); | ||
const [ isUploadFailed, setIsUploadFailed ] = useState( false ); | ||
const [ subscriptionParentMediaUpload, setSubscriptionParentMediaUpload ] = | ||
useState( null ); | ||
const [ progressBarStyle, setProgressBarStyle ] = useState( [] ); | ||
|
||
componentDidMount() { | ||
this.addMediaUploadListener(); | ||
} | ||
const { renderContent = () => null } = props; | ||
|
||
componentWillUnmount() { | ||
this.removeMediaUploadListener(); | ||
} | ||
const mediaUpload = useCallback( | ||
( payload ) => { | ||
const { mediaId } = props; | ||
|
||
mediaUpload( payload ) { | ||
const { mediaId } = this.props; | ||
if ( payload.mediaId !== mediaId ) { | ||
return; | ||
} | ||
|
||
if ( payload.mediaId !== mediaId ) { | ||
return; | ||
} | ||
switch ( payload.state ) { | ||
case MEDIA_UPLOAD_STATE_UPLOADING: | ||
updateMediaProgress( payload ); | ||
break; | ||
case MEDIA_UPLOAD_STATE_SUCCEEDED: | ||
finishMediaUploadWithSuccess( payload ); | ||
break; | ||
case MEDIA_UPLOAD_STATE_FAILED: | ||
finishMediaUploadWithFailure( payload ); | ||
break; | ||
case MEDIA_UPLOAD_STATE_RESET: | ||
mediaUploadStateReset( payload ); | ||
break; | ||
} | ||
}, | ||
[ | ||
props, | ||
updateMediaProgress, | ||
finishMediaUploadWithSuccess, | ||
finishMediaUploadWithFailure, | ||
mediaUploadStateReset, | ||
] | ||
); | ||
|
||
const updateMediaProgress = useCallback( | ||
( payload ) => { | ||
setProgress( payload.progress ); | ||
setIsUploadInProgress( true ); | ||
setIsUploadFailed( false ); | ||
|
||
if ( props.onUpdateMediaProgress ) { | ||
props.onUpdateMediaProgress( payload ); | ||
} | ||
}, | ||
[ props.onUpdateMediaProgress ] | ||
); | ||
|
||
const finishMediaUploadWithSuccess = useCallback( | ||
( payload ) => { | ||
setIsUploadInProgress( false ); | ||
if ( props.onFinishMediaUploadWithSuccess ) { | ||
props.onFinishMediaUploadWithSuccess( payload ); | ||
} | ||
}, | ||
[ props.onFinishMediaUploadWithSuccess ] | ||
); | ||
|
||
switch ( payload.state ) { | ||
case MEDIA_UPLOAD_STATE_UPLOADING: | ||
this.updateMediaProgress( payload ); | ||
break; | ||
case MEDIA_UPLOAD_STATE_SUCCEEDED: | ||
this.finishMediaUploadWithSuccess( payload ); | ||
break; | ||
case MEDIA_UPLOAD_STATE_FAILED: | ||
this.finishMediaUploadWithFailure( payload ); | ||
break; | ||
case MEDIA_UPLOAD_STATE_RESET: | ||
this.mediaUploadStateReset( payload ); | ||
break; | ||
} | ||
} | ||
|
||
updateMediaProgress( payload ) { | ||
this.setState( { | ||
progress: payload.progress, | ||
isUploadInProgress: true, | ||
isUploadFailed: false, | ||
} ); | ||
if ( this.props.onUpdateMediaProgress ) { | ||
this.props.onUpdateMediaProgress( payload ); | ||
} | ||
} | ||
const finishMediaUploadWithFailure = useCallback( | ||
( payload ) => { | ||
setIsUploadInProgress( false ); | ||
setIsUploadFailed( true ); | ||
|
||
finishMediaUploadWithSuccess( payload ) { | ||
this.setState( { isUploadInProgress: false } ); | ||
if ( this.props.onFinishMediaUploadWithSuccess ) { | ||
this.props.onFinishMediaUploadWithSuccess( payload ); | ||
} | ||
} | ||
if ( props.onFinishMediaUploadWithFailure ) { | ||
props.onFinishMediaUploadWithFailure( payload ); | ||
} | ||
}, | ||
[ props.onFinishMediaUploadWithFailure ] | ||
); | ||
|
||
finishMediaUploadWithFailure( payload ) { | ||
this.setState( { isUploadInProgress: false, isUploadFailed: true } ); | ||
if ( this.props.onFinishMediaUploadWithFailure ) { | ||
this.props.onFinishMediaUploadWithFailure( payload ); | ||
} | ||
} | ||
const mediaUploadStateReset = useCallback( | ||
( payload ) => { | ||
setIsUploadInProgress( false ); | ||
setIsUploadFailed( false ); | ||
|
||
mediaUploadStateReset( payload ) { | ||
this.setState( { isUploadInProgress: false, isUploadFailed: false } ); | ||
if ( this.props.onMediaUploadStateReset ) { | ||
this.props.onMediaUploadStateReset( payload ); | ||
} | ||
} | ||
if ( props.onMediaUploadStateReset ) { | ||
props.onMediaUploadStateReset( payload ); | ||
} | ||
}, | ||
[ props.onMediaUploadStateReset ] | ||
); | ||
|
||
addMediaUploadListener() { | ||
const addMediaUploadListener = useCallback( () => { | ||
// If we already have a subscription not worth doing it again. | ||
if ( this.subscriptionParentMediaUpload ) { | ||
if ( subscriptionParentMediaUpload ) { | ||
return; | ||
} | ||
this.subscriptionParentMediaUpload = subscribeMediaUpload( | ||
( payload ) => { | ||
this.mediaUpload( payload ); | ||
} | ||
setSubscriptionParentMediaUpload( | ||
subscribeMediaUpload( ( payload ) => { | ||
mediaUpload( payload ); | ||
} ) | ||
); | ||
} | ||
}, [ subscriptionParentMediaUpload ] ); | ||
|
||
removeMediaUploadListener() { | ||
if ( this.subscriptionParentMediaUpload ) { | ||
this.subscriptionParentMediaUpload.remove(); | ||
const removeMediaUploadListener = useCallback( () => { | ||
if ( subscriptionParentMediaUpload ) { | ||
subscriptionParentMediaUpload.remove(); | ||
} | ||
} | ||
|
||
render() { | ||
const { renderContent = () => null } = this.props; | ||
const { isUploadInProgress, isUploadFailed } = this.state; | ||
const showSpinner = this.state.isUploadInProgress; | ||
const progress = this.state.progress * 100; | ||
// eslint-disable-next-line @wordpress/i18n-no-collapsible-whitespace | ||
const retryMessage = __( | ||
'Failed to insert media.\nTap for more info.' | ||
); | ||
}, [ subscriptionParentMediaUpload ] ); | ||
|
||
const progressBarStyle = [ | ||
useEffect( () => { | ||
setProgressBarStyle( [ | ||
styles.progressBar, | ||
showSpinner || styles.progressBarHidden, | ||
this.props.progressBarStyle, | ||
]; | ||
isUploadInProgress || styles.progressBarHidden, | ||
props.progressBarStyle, | ||
] ); | ||
}, [ isUploadInProgress, props.progressBarStyle ] ); | ||
|
||
useEffect( () => { | ||
addMediaUploadListener(); | ||
|
||
return ( | ||
return () => { | ||
removeMediaUploadListener(); | ||
}; | ||
}, [] ); | ||
|
||
return useMemo( | ||
() => ( | ||
<View | ||
style={ [ | ||
styles.mediaUploadProgress, | ||
this.props.containerStyle, | ||
] } | ||
style={ [ styles.mediaUploadProgress, props.containerStyle ] } | ||
pointerEvents="box-none" | ||
> | ||
<View style={ progressBarStyle }> | ||
{ showSpinner && ( | ||
{ isUploadInProgress && ( | ||
<Spinner | ||
progress={ progress } | ||
style={ this.props.spinnerStyle } | ||
progress={ progress * 100 } | ||
style={ props.spinnerStyle } | ||
testID="spinner" | ||
/> | ||
) } | ||
|
@@ -154,8 +168,16 @@ export class MediaUploadProgress extends Component { | |
retryMessage, | ||
} ) } | ||
</View> | ||
); | ||
} | ||
), | ||
[ | ||
isUploadInProgress, | ||
isUploadFailed, | ||
progress, | ||
progressBarStyle, | ||
renderContent, | ||
props, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sharing similar concerns about passing all |
||
] | ||
); | ||
} | ||
|
||
export default MediaUploadProgress; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm hesitant about passing all
props
to the dependency array. It feels like it defeats the purpose of having the array if the function's going to update on every render, and also doesn't seem necessary to me.In the original code,
mediaUpload
is only called when the component is mounted. Could we maybe refactor this to useuseEffect
or potentially move some of this logic to a separate custom hook within this file? I think we could make this even cleaner.