FeaturesControl.js
1 year ago
ImportOptionsControl.js
1 year ago
LogoControl.js
1 year ago
PaletteControl.js
1 year ago
SiteNameControl.js
1 year ago
TypographyControl.js
2 months ago
LogoControl.js
144 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { compose } from '@wordpress/compose'; |
| 3 | import { withSelect, withDispatch } from '@wordpress/data'; |
| 4 | import { Button } from '@wordpress/components'; |
| 5 | import { MediaUpload } from '@wordpress/media-utils'; |
| 6 | import { addFilter } from '@wordpress/hooks'; |
| 7 | import classnames from 'classnames'; |
| 8 | import { useState } from '@wordpress/element'; |
| 9 | |
| 10 | const LogoControl = ( { userCustomSettings, handleLogoChange } ) => { |
| 11 | const { siteLogo } = userCustomSettings; |
| 12 | const [ logo, setLogo ] = useState( siteLogo?.url || '' ); |
| 13 | |
| 14 | const replaceMediaUpload = () => MediaUpload; |
| 15 | |
| 16 | addFilter( |
| 17 | 'editor.MediaUpload', |
| 18 | 'tpc/onboarding/replace-media-upload', |
| 19 | replaceMediaUpload() |
| 20 | ); |
| 21 | |
| 22 | return ( |
| 23 | <div className="ob-ctrl"> |
| 24 | <div className="ob-ctrl-head small-gap"> |
| 25 | <h3> |
| 26 | { __( 'Upload a logo', 'templates-patterns-collection' ) } |
| 27 | </h3> |
| 28 | </div> |
| 29 | <div className="ob-ctrl-wrap media"> |
| 30 | <MediaUpload |
| 31 | onSelect={ ( newLogo ) => { |
| 32 | handleLogoChange( newLogo ); |
| 33 | setLogo( newLogo.url ); |
| 34 | } } |
| 35 | allowedTypes={ [ 'image' ] } |
| 36 | value={ siteLogo?.id || '' } |
| 37 | render={ ( { open } ) => ( |
| 38 | <> |
| 39 | <button |
| 40 | className={ classnames( |
| 41 | 'ob-media', |
| 42 | logo ? 'has-logo' : '' |
| 43 | ) } |
| 44 | onClick={ open } |
| 45 | > |
| 46 | { ! logo && |
| 47 | __( |
| 48 | 'Select or upload image', |
| 49 | 'templates-patterns-collection' |
| 50 | ) } |
| 51 | { logo && ( |
| 52 | <span className="ob-responsive-wrapper"> |
| 53 | <span |
| 54 | style={ { paddingBottom: '150px' } } |
| 55 | ></span> |
| 56 | <img |
| 57 | src={ logo } |
| 58 | alt={ __( |
| 59 | 'Uploaded image', |
| 60 | 'templates-patterns-collection' |
| 61 | ) } |
| 62 | /> |
| 63 | </span> |
| 64 | ) } |
| 65 | </button> |
| 66 | { logo && ( |
| 67 | <div className="ob-media-actions"> |
| 68 | <Button |
| 69 | isTertiary |
| 70 | onClick={ () => { |
| 71 | setLogo( '' ); |
| 72 | handleLogoChange( null ); |
| 73 | } } |
| 74 | > |
| 75 | { __( |
| 76 | 'Remove', |
| 77 | 'templates-patterns-collection' |
| 78 | ) } |
| 79 | </Button> |
| 80 | <Button isTertiary onClick={ open }> |
| 81 | { __( |
| 82 | 'Change', |
| 83 | 'templates-patterns-collection' |
| 84 | ) } |
| 85 | </Button> |
| 86 | </div> |
| 87 | ) } |
| 88 | </> |
| 89 | ) } |
| 90 | /> |
| 91 | </div> |
| 92 | </div> |
| 93 | ); |
| 94 | }; |
| 95 | |
| 96 | export default compose( |
| 97 | withSelect( ( select ) => { |
| 98 | const { getUserCustomSettings, getImportData } = |
| 99 | select( 'ti-onboarding' ); |
| 100 | return { |
| 101 | userCustomSettings: getUserCustomSettings(), |
| 102 | importData: getImportData(), |
| 103 | }; |
| 104 | } ), |
| 105 | withDispatch( |
| 106 | ( dispatch, { importData, userCustomSettings, importDataDefault } ) => { |
| 107 | const { setUserCustomSettings, setImportData, setRefresh } = |
| 108 | dispatch( 'ti-onboarding' ); |
| 109 | |
| 110 | return { |
| 111 | handleLogoChange: ( newLogo ) => { |
| 112 | const updatedSettings = { |
| 113 | ...userCustomSettings, |
| 114 | siteLogo: newLogo, |
| 115 | }; |
| 116 | setUserCustomSettings( updatedSettings ); |
| 117 | |
| 118 | const newImportData = { |
| 119 | ...importData, |
| 120 | theme_mods: { |
| 121 | ...importData.theme_mods, |
| 122 | custom_logo: newLogo |
| 123 | ? newLogo.id |
| 124 | : importDataDefault.theme_mods.custom_logo, |
| 125 | logo_logo: newLogo |
| 126 | ? JSON.stringify( { |
| 127 | dark: newLogo.id, |
| 128 | light: newLogo.id, |
| 129 | same: true, |
| 130 | } ) |
| 131 | : JSON.stringify( { |
| 132 | ...importDataDefault.theme_mods |
| 133 | .logo_logo, |
| 134 | } ), |
| 135 | }, |
| 136 | }; |
| 137 | setImportData( newImportData ); |
| 138 | setRefresh( true ); |
| 139 | }, |
| 140 | }; |
| 141 | } |
| 142 | ) |
| 143 | )( LogoControl ); |
| 144 |