Skip to content

Commit ad3e054

Browse files
committed
fix: optimizations
1 parent 442d17e commit ad3e054

File tree

10 files changed

+239
-270
lines changed

10 files changed

+239
-270
lines changed

src/components/shared/profileCompletion/ProfileCompletion.jsx

Lines changed: 45 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import Cookies from "js-cookie";
22
import React, { useEffect, useState } from "react";
33
import { IoMdCloseCircleOutline } from "react-icons/io";
44
import { useDispatch, useSelector } from "react-redux";
5-
import { ProfileCompletionDetails } from "../../../constants";
5+
6+
import { ProfileElements } from "../../../constants";
67
import { updateUserData } from "../../../redux/slice/userSlice";
78
import { UpdateUser } from "../../../service/MilanApi";
89
import { showErrorToast, showSuccessToast } from "../../../utils/Toasts";
10+
import getProfileFields from "../../../utils/getProfileFields";
911
import Button from "../buttons/globalbutton/Button";
1012
import "./ProfileCompletion.scss";
1113

@@ -14,96 +16,34 @@ const ProfileCompletion = ({
1416
editProfile,
1517
seteditProfile,
1618
}) => {
17-
const [missingElements, setMissingElements] = useState([]);
18-
const [currentStep, setcurrentStep] = useState(0);
19+
const [currentStep, setcurrentStep] = useState(2);
20+
const [currentIndex, setcurrentIndex] = useState(0);
1921
const [formData, setFormData] = useState({});
2022
const [errors, setErrors] = useState({});
2123
const info = useSelector((state) => state.user);
2224
const dispatch = useDispatch();
2325

2426
useEffect(() => {
25-
calculateMissingElements();
2627
if (editProfile) {
2728
setFormData(info);
2829
}
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+
}, []);
7331

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;
9534

9635
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);
10239
}
10340
};
10441

10542
const handleDecrementStep = () => {
106-
if (currentStep - 2 >= 0) setcurrentStep(currentStep - 2);
43+
if (currentStep - 2 >= 2) {
44+
setcurrentIndex(currentIndex - 2);
45+
setcurrentStep(currentStep - 2);
46+
}
10747
};
10848

10949
const handleChange = (e) => {
@@ -113,24 +53,18 @@ const ProfileCompletion = ({
11353

11454
const handleSubmit = async (e) => {
11555
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);
12765
}
12866
};
12967

130-
const maxSteps = editProfile
131-
? editProfileFields.length
132-
: missingElements.length;
133-
13468
return (
13569
<div className="profilecompletion_overlay">
13670
<div className="profilecompletion_modal">
@@ -154,73 +88,36 @@ const ProfileCompletion = ({
15488
</div>
15589

15690
<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+
})}
213110
</form>
214111

215112
<div className="profilecompletion_btndiv">
216113
<Button
217114
variant="solid"
115+
disabled={currentStep === 2}
218116
onClickfunction={handleDecrementStep}
219-
disabled={currentStep === 0}
220117
>
221118
Previous
222119
</Button>
223-
{currentStep + 2 >= maxSteps ? (
120+
{currentStep == totalfields ? (
224121
<Button variant="solid" onClickfunction={handleSubmit}>
225122
Finish
226123
</Button>

src/constants/ProfileCompletionDetails.js

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)