Skip to content

Commit

Permalink
fix: edit profile flow
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalCodes committed Dec 16, 2024
1 parent a80202d commit 6a0056e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 45 deletions.
63 changes: 20 additions & 43 deletions src/components/shared/profileUpdate/ProfileUpdate.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-unused-vars */
import { STATUSCODE } from "@/static/Constants";
import { completeProfileApiCall } from "@service/MilanApi";
import { updateUserProfile } from "@service/MilanApi";
import { showSuccessToast } from "@utils/Toasts";
import clsx from "clsx";
import { useState } from "react";
Expand Down Expand Up @@ -29,8 +29,8 @@ const ProfileUpdate = ({ setOpenModal, refreshProfileData, profileData }) => {
const handleChange = (field) => (event) => {
const updatedCredentials = { ...credentials };

if (field === "description") {
updatedCredentials.description = event.target.value;
if (field === "description" || field === "name") {
updatedCredentials[field] = event.target.value;
} else {
// For address fields, update the address object inside the credentials
updatedCredentials.address[field] = event.target.value;
Expand Down Expand Up @@ -66,16 +66,13 @@ const ProfileUpdate = ({ setOpenModal, refreshProfileData, profileData }) => {
});
};

const validateForm = async (updatedCredentials) => {
const validateForm = async () => {
const newErrors = {};

// Check required fields for top-level fields
const requiredFields = ["description"];
const requiredFields = ["description", "name"];
requiredFields.forEach((field) => {
if (
!updatedCredentials[field] ||
updatedCredentials[field].trim() === ""
) {
if (!credentials[field] || credentials[field].trim() === "") {
newErrors[field] = `${field} is required.`;
}
});
Expand All @@ -91,62 +88,43 @@ const ProfileUpdate = ({ setOpenModal, refreshProfileData, profileData }) => {
];
addressFields.forEach((field) => {
if (
!updatedCredentials.address[field] ||
updatedCredentials.address[field].trim() === ""
!credentials.address[field] ||
credentials.address[field].trim() === ""
) {
newErrors[`address.${field}`] = `${field} is required.`;
}
});

// Description length validation
if (
updatedCredentials.description &&
updatedCredentials.description.length > 500
) {
if (credentials.description && credentials.description.length > 500) {
newErrors.description = "Description cannot be more than 500 characters.";
}

if (
updatedCredentials.description &&
updatedCredentials.description.length < 100
) {
if (credentials.description && credentials.description.length < 100) {
newErrors.description = "Description cannot be less than 100 characters.";
}

// Pincode validation
if (
updatedCredentials.address.pincode &&
isNaN(updatedCredentials.address.pincode)
) {
if (credentials.address.pincode && isNaN(credentials.address.pincode)) {
newErrors["address.pincode"] = "Pincode must be a valid number.";
}

setErrors(newErrors);

const data = await completeProfileApiCall({
credentials: {
...updatedCredentials,
config: {
hasCompletedProfile: true,
},
},
const data = await updateUserProfile({
credentials,
});

if (data.status === STATUSCODE.OK) {
showSuccessToast(data?.data?.message);
refreshProfileData();
setOpenModal(false);
return;
}

return Object.keys(newErrors).length === 0;
};

const clearError = (field) => {
setErrors((prevErrors) => {
const { [field]: ignored, ...rest } = prevErrors;
return rest;
});
};

return (
<div className="profileupdate_overlay">
<div className="profileupdate_modal">
Expand All @@ -161,6 +139,10 @@ const ProfileUpdate = ({ setOpenModal, refreshProfileData, profileData }) => {
<h1> Edit profile </h1>
<Button
type="submit"
onClickfunction={(e) => {
e.preventDefault();
validateForm();
}}
disabled={
!credentials?.description ||
!credentials?.address?.line1 ||
Expand All @@ -176,12 +158,7 @@ const ProfileUpdate = ({ setOpenModal, refreshProfileData, profileData }) => {
</div>
</div>

<form
onSubmit={(e) => {
e.preventDefault();
validateForm(credentials);
}}
>
<form>
<div className="profileupdate_element">
<div className="dropzone_container">
<p className="dropzone_coverlabel">Cover Image</p>
Expand Down
3 changes: 2 additions & 1 deletion src/integrations/ApiEndpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const userEndpoints = {
profile: `${API}/user/profile`,
update: `${API}/user/update/profile`,
report: `${API}/user/report`,
completeProfile: `${API}/user/completeprofile`,
completeProfile: `${API}/user/complete`,
updateProfile: `${API}/user/update`,
};

const clubEndpoints = {
Expand Down
19 changes: 18 additions & 1 deletion src/service/MilanApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const ReportProblem = async (credentials) => {
}
};

// UPDATE USER
// Complete User Profile
export const completeProfileApiCall = async ({ credentials }) => {
try {
const response = await Axios.patch(
Expand All @@ -78,6 +78,23 @@ export const completeProfileApiCall = async ({ credentials }) => {
}
};

// Update User Profile
export const updateUserProfile = async ({ credentials }) => {
try {
const response = await Axios.patch(
userEndpoints.updateProfile,
credentials,
{
withCredentials: true,
},
);

return response;
} catch (error) {
return error.response;
}
};

// Google Auth screen
export const GoogleAuth = async () => {
try {
Expand Down

0 comments on commit 6a0056e

Please sign in to comment.