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

External Media: Render Media after authentication #16044

Merged
merged 1 commit into from
Jun 4, 2020
Merged
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
Expand Up @@ -20,8 +20,7 @@ import AuthInstructions from './auth-instructions';
import AuthProgress from './auth-progress';

function GooglePhotosAuth( props ) {
const { getMedia } = props;

const { setAuthenticated } = props;
const [ isAuthing, setIsAuthing ] = useState( false );

const onAuthorize = useCallback( () => {
Expand All @@ -39,15 +38,14 @@ function GooglePhotosAuth( props ) {
// Open authorize URL in a window and let it play out
requestExternalAccess( service.connect_URL, () => {
setIsAuthing( false );
const url = getApiUrl( 'list', SOURCE_GOOGLE_PHOTOS );
getMedia( url, true );
setAuthenticated( true );
} );
} )
.catch( () => {
// Not much we can tell the user at this point so let them try and auth again
setIsAuthing( false );
} );
}, [ getMedia ] );
}, [ setAuthenticated ] );

return (
<div className="jetpack-external-media-auth">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
Expand All @@ -11,7 +6,7 @@ import GooglePhotosAuth from './google-photos-auth';
import GooglePhotosMedia from './google-photos-media';

function GooglePhotos( props ) {
if ( props.requiresAuth ) {
if ( ! props.isAuthenticated ) {
return <GooglePhotosAuth { ...props } />;
}

Expand Down
13 changes: 8 additions & 5 deletions extensions/shared/external-media/sources/with-media.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ export default function withMedia() {
nextHandle: false,
isLoading: false,
isCopying: null,
requiresAuth: false,
isAuthenticated: true,
path: { ID: PATH_RECENT },
};
}

setAuthenticated = isAuthenticated => this.setState( { isAuthenticated } );

mergeMedia( initial, media ) {
return uniqBy( initial.concat( media ), 'ID' );
}
Expand Down Expand Up @@ -88,7 +90,7 @@ export default function withMedia() {

handleApiError = error => {
if ( error.code === 'authorization_required' ) {
this.setState( { requiresAuth: true, isLoading: false, isCopying: false } );
this.setState( { isAuthenticated: false, isLoading: false, isCopying: false } );
return;
}

Expand Down Expand Up @@ -121,7 +123,7 @@ export default function withMedia() {
const path = this.getRequestUrl( url );
const method = 'GET';

this.setState( { requiresAuth: false } );
this.setAuthenticated( true );

apiFetch( {
path,
Expand Down Expand Up @@ -174,7 +176,7 @@ export default function withMedia() {
}

renderContent() {
const { media, isLoading, nextHandle, requiresAuth, path } = this.state;
const { media, isLoading, nextHandle, isAuthenticated, path } = this.state;
const { noticeUI, allowedTypes, multiple = false } = this.props;

return (
Expand All @@ -189,7 +191,8 @@ export default function withMedia() {
media={ media }
pageHandle={ nextHandle }
allowedTypes={ allowedTypes }
requiresAuth={ requiresAuth }
isAuthenticated={ isAuthenticated }
setAuthenticated={ this.setAuthenticated }
multiple={ multiple }
path={ path }
onChangePath={ this.onChangePath }
Expand Down