Skip to content
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

[RN Mobile] Audio Block: Add from URL - Refactor Prompt component [WIP] #31853

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { LinkSettingsNavigation } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { isURL } from '@wordpress/url';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';

const linkSettingsOptions = {
url: {
label: __( 'Insert from URL' ),
placeholder: __( 'Add link' ),
autoFocus: true,
autoFill: true,
},
};

const AddFromUrlBottomSheet = ( { value, onClose, isVisible } ) => {
const [ url, setUrl ] = useState( value );
const { createErrorNotice } = useDispatch( noticesStore );

function onCloseWithUrl() {
if ( isURL( url ) ) {
onClose( { url } );
} else {
createErrorNotice( __( 'Invalid URL. Please enter a valid URL.' ) );
onClose( {} );
}
}

return (
<LinkSettingsNavigation
isVisible={ isVisible }
url={ url }
setAttributes={ ( attributes ) => setUrl( attributes.url ) }
onClose={ onCloseWithUrl }
options={ linkSettingsOptions }
withBottomSheet
showIcon
/>
);
};

export default AddFromUrlBottomSheet;
68 changes: 34 additions & 34 deletions packages/block-editor/src/components/media-upload/index.native.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/**
* Internal dependencies
*/
import AddFromUrlBottomSheet from './add-from-url-bottom-sheet';
/**
* External dependencies
*/
import { Platform } from 'react-native';
import { View, Platform } from 'react-native';

import { delay } from 'lodash';

import prompt from 'react-native-prompt-android';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -46,6 +48,7 @@ export class MediaUpload extends Component {
this.getAllSources = this.getAllSources.bind( this );
this.state = {
otherMediaOptions: [],
isAddFromUrlBottomsheetVisible: false,
};
}

Expand Down Expand Up @@ -178,31 +181,10 @@ export class MediaUpload extends Component {
}

onPickerSelect( value ) {
const {
allowedTypes = [],
onSelect,
onSelectURL,
multiple = false,
} = this.props;
const { allowedTypes = [], onSelect, multiple = false } = this.props;

if ( value === 'URL' ) {
prompt(
__( 'Type a URL' ), // title
undefined, // message
[
{
text: __( 'Cancel' ),
style: 'cancel',
},
{
text: __( 'Apply' ),
onPress: ( url ) => onSelectURL( url ),
},
], // buttons
'plain-text', // type
undefined, // defaultValue
'url' // keyboardType
);
this.setState( { isAddFromUrlBottomsheetVisible: true } );
return;
}

Expand All @@ -221,7 +203,12 @@ export class MediaUpload extends Component {
}

render() {
const { allowedTypes = [], isReplacingMedia, multiple } = this.props;
const {
allowedTypes = [],
isReplacingMedia,
multiple,
onSelectURL,
} = this.props;
const isOneType = allowedTypes.length === 1;
const isImage = isOneType && allowedTypes.includes( MEDIA_TYPE_IMAGE );
const isVideo = isOneType && allowedTypes.includes( MEDIA_TYPE_VIDEO );
Expand Down Expand Up @@ -270,13 +257,26 @@ export class MediaUpload extends Component {
}

const getMediaOptions = () => (
<Picker
title={ pickerTitle }
hideCancelButton
ref={ ( instance ) => ( this.picker = instance ) }
options={ this.getMediaOptionsItems() }
onChange={ this.onPickerSelect }
/>
<View>
{ this.state.isAddFromUrlBottomsheetVisible && (
<AddFromUrlBottomSheet
onClose={ ( { url } ) => {
this.setState( {
isAddFromUrlBottomsheetVisible: false,
} );
Comment on lines +262 to +266
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I like handling the state for the bottom sheet here. I wanted to encapsulate it within the AddFromUrlBottomSheet the issue I was having was the AddFromUrlBottomSheet is a function component while the MediaUpload is a class component, so the ref ideas I had weren't clear to me. Let me know what you think. This sheet is based on #31343

onSelectURL( url );
} }
isVisible={ this.state.isAddFromUrlBottomsheetVisible }
/>
) }
<Picker
title={ pickerTitle }
hideCancelButton
ref={ ( instance ) => ( this.picker = instance ) }
options={ this.getMediaOptionsItems() }
onChange={ this.onPickerSelect }
/>
</View>
);

return this.props.render( {
Expand Down
7 changes: 1 addition & 6 deletions packages/block-library/src/audio/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { audio as icon, replace } from '@wordpress/icons';
import { useState } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { isURL } from '@wordpress/url';

/**
* Internal dependencies
Expand Down Expand Up @@ -78,11 +77,7 @@ function AudioEdit( {

function onSelectURL( newSrc ) {
if ( newSrc !== src ) {
if ( isURL( newSrc ) ) {
setAttributes( { src: newSrc, id: undefined } );
} else {
createErrorNotice( __( 'Invalid URL. Audio file not found.' ) );
}
setAttributes( { src: newSrc, id: undefined } );
}
}

Expand Down