| 1 |
/* eslint-disable jsx-a11y/no-static-element-interactions */ |
| 2 |
/* eslint-disable jsx-a11y/click-events-have-key-events */ |
| 3 |
import { useContext, useEffect, useState, useCallback } from 'react'; |
| 4 |
import { OnboardingContext } from '../context/context'; |
| 5 |
import { useNavigate } from '@reach/router'; |
| 6 |
import useAjax from 'elementor-app/hooks/use-ajax'; |
| 7 |
import DropZone from 'elementor-app/organisms/drop-zone'; |
| 8 |
import UnfilteredFilesDialog from 'elementor-app/organisms/unfiltered-files-dialog'; |
| 9 |
|
| 10 |
import Layout from '../components/layout/layout'; |
| 11 |
import PageContentLayout from '../components/layout/page-content-layout'; |
| 12 |
|
| 13 |
export default function SiteLogo() { |
| 14 |
const { state, updateState, getStateObjectToUpdate } = useContext( OnboardingContext ), |
| 15 |
[ file, setFile ] = useState( state.siteLogo.id ? state.siteLogo : null ), |
| 16 |
[ isUploading, setIsUploading ] = useState( false ), |
| 17 |
[ showUnfilteredFilesDialog, setShowUnfilteredFilesDialog ] = useState( false ), |
| 18 |
[ fileSource, setFileSource ] = useState(), |
| 19 |
[ noticeState, setNoticeState ] = useState( null ), |
| 20 |
{ ajaxState: updateLogoAjaxState, setAjax: setUpdateLogoAjax } = useAjax(), |
| 21 |
{ ajaxState: uploadImageAjaxState, setAjax: setUploadImageAjax } = useAjax(), |
| 22 |
pageId = 'siteLogo', |
| 23 |
nextStep = 'goodToGo', |
| 24 |
navigate = useNavigate(), |
| 25 |
actionButton = { |
| 26 |
role: 'button', |
| 27 |
onClick: () => { |
| 28 |
elementorCommon.events.dispatchEvent( { |
| 29 |
event: 'next', |
| 30 |
version: '', |
| 31 |
details: { |
| 32 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 33 |
step: state.currentStep, |
| 34 |
}, |
| 35 |
} ); |
| 36 |
|
| 37 |
if ( file.id ) { |
| 38 |
if ( file.id !== state.siteLogo.id ) { |
| 39 |
updateSiteLogo(); |
| 40 |
} else { |
| 41 |
// If the currently displayed logo is already set as the site logo, just go to the next screen. |
| 42 |
const stateToUpdate = getStateObjectToUpdate( state, 'steps', pageId, 'completed' ); |
| 43 |
|
| 44 |
updateState( stateToUpdate ); |
| 45 |
|
| 46 |
navigate( 'onboarding/' + nextStep ); |
| 47 |
} |
| 48 |
} |
| 49 |
}, |
| 50 |
}; |
| 51 |
|
| 52 |
let skipButton; |
| 53 |
|
| 54 |
if ( 'completed' !== state.steps[ pageId ] ) { |
| 55 |
skipButton = { |
| 56 |
text: __( 'Skip', 'elementor' ), |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
if ( isUploading ) { |
| 61 |
actionButton.text = ( |
| 62 |
<> |
| 63 |
<i className="eicon-loading eicon-animation-spin" aria-hidden="true" /> |
| 64 |
</> |
| 65 |
); |
| 66 |
} else { |
| 67 |
actionButton.text = __( 'Next', 'elementor' ); |
| 68 |
} |
| 69 |
|
| 70 |
if ( ! file ) { |
| 71 |
actionButton.className = 'e-onboarding__button--disabled'; |
| 72 |
} |
| 73 |
|
| 74 |
const updateSiteLogo = useCallback( () => { |
| 75 |
setIsUploading( true ); |
| 76 |
|
| 77 |
setUpdateLogoAjax( { |
| 78 |
data: { |
| 79 |
action: 'elementor_update_site_logo', |
| 80 |
data: JSON.stringify( { |
| 81 |
attachmentId: file.id, |
| 82 |
} ), |
| 83 |
}, |
| 84 |
} ); |
| 85 |
}, [ file ] ); |
| 86 |
|
| 87 |
const uploadSiteLogo = ( fileToUpload ) => { |
| 88 |
setIsUploading( true ); |
| 89 |
|
| 90 |
setUploadImageAjax( { |
| 91 |
data: { |
| 92 |
action: 'elementor_upload_site_logo', |
| 93 |
fileToUpload, |
| 94 |
}, |
| 95 |
} ); |
| 96 |
}; |
| 97 |
|
| 98 |
const dismissUnfilteredFilesCallback = () => { |
| 99 |
setIsUploading( false ); |
| 100 |
|
| 101 |
setFile( null ); |
| 102 |
|
| 103 |
setShowUnfilteredFilesDialog( false ); |
| 104 |
}; |
| 105 |
|
| 106 |
const onFileSelect = ( selectedFile ) => { |
| 107 |
setFileSource( 'drop' ); |
| 108 |
|
| 109 |
if ( 'image/svg+xml' === selectedFile.type && ! elementorAppConfig.onboarding.isUnfilteredFilesEnabled ) { |
| 110 |
setFile( selectedFile ); |
| 111 |
|
| 112 |
setIsUploading( true ); |
| 113 |
|
| 114 |
setShowUnfilteredFilesDialog( true ); |
| 115 |
} else { |
| 116 |
setFile( selectedFile ); |
| 117 |
|
| 118 |
setNoticeState( null ); |
| 119 |
|
| 120 |
uploadSiteLogo( selectedFile ); |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
const onImageRemoveClick = () => { |
| 125 |
elementorCommon.events.dispatchEvent( { |
| 126 |
event: 'remove selected logo', |
| 127 |
version: '', |
| 128 |
details: { |
| 129 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 130 |
}, |
| 131 |
} ); |
| 132 |
|
| 133 |
setFile( null ); |
| 134 |
}; |
| 135 |
|
| 136 |
/** |
| 137 |
* Ajax Callbacks |
| 138 |
*/ |
| 139 |
// Run the callback for the new image upload AJAX request. |
| 140 |
useEffect( () => { |
| 141 |
if ( 'initial' !== uploadImageAjaxState.status ) { |
| 142 |
if ( 'success' === uploadImageAjaxState.status && uploadImageAjaxState.response?.imageAttachment?.id ) { |
| 143 |
elementorCommon.events.dispatchEvent( { |
| 144 |
event: 'logo image uploaded', |
| 145 |
version: '', |
| 146 |
details: { |
| 147 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 148 |
source: fileSource, |
| 149 |
}, |
| 150 |
} ); |
| 151 |
|
| 152 |
setIsUploading( false ); |
| 153 |
|
| 154 |
setFile( uploadImageAjaxState.response.imageAttachment ); |
| 155 |
|
| 156 |
if ( noticeState ) { |
| 157 |
setNoticeState( null ); |
| 158 |
} |
| 159 |
} else if ( 'error' === uploadImageAjaxState.status ) { |
| 160 |
setIsUploading( false ); |
| 161 |
|
| 162 |
setFile( null ); |
| 163 |
|
| 164 |
elementorCommon.events.dispatchEvent( { |
| 165 |
event: 'indication prompt', |
| 166 |
version: '', |
| 167 |
details: { |
| 168 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 169 |
action_state: 'failure', |
| 170 |
action: 'logo image upload', |
| 171 |
}, |
| 172 |
} ); |
| 173 |
|
| 174 |
setNoticeState( { |
| 175 |
type: 'error', |
| 176 |
icon: 'eicon-warning', |
| 177 |
message: 'That didn\'t work. Try uploading your file again.', |
| 178 |
} ); |
| 179 |
} |
| 180 |
} |
| 181 |
}, [ uploadImageAjaxState.status ] ); |
| 182 |
|
| 183 |
// Run the callback for the site logo update AJAX request. |
| 184 |
useEffect( () => { |
| 185 |
if ( 'initial' !== updateLogoAjaxState.status ) { |
| 186 |
if ( 'success' === updateLogoAjaxState.status && updateLogoAjaxState.response?.siteLogoUpdated ) { |
| 187 |
elementorCommon.events.dispatchEvent( { |
| 188 |
event: 'logo image updated', |
| 189 |
version: '', |
| 190 |
details: { |
| 191 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 192 |
source: fileSource, |
| 193 |
}, |
| 194 |
} ); |
| 195 |
|
| 196 |
setIsUploading( false ); |
| 197 |
|
| 198 |
if ( noticeState ) { |
| 199 |
setNoticeState( null ); |
| 200 |
} |
| 201 |
|
| 202 |
const stateToUpdate = getStateObjectToUpdate( state, 'steps', pageId, 'completed' ); |
| 203 |
|
| 204 |
stateToUpdate.siteLogo = { |
| 205 |
id: file.id, |
| 206 |
url: file.url, |
| 207 |
}; |
| 208 |
|
| 209 |
updateState( stateToUpdate ); |
| 210 |
|
| 211 |
navigate( 'onboarding/' + nextStep ); |
| 212 |
} else if ( 'error' === updateLogoAjaxState.status ) { |
| 213 |
setIsUploading( false ); |
| 214 |
|
| 215 |
elementorCommon.events.dispatchEvent( { |
| 216 |
event: 'indication prompt', |
| 217 |
version: '', |
| 218 |
details: { |
| 219 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 220 |
step: state.currentStep, |
| 221 |
action_state: 'failure', |
| 222 |
action: 'update site logo', |
| 223 |
}, |
| 224 |
} ); |
| 225 |
|
| 226 |
setNoticeState( { |
| 227 |
type: 'error', |
| 228 |
icon: 'eicon-warning', |
| 229 |
message: 'That didn\'t work. Try uploading your file again.', |
| 230 |
} ); |
| 231 |
} |
| 232 |
} |
| 233 |
}, [ updateLogoAjaxState.status ] ); |
| 234 |
|
| 235 |
return ( |
| 236 |
<Layout pageId={ pageId } nextStep={ nextStep }> |
| 237 |
<PageContentLayout |
| 238 |
image={ elementorCommon.config.urls.assets + 'images/app/onboarding/Illustration_Setup.svg' } |
| 239 |
title={ __( 'Have a logo? Add it here.', 'elementor' ) } |
| 240 |
actionButton={ actionButton } |
| 241 |
skipButton={ skipButton } |
| 242 |
noticeState={ noticeState } |
| 243 |
> |
| 244 |
<span> |
| 245 |
{ __( 'Otherwise, you can skip this and add one later.', 'elementor' ) } |
| 246 |
</span> |
| 247 |
{ ( file && ! showUnfilteredFilesDialog ) |
| 248 |
? ( |
| 249 |
<div className={ 'e-onboarding__logo-container' + ( isUploading ? ' e-onboarding__is-uploading' : '' ) }> |
| 250 |
<div className="e-onboarding__logo-remove" onClick={ () => onImageRemoveClick() }> |
| 251 |
<i className="eicon-trash-o" /> |
| 252 |
</div> |
| 253 |
<img src={ file.url } alt={ __( 'Potential Site Logo', 'elementor' ) } /> |
| 254 |
</div> |
| 255 |
) |
| 256 |
: <> |
| 257 |
<DropZone |
| 258 |
className="e-onboarding__drop-zone" |
| 259 |
heading={ __( 'Drop image here', 'elementor' ) } |
| 260 |
secondaryText={ __( 'or', 'elementor' ) } |
| 261 |
buttonText={ __( 'Open Media Library', 'elementor' ) } |
| 262 |
buttonVariant="outlined" |
| 263 |
buttonColor="cta" |
| 264 |
icon={ '' } |
| 265 |
type="wp-media" |
| 266 |
filetypes={ [ 'jpg', 'jpeg', 'png', 'svg' ] } |
| 267 |
onFileSelect={ ( selectedFile ) => onFileSelect( selectedFile ) } |
| 268 |
onWpMediaSelect={ ( frame ) => { |
| 269 |
// Get media attachment details from the frame state |
| 270 |
var attachment = frame.state().get( 'selection' ).first().toJSON(); |
| 271 |
|
| 272 |
setFileSource( 'browse' ); |
| 273 |
setFile( attachment ); |
| 274 |
|
| 275 |
setNoticeState( null ); |
| 276 |
} } |
| 277 |
onButtonClick={ () => { |
| 278 |
elementorCommon.events.dispatchEvent( { |
| 279 |
event: 'browse file click', |
| 280 |
version: '', |
| 281 |
details: { |
| 282 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 283 |
step: state.currentStep, |
| 284 |
}, |
| 285 |
} ); |
| 286 |
} } |
| 287 |
// TODO: DEAL WITH ERROR |
| 288 |
onError={ ( error ) => { |
| 289 |
if ( 'file_not_allowed' === error.id ) { |
| 290 |
elementorCommon.events.dispatchEvent( { |
| 291 |
event: 'indication prompt', |
| 292 |
version: '', |
| 293 |
details: { |
| 294 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 295 |
step: state.currentStep, |
| 296 |
action_state: 'failure', |
| 297 |
action: 'logo upload format', |
| 298 |
}, |
| 299 |
} ); |
| 300 |
|
| 301 |
setNoticeState( { |
| 302 |
type: 'error', |
| 303 |
icon: 'eicon-warning', |
| 304 |
message: __( 'This file type is not supported. Try a different type of file', 'elementor' ), |
| 305 |
} ); |
| 306 |
} |
| 307 |
} } |
| 308 |
/> |
| 309 |
</> |
| 310 |
} |
| 311 |
{ |
| 312 |
<UnfilteredFilesDialog |
| 313 |
show={ showUnfilteredFilesDialog } |
| 314 |
setShow={ setShowUnfilteredFilesDialog } |
| 315 |
confirmModalText={ __( 'This allows Elementor to scan your SVGs for malicious content. If you do not wish to allow this, use a different image format.', 'elementor' ) } |
| 316 |
errorModalText={ __( 'There was a problem with enabling SVG uploads. Try again, or use another image format.', 'elementor' ) } |
| 317 |
onReady={ () => { |
| 318 |
setShowUnfilteredFilesDialog( false ); |
| 319 |
|
| 320 |
elementorAppConfig.onboarding.isUnfilteredFilesEnabled = true; |
| 321 |
|
| 322 |
uploadSiteLogo( file ); |
| 323 |
} } |
| 324 |
onDismiss={ () => dismissUnfilteredFilesCallback() } |
| 325 |
onCancel={ () => dismissUnfilteredFilesCallback() } |
| 326 |
/> |
| 327 |
} |
| 328 |
</PageContentLayout> |
| 329 |
</Layout> |
| 330 |
); |
| 331 |
} |
| 332 |
|