-
Notifications
You must be signed in to change notification settings - Fork 806
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
35 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
.../packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx
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,57 @@ | ||
import { useState } from 'react'; | ||
import wpcomRequest from 'wpcom-proxy-request'; | ||
|
||
export default function WpcomMediaUrlImportForm( { siteId } ) { | ||
const [ url, setUrl ] = useState( '' ); | ||
|
||
const handleUrlChange = e => { | ||
setUrl( e.target.value ); | ||
}; | ||
|
||
const handleSubmit = async e => { | ||
try { | ||
new URL(url); | ||
Check failure on line 13 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx GitHub Actions / ESLint (non-excluded files only)
|
||
} catch (e) { | ||
Check failure on line 14 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx GitHub Actions / ESLint (non-excluded files only)
Check failure on line 14 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx GitHub Actions / ESLint (non-excluded files only)
|
||
return false; | ||
} | ||
e.preventDefault(); | ||
|
||
console.log({ url, siteId } ); | ||
Check failure on line 19 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx GitHub Actions / ESLint (non-excluded files only)
|
||
|
||
console.log({ wpcomRequest }); | ||
|
||
window.wpcomRequest = wpcomRequest; | ||
|
||
// NOT WORKING!! | ||
|
||
const response = await wpcomRequest( { | ||
method: 'POST', | ||
apiVersion: '2', | ||
apiNamespace: 'rest/v1.1', | ||
path: `/sites/${ siteId }/media/new`, | ||
body: { | ||
media_urls: [ url ], | ||
} | ||
}); | ||
|
||
|
||
console.log('FINISH'); | ||
console.log({ response}); | ||
|
||
return false; | ||
} | ||
|
||
return ( | ||
<div class="wrap"> | ||
<p>or import from URL YAY!</p> | ||
<form onsubmit="return false;"> | ||
<input | ||
type="url" | ||
value={ url } | ||
onChange={ handleUrlChange } | ||
required /> | ||
<button onClick={ handleSubmit } class="button button-primary">Import</button> | ||
</form> | ||
</div> | ||
); | ||
} |
22 changes: 19 additions & 3 deletions
22
projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.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 |
---|---|---|
@@ -1,5 +1,21 @@ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import WpcomMediaUrlImportForm from './wpcom-media-url-import-form'; | ||
|
||
document.addEventListener( 'DOMContentLoaded', () => { | ||
console.log( 'TESTING' ); | ||
const props = typeof window === 'object' ? window.JETPACK_MU_WPCOM_MEDIA_URL_IMPORT : {}; | ||
|
||
document.addEventListener( 'DOMContentLoaded', function () { | ||
const observer = new MutationObserver( mutations => { | ||
mutations.forEach( mutation => { | ||
if ( mutation.addedNodes.length > 0 ) { | ||
const container = document.getElementById( 'wpcom-media-url-import' ); | ||
if ( container ) { | ||
const root = ReactDOM.createRoot( container ); | ||
root.render( <WpcomMediaUrlImportForm { ...props } /> ); | ||
observer.disconnect(); | ||
} | ||
} | ||
} ); | ||
} ); | ||
observer.observe( document.body, { childList: true, subtree: 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