Skip to content

Commit 89ae851

Browse files
committed
Fix dropped logo not updating preview
1 parent 577579d commit 89ae851

File tree

2 files changed

+30
-52
lines changed

2 files changed

+30
-52
lines changed

src/app/steps/Logo/LogoStep.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@ import { Navigate, Step } from '@/components';
55
import { OnboardingEvent, trackOnboardingEvent } from '@/utils/analytics/hiive';
66
import { ACTION_LOGO_ADDED, ACTION_LOGO_SKIPPED } from '@/utils/analytics/hiive/constants';
77
import { LogoUploadInput } from '.';
8+
import { nfdOnboardingStore } from '@/data/store';
89

910
const LogoStep = () => {
10-
const [ logoValue, setLogoValue ] = useState( {
11-
id: null,
12-
url: null,
13-
} );
11+
const [ isUploading, setIsUploading ] = useState( false );
1412

1513
const { editEntityRecord, saveEditedEntityRecord } = useDispatch( coreStore );
16-
const { siteLogo } = useSelect( ( select ) => {
14+
const { siteLogoId, storeLogoId } = useSelect( ( select ) => {
15+
// Get the site logo from the core data store.
1716
const { getEntityRecord } = select( coreStore );
1817
const siteSettings = getEntityRecord( 'root', 'site' );
1918
const logoId = siteSettings?.site_logo;
19+
20+
// Get the logo from the input slice.
21+
const logo = select( nfdOnboardingStore ).getLogo();
2022
return {
21-
siteLogo: logoId,
23+
siteLogoId: logoId,
24+
storeLogoId: logo.id,
2225
};
2326
}, [] );
2427

2528
const handleNext = () => {
2629
// If the logo is set.
27-
if ( null !== logoValue.id && logoValue.id !== siteLogo ) {
30+
if ( null !== storeLogoId && storeLogoId !== siteLogoId ) {
2831
editEntityRecord( 'root', 'site', undefined, {
29-
site_logo: logoValue.id,
32+
site_logo: storeLogoId,
3033
} );
3134
saveEditedEntityRecord( 'root', 'site' );
3235

@@ -51,21 +54,26 @@ const LogoStep = () => {
5154
className="nfd-gap-2"
5255
/>
5356
<Container.Block separator={ false }>
54-
<LogoUploadInput logo={ logoValue } setLogoValue={ setLogoValue } />
57+
<LogoUploadInput
58+
isUploading={ isUploading }
59+
setIsUploading={ setIsUploading }
60+
/>
5561
</Container.Block>
5662
<Container.Footer>
5763
<Step.Actions>
5864
<Navigate
5965
toRoute="/generating"
6066
direction="forward"
6167
callback={ handleNext }
68+
disabled={ isUploading }
6269
>
6370
{ __( 'Next', 'wp-module-onboarding' ) }
6471
</Navigate>
6572
<Navigate
6673
toRoute="/intake"
6774
direction="backward"
6875
variant="secondary"
76+
disabled={ isUploading }
6977
>
7078
{ __( 'Back', 'wp-module-onboarding' ) }
7179
</Navigate>

src/app/steps/Logo/LogoUploadInput.js

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import { dispatch, select } from '@wordpress/data';
1+
import { dispatch, useSelect } from '@wordpress/data';
22
import { uploadMedia, validateMimeType, validateFileSize } from '@wordpress/media-utils';
33
import classNames from 'classnames';
44
import { ImageImport, Label } from '@newfold/ui-component-library';
55
import { nfdOnboardingStore } from '@/data/store';
66
import { OnboardingEvent, trackOnboardingEvent } from '@/utils/analytics/hiive';
77
import { ACTION_LOGO_UPLOAD_FAILED } from '@/utils/analytics/hiive/constants';
88

9-
const LogoUploadInput = ( { logoValue, setLogoValue } ) => {
10-
const [ isUploading, setIsUploading ] = useState( false );
9+
const LogoUploadInput = ( { isUploading, setIsUploading } ) => {
1110
const [ error, setError ] = useState( {
1211
status: false,
1312
message: '',
1413
} );
1514

15+
const logo = useSelect( ( select ) => select( nfdOnboardingStore ).getLogo(), [] );
16+
1617
const validateFile = ( file ) => {
1718
const validationResult = {
1819
success: true,
@@ -60,24 +61,20 @@ const LogoUploadInput = ( { logoValue, setLogoValue } ) => {
6061
* Since we're only interested in the final uploaded object...
6162
* The isOptimisticUrl flag is used to track the first call and ignore the blob src.
6263
*/
63-
let isOptimisticUrl = false;
64+
let isOptimisticUrl = true;
6465
uploadMedia( {
6566
multiple: false,
6667
filesList: [ file ],
6768
onFileChange: ( files ) => {
68-
if ( ! isOptimisticUrl ) {
69-
return isOptimisticUrl = true;
69+
if ( isOptimisticUrl ) {
70+
isOptimisticUrl = false;
71+
return;
7072
}
7173
// Set the logo in the input slice
7274
dispatch( nfdOnboardingStore ).setLogo( {
7375
id: files[ 0 ].id,
7476
url: files[ 0 ].url,
7577
} );
76-
// Set the logo in the step state
77-
setLogoValue( {
78-
id: files[ 0 ].id,
79-
url: files[ 0 ].url,
80-
} );
8178
// Reset the uploading state
8279
setIsUploading( false );
8380
},
@@ -103,52 +100,25 @@ const LogoUploadInput = ( { logoValue, setLogoValue } ) => {
103100
id: null,
104101
url: null,
105102
} );
106-
// Reset the logo in the step state
107-
setLogoValue( {
108-
id: 0,
109-
url: '',
110-
} );
111103
};
112104

113-
/**
114-
* Get the current site logo from the input slice
115-
* @return {object|null} The current site logo object or null if no logo is set
116-
*/
117-
const getCurrentSiteLogo = () => {
118-
const { logo } = select( nfdOnboardingStore ).getInputSlice();
119-
return logo?.url || null;
120-
};
121-
122-
/**
123-
* On mount, set the parent logo state to the input slice.
124-
* Helpful in cases where the user navigate to another step then return to the Logo step or...
125-
* Refresh the app.
126-
*/
127-
useEffect( () => {
128-
const { logo } = select( nfdOnboardingStore ).getInputSlice();
129-
if ( logo?.url && logo?.id !== logoValue?.id ) {
130-
setLogoValue( {
131-
id: logo.id,
132-
url: logo.url,
133-
} );
134-
}
135-
// eslint-disable-next-line react-hooks/exhaustive-deps
136-
}, [] );
137-
138105
return (
139106
<div className="nfd-onboarding-logo-upload nfd-flex nfd-flex-col nfd-gap-2">
140107
<Label htmlFor="nfd-onboarding-logo-input">
141108
{ __( 'Site logo', 'wp-module-onboarding' ) }
142109
</Label>
143110
<ImageImport
111+
key={ logo?.url || 'no-logo' }
144112
id="nfd-onboarding-logo-input"
145113
name="nfd-onboarding-logo-input"
146114
imageInputVariant="rounded"
147-
previewImage={ getCurrentSiteLogo() }
115+
previewImage={ logo?.url || null }
148116
dropLabel={ __( 'accepted: .png, .jpg, .jpeg (5MB max)', 'wp-module-onboarding' ) }
149117
buttonText={ __( 'Browse', 'wp-module-onboarding' ) }
150118
onChange={ ( { file } ) => handleUpload( file ) }
151-
onDrop={ handleUpload }
119+
onDrop={ ( e ) => {
120+
handleUpload( e.dataTransfer.files[ 0 ] );
121+
} }
152122
onReset={ handleReset }
153123
disabled={ isUploading }
154124
isLoading={ isUploading }

0 commit comments

Comments
 (0)