import React, { useState, useEffect } from "react"; import axios from "axios"; import { useNavigate, useParams } from "react-router-dom"; import { isAutheticated } from "src/auth"; import Modal from "react-bootstrap/Modal"; import Button from "react-bootstrap/Button"; import Form from "react-bootstrap/Form"; import swal from "sweetalert"; const EditSalesCoOrdinator = () => { const navigate = useNavigate(); const token = isAutheticated(); const { id } = useParams(); const [formData, setFormData] = useState({ name: "", email: "", countryCode: "", mobileNumber: "", currentPassword: "", newPassword: "", confirmPassword: "", otp: "", }); const [showVerifyModal, setShowVerifyModal] = useState(false); const [showPasswordModal, setShowPasswordModal] = useState(false); useEffect(() => { getSalesCoOrdinatorData(); }, []); const getSalesCoOrdinatorData = async () => { try { const response = await axios.get(`/api/salescoordinator/getOne/${id}`, { headers: { Authorization: `Bearer ${token}`, }, }); const { data } = response.data; // Assuming country code always starts with + and is followed by 1-3 digits const fullMobileNumber = data?.mobileNumber || ""; const countryCodeMatch = fullMobileNumber.match(/^\+\d{1,2}/); const countryCode = countryCodeMatch ? countryCodeMatch[0] : ""; const mobileNumber = fullMobileNumber.replace(countryCode, ""); setFormData({ name: data?.name || "", email: data?.email || "", countryCode: countryCode, mobileNumber: mobileNumber, currentPassword: "", newPassword: "", confirmPassword: "", otp: "", }); } catch (error) { console.error("Error fetching sales coordinator data: ", error); } }; const handleChange = (e) => { setFormData({ ...formData, [e.target.name]: e.target.value }); }; const handleUpdateBasicInfo = () => { axios .patch( `/api/salescoordinator/profile/update/${id}`, { name: formData.name, email: formData.email, }, { headers: { Authorization: `Bearer ${token}`, }, } ) .then((response) => { swal({ title: "Success", text: "Sales coordinator information updated successfully!", icon: "success", button: "OK", }); }) .catch((error) => { let msg = error?.response?.data?.message || "Something went wrong!"; swal({ title: "Warning", text: msg, icon: "error", button: "Retry", dangerMode: true, }); }); }; const handleVerifyMobile = () => { axios .post( `/api/salescoordinator/update-mobile-number/${id}`, { newCountryCode: formData.countryCode, newMobileNumber: formData.mobileNumber, }, { headers: { Authorization: `Bearer ${token}`, }, } ) .then(() => { setShowVerifyModal(true); }) .catch((error) => { let msg = error?.response?.data?.message || "Something went wrong!"; swal({ title: "Warning", text: msg, icon: "error", button: "Retry", dangerMode: true, }); }); }; const handleVerifyOTP = () => { axios .post( "/api/salescoordinator/verify-updated-mobile-otp", { otp: formData.otp, newMobileNumber: `${formData.countryCode}${formData.mobileNumber}`, }, { headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, } ) .then(() => { setShowVerifyModal(false); // Handle success, if needed }) .catch((error) => { let msg = error?.response?.data?.message || "Something went wrong!"; swal({ title: "Warning", text: msg, icon: "error", button: "Retry", dangerMode: true, }); }); }; const handleChangePassword = () => { axios .put( `/api/salescoordinator/password/update/${id}`, { oldPassword: formData.currentPassword, newPassword: formData.newPassword, confirmPassword: formData.confirmPassword, }, { headers: { Authorization: `Bearer ${token}`, }, } ) .then(() => { setShowPasswordModal(false); swal({ title: "Success", text: "Password updated successfully!", icon: "success", button: "OK", }); }) .catch((error) => { let msg = error?.response?.data?.message || "Something went wrong!"; swal({ title: "Warning", text: msg, icon: "error", button: "Retry", dangerMode: true, }); }); }; const onCancel = () => { navigate("/salescoordinators"); }; return (

Edit Sales Coordinator

{/*
*/}
{/* OTP Verification Modal */} setShowVerifyModal(false)} > Verify Mobile Enter OTP {/* Change Password Modal */} setShowPasswordModal(false)} > Change Password Current Password New Password Confirm New Password
); }; export default EditSalesCoOrdinator;