1
- import { dispatch , select } from '@wordpress/data' ;
1
+ import { dispatch , useSelect } from '@wordpress/data' ;
2
2
import { uploadMedia , validateMimeType , validateFileSize } from '@wordpress/media-utils' ;
3
3
import classNames from 'classnames' ;
4
4
import { ImageImport , Label } from '@newfold/ui-component-library' ;
5
5
import { nfdOnboardingStore } from '@/data/store' ;
6
6
import { OnboardingEvent , trackOnboardingEvent } from '@/utils/analytics/hiive' ;
7
7
import { ACTION_LOGO_UPLOAD_FAILED } from '@/utils/analytics/hiive/constants' ;
8
8
9
- const LogoUploadInput = ( { logoValue, setLogoValue } ) => {
10
- const [ isUploading , setIsUploading ] = useState ( false ) ;
9
+ const LogoUploadInput = ( { isUploading, setIsUploading } ) => {
11
10
const [ error , setError ] = useState ( {
12
11
status : false ,
13
12
message : '' ,
14
13
} ) ;
15
14
15
+ const logo = useSelect ( ( select ) => select ( nfdOnboardingStore ) . getLogo ( ) , [ ] ) ;
16
+
16
17
const validateFile = ( file ) => {
17
18
const validationResult = {
18
19
success : true ,
@@ -60,24 +61,20 @@ const LogoUploadInput = ( { logoValue, setLogoValue } ) => {
60
61
* Since we're only interested in the final uploaded object...
61
62
* The isOptimisticUrl flag is used to track the first call and ignore the blob src.
62
63
*/
63
- let isOptimisticUrl = false ;
64
+ let isOptimisticUrl = true ;
64
65
uploadMedia ( {
65
66
multiple : false ,
66
67
filesList : [ file ] ,
67
68
onFileChange : ( files ) => {
68
- if ( ! isOptimisticUrl ) {
69
- return isOptimisticUrl = true ;
69
+ if ( isOptimisticUrl ) {
70
+ isOptimisticUrl = false ;
71
+ return ;
70
72
}
71
73
// Set the logo in the input slice
72
74
dispatch ( nfdOnboardingStore ) . setLogo ( {
73
75
id : files [ 0 ] . id ,
74
76
url : files [ 0 ] . url ,
75
77
} ) ;
76
- // Set the logo in the step state
77
- setLogoValue ( {
78
- id : files [ 0 ] . id ,
79
- url : files [ 0 ] . url ,
80
- } ) ;
81
78
// Reset the uploading state
82
79
setIsUploading ( false ) ;
83
80
} ,
@@ -103,52 +100,25 @@ const LogoUploadInput = ( { logoValue, setLogoValue } ) => {
103
100
id : null ,
104
101
url : null ,
105
102
} ) ;
106
- // Reset the logo in the step state
107
- setLogoValue ( {
108
- id : 0 ,
109
- url : '' ,
110
- } ) ;
111
103
} ;
112
104
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
-
138
105
return (
139
106
< div className = "nfd-onboarding-logo-upload nfd-flex nfd-flex-col nfd-gap-2" >
140
107
< Label htmlFor = "nfd-onboarding-logo-input" >
141
108
{ __ ( 'Site logo' , 'wp-module-onboarding' ) }
142
109
</ Label >
143
110
< ImageImport
111
+ key = { logo ?. url || 'no-logo' }
144
112
id = "nfd-onboarding-logo-input"
145
113
name = "nfd-onboarding-logo-input"
146
114
imageInputVariant = "rounded"
147
- previewImage = { getCurrentSiteLogo ( ) }
115
+ previewImage = { logo ?. url || null }
148
116
dropLabel = { __ ( 'accepted: .png, .jpg, .jpeg (5MB max)' , 'wp-module-onboarding' ) }
149
117
buttonText = { __ ( 'Browse' , 'wp-module-onboarding' ) }
150
118
onChange = { ( { file } ) => handleUpload ( file ) }
151
- onDrop = { handleUpload }
119
+ onDrop = { ( e ) => {
120
+ handleUpload ( e . dataTransfer . files [ 0 ] ) ;
121
+ } }
152
122
onReset = { handleReset }
153
123
disabled = { isUploading }
154
124
isLoading = { isUploading }
0 commit comments