1- /* global cloudinaryGalleryApi */
1+ /* global cloudinaryGalleryApi cloudinaryGalleryConfig */
2+
3+ /**
4+ * External dependencies
5+ */
6+ import Dot from 'dot-object' ;
27
38/**
49 * WordPress dependencies
510 */
611import { __ } from '@wordpress/i18n' ;
12+ import { Spinner } from '@wordpress/components' ;
713import '@wordpress/components/build-style/style.css' ;
814import { useEffect , useMemo , useState } from '@wordpress/element' ;
915import { InspectorControls , MediaPlaceholder } from '@wordpress/block-editor' ;
@@ -16,51 +22,72 @@ import Controls from './controls';
1622import { ALLOWED_MEDIA_TYPES } from './options' ;
1723import { generateId , setupAttributesForRendering , showNotice } from './utils' ;
1824
25+ const dot = new Dot ( '_' ) ;
26+
1927const PLACEHOLDER_TEXT = __ (
2028 'Drag images, upload new ones or select files from your library.' ,
2129 'cloudinary'
2230) ;
2331
24- function galleryWidgetConfig ( config , container ) {
25- return {
26- cloudName : CLDN . mloptions . cloud_name ,
27- ...config ,
28- container : '.' + container ,
29- zoom : false ,
30- } ;
31- }
32+ const galleryWidgetConfig = ( config , container ) => ( {
33+ ...config ,
34+ container : '.' + container ,
35+ zoom : false ,
36+ } ) ;
3237
3338const Edit = ( { setAttributes, attributes, className, isSelected } ) => {
3439 const [ errorMessage , setErrorMessage ] = useState ( null ) ;
40+ const [ loading , setLoading ] = useState ( false ) ;
41+
42+ const preparedAttributes = useMemo ( ( ) => {
43+ const defaultAttrs = { } ;
44+
45+ // eslint-disable-next-line no-unused-vars
46+ const { container, ...flattenedAttrs } = dot . dot (
47+ cloudinaryGalleryConfig
48+ ) ;
49+
50+ Object . keys ( flattenedAttrs ) . forEach ( ( attr ) => {
51+ if ( ! attributes [ attr ] ) {
52+ defaultAttrs [ attr ] = flattenedAttrs [ attr ] ;
53+ }
54+ } ) ;
55+
56+ return { ...attributes , ...defaultAttrs } ;
57+ } , [ attributes ] ) ;
3558
3659 const getAttachmentIds = useMemo ( ( ) => {
3760 if ( ! attributes . selectedImages . length ) {
3861 return [ ] ;
3962 }
4063
41- return attributes . selectedImages . map ( ( image ) => ( {
42- id : image . attachmentId ,
64+ return attributes . selectedImages . map ( ( { attachmentId } ) => ( {
65+ id : attachmentId ,
4366 } ) ) ;
4467 } , [ attributes ] ) ;
4568
46- const onSelect = ( images ) => {
47- fetch ( cloudinaryGalleryApi . endpoint , {
48- method : 'POST' ,
49- body : JSON . stringify ( { images } ) ,
50- headers : {
51- 'X-WP-Nonce' : cloudinaryGalleryApi . nonce ,
52- } ,
53- } )
54- . then ( ( res ) => res . json ( ) )
55- . then ( ( selectedImages ) => setAttributes ( { selectedImages } ) )
56- . catch ( ( ) =>
57- setErrorMessage (
58- __ (
59- 'Could not load selected images. Please try again.' ,
60- 'cloudinary'
61- )
69+ const onSelect = async ( images ) => {
70+ setLoading ( true ) ;
71+
72+ try {
73+ const res = await fetch ( cloudinaryGalleryApi . endpoint , {
74+ method : 'POST' ,
75+ body : JSON . stringify ( { images } ) ,
76+ headers : {
77+ 'X-WP-Nonce' : cloudinaryGalleryApi . nonce ,
78+ } ,
79+ } ) ;
80+
81+ const selectedImages = await res . json ( ) ;
82+ setAttributes ( { selectedImages } ) ;
83+ } catch {
84+ setErrorMessage (
85+ __ (
86+ 'Could not load selected images. Please try again.' ,
87+ 'cloudinary'
6288 )
6389 ) ;
90+ }
6491 } ;
6592
6693 useEffect ( ( ) => {
@@ -76,12 +103,6 @@ const Edit = ( { setAttributes, attributes, className, isSelected } ) => {
76103 attributes
77104 ) ;
78105
79- if ( ! attributes . container ) {
80- setAttributes ( {
81- container : `${ className } ${ generateId ( 15 ) } ` ,
82- } ) ;
83- }
84-
85106 try {
86107 gallery = cloudinary . galleryWidget (
87108 galleryWidgetConfig (
@@ -96,13 +117,22 @@ const Edit = ( { setAttributes, attributes, className, isSelected } ) => {
96117 }
97118
98119 gallery . render ( ) ;
120+ setLoading ( false ) ;
99121
100122 return ( ) => gallery . destroy ( ) ;
101123 }
102124 } , [ errorMessage , attributes , setAttributes , className ] ) ;
103125
104126 const hasImages = ! ! attributes . selectedImages . length ;
105127
128+ if ( ! attributes . container ) {
129+ setAttributes ( {
130+ container : `${ className } ${ generateId ( 15 ) } ` ,
131+ } ) ;
132+ }
133+
134+ setAttributes ( preparedAttributes ) ;
135+
106136 return (
107137 < >
108138 < >
@@ -120,10 +150,16 @@ const Edit = ( { setAttributes, attributes, className, isSelected } ) => {
120150 allowedTypes = { ALLOWED_MEDIA_TYPES }
121151 addToGallery = { hasImages }
122152 isAppender = { hasImages }
123- onSelect = { onSelect }
153+ onSelect = { ( images ) => onSelect ( images ) }
124154 value = { getAttachmentIds }
125155 multiple
126- />
156+ >
157+ { loading && (
158+ < div className = "loading-spinner-container" >
159+ < Spinner />
160+ </ div >
161+ ) }
162+ </ MediaPlaceholder >
127163 </ div >
128164 </ >
129165 < InspectorControls >
0 commit comments