| 1 |
import React, { useState, useEffect } from 'react'; |
| 2 |
import { Tooltip } from '@WizardFields'; |
| 3 |
import { addFilter } from '@wordpress/hooks'; |
| 4 |
import { sendPostMessage } from '@Utils/Helpers'; |
| 5 |
import { PhotoIcon, XMarkIcon } from '@heroicons/react/24/outline'; |
| 6 |
|
| 7 |
import { __ } from '@wordpress/i18n'; |
| 8 |
import { useStateValue } from '../../utils/StateProvider'; |
| 9 |
import { MediaUpload } from '@wordpress/media-utils'; |
| 10 |
|
| 11 |
function UploadSiteLogo( props ) { |
| 12 |
const { defaultPageBuilder } = props; |
| 13 |
const replaceMediaUpload = () => MediaUpload; |
| 14 |
const [ { site_logo }, dispatch ] = useStateValue(); |
| 15 |
|
| 16 |
addFilter( |
| 17 |
'editor.MediaUpload', |
| 18 |
'core/edit-post/components/media-upload/replace-media-upload', |
| 19 |
replaceMediaUpload |
| 20 |
); |
| 21 |
|
| 22 |
const [ imgUpdated, setImageUpdated ] = useState( false ); |
| 23 |
|
| 24 |
useEffect( () => { |
| 25 |
let temp_site_logo = site_logo; |
| 26 |
|
| 27 |
if ( '' === temp_site_logo && '' !== cartflows_wizard.site_logo.url ) { |
| 28 |
temp_site_logo = cartflows_wizard.site_logo; |
| 29 |
} |
| 30 |
|
| 31 |
updateValues( temp_site_logo ); |
| 32 |
}, [] ); |
| 33 |
|
| 34 |
/** |
| 35 |
* Prepare the selected image array and update it in the preview iframe. |
| 36 |
* |
| 37 |
* @param {media} media |
| 38 |
*/ |
| 39 |
const onSelectImage = ( media ) => { |
| 40 |
const mediaData = { |
| 41 |
id: media.id, |
| 42 |
url: media.url, |
| 43 |
width: site_logo.width, |
| 44 |
}; |
| 45 |
|
| 46 |
updateValues( mediaData ); |
| 47 |
}; |
| 48 |
|
| 49 |
/** |
| 50 |
* Remove the selected image from the iframe preview. |
| 51 |
*/ |
| 52 |
const removeImage = () => { |
| 53 |
updateValues( '' ); |
| 54 |
}; |
| 55 |
|
| 56 |
/** |
| 57 |
* Update the selected image value in the state and on the iframe preview. |
| 58 |
* |
| 59 |
* @param {data} data |
| 60 |
*/ |
| 61 |
const updateValues = ( data ) => { |
| 62 |
dispatch( { |
| 63 |
status: 'SET_SITE_LOGO', |
| 64 |
site_logo: data, |
| 65 |
} ); |
| 66 |
|
| 67 |
// Change the preview. |
| 68 |
changelogoInPreview( data ); |
| 69 |
setImageUpdated( ! imgUpdated ); |
| 70 |
}; |
| 71 |
|
| 72 |
/** |
| 73 |
* Send the data to the iframe preview window using windows messaging feature. |
| 74 |
* |
| 75 |
* @param {data} data |
| 76 |
*/ |
| 77 |
const changelogoInPreview = ( data ) => { |
| 78 |
if ( '' === data ) { |
| 79 |
sendPostMessage( { |
| 80 |
action: 'clearHeaderLogo', |
| 81 |
data: { |
| 82 |
default_builder: defaultPageBuilder, |
| 83 |
site_logo: [], |
| 84 |
}, |
| 85 |
} ); |
| 86 |
} else { |
| 87 |
sendPostMessage( { |
| 88 |
action: 'changeHeaderLogo', |
| 89 |
data: { |
| 90 |
default_builder: defaultPageBuilder, |
| 91 |
site_logo: data, |
| 92 |
}, |
| 93 |
} ); |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
const logo_btn_text = |
| 98 |
'' === site_logo || undefined === site_logo.url |
| 99 |
? __( 'Upload a Logo', 'cartflows' ) |
| 100 |
: __( 'Change a Logo', 'cartflows' ); |
| 101 |
return ( |
| 102 |
<> |
| 103 |
<div className="wcf-options--row"> |
| 104 |
<MediaUpload |
| 105 |
onSelect={ ( media ) => onSelectImage( media ) } |
| 106 |
allowedTypes={ [ 'image' ] } |
| 107 |
value={ site_logo.id } |
| 108 |
// multiple={ false } |
| 109 |
render={ ( { open } ) => ( |
| 110 |
<> |
| 111 |
<div className="wcf-media-upload-wrapper flex gap-4 items-center"> |
| 112 |
{ '' !== site_logo.url && |
| 113 |
undefined !== site_logo.url ? ( |
| 114 |
<div className="wcf-site-logo-wrapper"> |
| 115 |
<div className="wcf-media-upload--selected-image"> |
| 116 |
<div className="wcf-media-upload--preview relative"> |
| 117 |
<span |
| 118 |
className="wcf-close-site-logo absolute p-0.5 -left-2.5 bg-white rounded-full border border-gray-300 -top-1.5 cursor-pointer" |
| 119 |
onClick={ removeImage } |
| 120 |
> |
| 121 |
<Tooltip |
| 122 |
text={ __( |
| 123 |
'Remove logo', |
| 124 |
'cartflows' |
| 125 |
) } |
| 126 |
classes={ |
| 127 |
'text-gray-400 !ml-0' |
| 128 |
} |
| 129 |
icon={ |
| 130 |
<XMarkIcon |
| 131 |
className="w-2.5 h-2.5 stroke-2 text-gray-600 hover:text-gray-800" |
| 132 |
aria-hidden="true" |
| 133 |
/> |
| 134 |
} |
| 135 |
/> |
| 136 |
</span> |
| 137 |
<img |
| 138 |
src={ site_logo?.url } |
| 139 |
alt={ |
| 140 |
'wcf-selected-logo-preview' |
| 141 |
} |
| 142 |
className="wcf-selected-image w-auto h-auto max-w-44 max-h-11" |
| 143 |
data-logo-data={ JSON.stringify( |
| 144 |
site_logo |
| 145 |
) } |
| 146 |
/> |
| 147 |
</div> |
| 148 |
</div> |
| 149 |
</div> |
| 150 |
) : ( |
| 151 |
'' |
| 152 |
) } |
| 153 |
|
| 154 |
<button |
| 155 |
className="wcf-media-upload-button relative inline-flex justify-center items-center gap-1.5 rounded px-4 py-2.5 text-sm font-normal leading-4 shadow-sm cursor-pointer bg-primary-25 border border-primary-300 text-primary-500 focus:bg-primary-50 focus:text-primary-600 focus:ring-offset-2 focus:ring-2 focus:ring-primary-500 focus:outline-none wcf-inline-tooltip" |
| 156 |
onClick={ open } |
| 157 |
data-tooltip={ __( |
| 158 |
'Suggested Dimensions: 180x60 pixels', |
| 159 |
'cartflows' |
| 160 |
) } |
| 161 |
> |
| 162 |
<PhotoIcon |
| 163 |
className="w-5 h-5 stroke-2" |
| 164 |
aria-hidden="true" |
| 165 |
/> |
| 166 |
{ logo_btn_text } |
| 167 |
</button> |
| 168 |
</div> |
| 169 |
</> |
| 170 |
) } |
| 171 |
/> |
| 172 |
</div> |
| 173 |
</> |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
export default UploadSiteLogo; |
| 178 |
|