Skip to content

Commit

Permalink
Use withAPIData HOC
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed May 30, 2018
1 parent 0be3946 commit 677b35c
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions core-blocks/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ClipboardButton,
IconButton,
Toolbar,
withAPIData,
withNotices,
} from '@wordpress/components';
import { Component, compose, Fragment } from '@wordpress/element';
Expand All @@ -32,7 +33,6 @@ class FileEdit extends Component {
const {
showDownloadButton = true,
buttonText = __( 'Download' ),
id,
} = this.props.attributes;

this.onSelectFile = this.onSelectFile.bind( this );
Expand All @@ -44,25 +44,8 @@ class FileEdit extends Component {
} );

this.state = {
attachmentPage: undefined,
showCopyConfirmation: false,
};

if ( id !== undefined ) {
// Get Attachment Page URL
wp.apiRequest( {
path: `/wp/v2/media/${ id }`,
method: 'GET',
} )
.then(
( result ) => {
this.setState( { attachmentPage: result.link } );
},
() => {
this.setState( { attachmentPage: undefined } );
}
);
}
}

componentDidMount() {
Expand Down Expand Up @@ -96,10 +79,7 @@ class FileEdit extends Component {
fileName: media.title,
textLinkHref: media.url,
id: media.id,
} );
this.setState( {
attachmentPage: media.link,
} );
}, this.props.media.get /* refetch attachment page url */ );
}
}

Expand All @@ -123,8 +103,10 @@ class FileEdit extends Component {
setAttributes,
noticeUI,
noticeOperations,
media,
} = this.props;
const { attachmentPage, showCopyConfirmation } = this.state;
const { showCopyConfirmation } = this.state;
const attachmentPage = ( id !== undefined ) && media.data && media.data.link;

const classNames = [
className,
Expand Down Expand Up @@ -239,5 +221,8 @@ class FileEdit extends Component {
}

export default compose( [
withAPIData( ( props ) => ( {
media: `/wp/v2/media/${ props.attributes.id }`,
} ) ),
withNotices,
] )( FileEdit );

0 comments on commit 677b35c

Please sign in to comment.