@@ -2,10 +2,12 @@ import Cookies from "js-cookie";
2
2
import React , { useEffect , useState } from "react" ;
3
3
import { IoMdCloseCircleOutline } from "react-icons/io" ;
4
4
import { useDispatch , useSelector } from "react-redux" ;
5
- import { ProfileCompletionDetails } from "../../../constants" ;
5
+
6
+ import { ProfileElements } from "../../../constants" ;
6
7
import { updateUserData } from "../../../redux/slice/userSlice" ;
7
8
import { UpdateUser } from "../../../service/MilanApi" ;
8
9
import { showErrorToast , showSuccessToast } from "../../../utils/Toasts" ;
10
+ import getProfileFields from "../../../utils/getProfileFields" ;
9
11
import Button from "../buttons/globalbutton/Button" ;
10
12
import "./ProfileCompletion.scss" ;
11
13
@@ -14,96 +16,34 @@ const ProfileCompletion = ({
14
16
editProfile,
15
17
seteditProfile,
16
18
} ) => {
17
- const [ missingElements , setMissingElements ] = useState ( [ ] ) ;
18
- const [ currentStep , setcurrentStep ] = useState ( 0 ) ;
19
+ const [ currentStep , setcurrentStep ] = useState ( 2 ) ;
20
+ const [ currentIndex , setcurrentIndex ] = useState ( 0 ) ;
19
21
const [ formData , setFormData ] = useState ( { } ) ;
20
22
const [ errors , setErrors ] = useState ( { } ) ;
21
23
const info = useSelector ( ( state ) => state . user ) ;
22
24
const dispatch = useDispatch ( ) ;
23
25
24
26
useEffect ( ( ) => {
25
- calculateMissingElements ( ) ;
26
27
if ( editProfile ) {
27
28
setFormData ( info ) ;
28
29
}
29
- } , [ info ] ) ;
30
-
31
- const calculateMissingElements = ( ) => {
32
- const missing = [ ] ;
33
- if ( ! info . tagLine ) missing . push ( "tagLine" ) ;
34
- if ( ! info . description ) missing . push ( "description" ) ;
35
- if ( ! info . city ) missing . push ( "city" ) ;
36
- if ( ! info . state ) missing . push ( "state" ) ;
37
- if ( ! info . address ) missing . push ( "address" ) ;
38
- if ( ! info . country ) missing . push ( "country" ) ;
39
- if ( ! info . pincode ) missing . push ( "pincode" ) ;
40
-
41
- setMissingElements ( missing ) ;
42
- } ;
43
-
44
- const editProfileFields = [
45
- "name" ,
46
- "tagLine" ,
47
- "description" ,
48
- "city" ,
49
- "state" ,
50
- "address" ,
51
- "country" ,
52
- "pincode" ,
53
- ] ;
54
-
55
- const calculateTotalSteps = ( ) => {
56
- if ( editProfile ) {
57
- if ( editProfileFields . length % 2 === 0 ) {
58
- return Math . ceil ( editProfileFields . length / 2 ) ;
59
- } else {
60
- return Math . ceil ( editProfileFields . length / 2 ) + 1 ;
61
- }
62
- } else {
63
- if ( missingElements . length % 2 === 0 ) {
64
- return Math . ceil ( missingElements . length / 2 ) ;
65
- } else {
66
- return Math . ceil ( missingElements . length / 2 ) + 1 ;
67
- }
68
- }
69
- } ;
70
-
71
- const validateFields = ( ) => {
72
- let stepErrors = { } ;
30
+ } , [ ] ) ;
73
31
74
- const currentMissingElements = missingElements . slice (
75
- currentStep ,
76
- currentStep + 2 ,
77
- ) ;
78
-
79
- const formElementstoBeValidated = ProfileCompletionDetails . elements . filter (
80
- ( element ) => currentMissingElements . includes ( element . id ) ,
81
- ) ;
82
-
83
- formElementstoBeValidated . forEach ( ( element ) => {
84
- if (
85
- ! formData [ element . id ] ||
86
- element . minimumLength > formData [ element . id ] ?. length
87
- ) {
88
- stepErrors [ element . id ] = element . errorMessage ;
89
- }
90
- } ) ;
91
-
92
- setErrors ( stepErrors ) ;
93
- return Object . keys ( stepErrors ) . length === 0 ;
94
- } ;
32
+ const fields = getProfileFields ( info , editProfile ) ;
33
+ const totalfields = fields . length ;
95
34
96
35
const handleIncrementStep = ( ) => {
97
- if ( editProfile ) {
98
- if ( currentStep < calculateTotalSteps ( ) ) setcurrentStep ( currentStep + 2 ) ;
99
- } else {
100
- if ( validateFields ( ) && currentStep < calculateTotalSteps ( ) )
101
- setcurrentStep ( currentStep + 2 ) ;
36
+ if ( currentStep + 2 <= totalfields ) {
37
+ setcurrentIndex ( currentIndex + 2 ) ;
38
+ setcurrentStep ( currentStep + 2 ) ;
102
39
}
103
40
} ;
104
41
105
42
const handleDecrementStep = ( ) => {
106
- if ( currentStep - 2 >= 0 ) setcurrentStep ( currentStep - 2 ) ;
43
+ if ( currentStep - 2 >= 2 ) {
44
+ setcurrentIndex ( currentIndex - 2 ) ;
45
+ setcurrentStep ( currentStep - 2 ) ;
46
+ }
107
47
} ;
108
48
109
49
const handleChange = ( e ) => {
@@ -113,24 +53,18 @@ const ProfileCompletion = ({
113
53
114
54
const handleSubmit = async ( e ) => {
115
55
e . preventDefault ( ) ;
116
- if ( validateFields ( ) ) {
117
- const response = await UpdateUser ( formData ) ;
118
- if ( response ?. status !== 200 ) {
119
- showErrorToast ( response ?. data ?. message ) ;
120
- } else {
121
- dispatch ( updateUserData ( formData ) ) ;
122
- setFormData ( { } ) ;
123
- setShowProfileModal ( false ) ;
124
- seteditProfile ( false ) ;
125
- showSuccessToast ( response ?. data ?. message ) ;
126
- }
56
+ const response = await UpdateUser ( formData ) ;
57
+ if ( response ?. status !== 200 ) {
58
+ showErrorToast ( response ?. data ?. message ) ;
59
+ } else {
60
+ dispatch ( updateUserData ( formData ) ) ;
61
+ setFormData ( { } ) ;
62
+ setShowProfileModal ( false ) ;
63
+ seteditProfile ( false ) ;
64
+ showSuccessToast ( response ?. data ?. message ) ;
127
65
}
128
66
} ;
129
67
130
- const maxSteps = editProfile
131
- ? editProfileFields . length
132
- : missingElements . length ;
133
-
134
68
return (
135
69
< div className = "profilecompletion_overlay" >
136
70
< div className = "profilecompletion_modal" >
@@ -154,73 +88,36 @@ const ProfileCompletion = ({
154
88
</ div >
155
89
156
90
< form >
157
- { editProfile
158
- ? editProfileFields
159
- . slice ( currentStep , currentStep + 2 )
160
- . map ( ( elId ) => {
161
- const formElement = ProfileCompletionDetails . elements . find (
162
- ( element ) => element . id === elId ,
163
- ) ;
164
- console . log ( "🚀 ~ .map ~ formElement:" , formElement ) ;
165
-
166
- return (
167
- < div
168
- className = "profilecompletion_element"
169
- key = { formElement . id }
170
- >
171
- < label > { formElement ?. label } </ label >
172
- < input
173
- type = { formElement ?. type }
174
- name = { formElement ?. id }
175
- value = { formData [ formElement ?. id ] || "" }
176
- onChange = { handleChange }
177
- className = "auth_input"
178
- placeholder = { formElement ?. placeholder }
179
- />
180
- { errors [ formElement ?. id ] && (
181
- < p > { errors [ formElement ?. id ] } </ p >
182
- ) }
183
- </ div >
184
- ) ;
185
- } )
186
- : missingElements
187
- . slice ( currentStep , currentStep + 2 )
188
- . map ( ( elId ) => {
189
- const formElement = ProfileCompletionDetails . elements . find (
190
- ( element ) => element . id === elId ,
191
- ) ;
192
-
193
- return (
194
- < div
195
- className = "profilecompletion_element"
196
- key = { formElement . id }
197
- >
198
- < label > { formElement ?. label } </ label >
199
- < input
200
- type = { formElement ?. type }
201
- name = { formElement ?. id }
202
- value = { formData [ formElement ?. id ] || "" }
203
- onChange = { handleChange }
204
- className = "auth_input"
205
- placeholder = { formElement ?. placeholder }
206
- />
207
- { errors [ formElement ?. id ] && (
208
- < p > { errors [ formElement ?. id ] } </ p >
209
- ) }
210
- </ div >
211
- ) ;
212
- } ) }
91
+ { fields . slice ( currentIndex , currentIndex + 2 ) . map ( ( elId ) => {
92
+ const formElement = ProfileElements . find (
93
+ ( element ) => element . id === elId ,
94
+ ) ;
95
+
96
+ return (
97
+ < div className = "profilecompletion_element" key = { formElement . id } >
98
+ < label > { formElement ?. label } </ label >
99
+ < input
100
+ type = { formElement ?. type }
101
+ name = { formElement ?. id }
102
+ value = { formData [ formElement ?. id ] || "" }
103
+ onChange = { handleChange }
104
+ className = "auth_input"
105
+ placeholder = { formElement ?. placeholder }
106
+ />
107
+ </ div >
108
+ ) ;
109
+ } ) }
213
110
</ form >
214
111
215
112
< div className = "profilecompletion_btndiv" >
216
113
< Button
217
114
variant = "solid"
115
+ disabled = { currentStep === 2 }
218
116
onClickfunction = { handleDecrementStep }
219
- disabled = { currentStep === 0 }
220
117
>
221
118
Previous
222
119
</ Button >
223
- { currentStep + 2 >= maxSteps ? (
120
+ { currentStep == totalfields ? (
224
121
< Button variant = "solid" onClickfunction = { handleSubmit } >
225
122
Finish
226
123
</ Button >
0 commit comments