import React, { useState, useEffect } from "react";
import PropTypes from "prop-types";
import BackdropComponent from "../../MUI/BackdropComponent";
import {
EditProfileContainer,
ProfileImageContainer,
InputFieldLabel,
InputField,
BackButton,
CloseButton,
SaveButton,
ProfileHeader,
BasicInfo,
DetailsInfo,
ButtonsContainer,
ErrorMessage,
ProfileImagePicker,
InputFieldLabelLocation,
} from "./EditProfile.styled";
import selectedTheme from "../../../themes";
import { useFormik } from "formik";
import { ReactComponent as ArrowBack } from "../../../assets/images/svg/arrow-back.svg";
import { ReactComponent as CloseIcon } from "../../../assets/images/svg/close-modal.svg";
import { useTranslation } from "react-i18next";
import {
editMineProfile,
fetchMineProfile,
} from "../../../store/actions/profile/profileActions";
import { useDispatch, useSelector } from "react-redux";
import { selectUserId } from "../../../store/selectors/loginSelectors";
import editProfileValidation from "../../../validations/editProfileValidation";
import useIsMobile from "../../../hooks/useIsMobile";
import AutoSuggestTextField from "../../TextFields/AutoSuggestTextField/AutoSuggestTextField";
import { selectLocations } from "../../../store/selectors/locationsSelectors";
const EditProfile = (props) => {
const [profileImage, setProfileImage] = useState(props.profile.image);
const [showBasic, setShowBasic] = useState(true);
const [showDetails, setShowDetails] = useState(true);
const { t } = useTranslation();
const dispatch = useDispatch();
const { isMobile } = useIsMobile();
const userId = useSelector(selectUserId);
const locations = useSelector(selectLocations);
useEffect(() => {
if (isMobile) {
setShowDetails(false);
} else {
setShowDetails(true);
}
}, [isMobile]);
const handleApiResponseSuccess = () => {
dispatch(fetchMineProfile(userId));
props.reFetchProfile();
};
const handleSubmit = (values) => {
console.log(values);
dispatch(editMineProfile({ ...values, handleApiResponseSuccess }));
props.closeModalHandler();
};
const formik = useFormik({
initialValues: {
firmName: `${props?.profile?.company?.name}`,
firmPIB: `${props?.profile?.company?.PIB}`,
firmLocation: `${props?.profile?.company?.contacts?.location ?? ""}`,
firmWebsite: `${props?.profile?.company?.contacts?.web ?? ""}`,
firmApplink: "",
firmPhone: `${props?.profile?.company?.contacts?.telephone ?? ""}`,
firmLogo: profileImage,
},
validationSchema: editProfileValidation,
onSubmit: handleSubmit,
validateOnBlur: true,
enableReinitialize: true,
});
const closeEditModalHandler = () => {
props.closeModalHandler();
};
const showDetailsHandler = () => {
setShowDetails(!showDetails);
setShowBasic(!showBasic);
};
const setImage = (image) => {
setProfileImage(image);
};
return (
<>
{!showBasic && (
)}
{props.profile.company.name}
{showBasic && (
({ name: item.city }))}
value={formik.values.firmLocation}
onChange={(event, { newValue }) =>
formik.setFieldValue("firmLocation", newValue)
}
/>
{/* */}
)}
{showDetails && (
{
formik.setFieldValue("firmPhone", event.target.value);
}}
error={formik.touched.firmPhone && formik.errors.firmPhone}
margin="normal"
fullWidth
onInput={(e) => {
e.target.value =
e.target.value[0] === "0" && e.target.value.length > 1
? "0" +
String(
Math.max(0, parseInt(e.target.value))
.toString()
.slice(0, 14)
)
: Math.max(0, parseInt(e.target.value))
.toString()
.slice(0, 14);
}}
/>
)}
{formik.errors.firmName && formik.touched.firmName ? (
{formik.errors.firmName}
) : formik.errors.firmPIB && formik.touched.firmPIB ? (
{formik.errors.firmPIB}
) : formik.errors.firmLocation && formik.touched.firmLocation ? (
{formik.errors.firmLocation}
) : formik.errors.firmPhone && formik.touched.firmPhone ? (
{formik.errors.firmPhone}
) : (
<>>
)}
{!isMobile ? (
{t("editProfile.saveChanges")}
) : (
{showDetails
? t("editProfile.showBasic")
: t("editProfile.showDetails")}
{t("common.save")}
)}
>
);
};
EditProfile.propTypes = {
children: PropTypes.node,
profile: PropTypes.any,
closeModalHandler: PropTypes.func,
setImage: PropTypes.func,
reFetchProfile: PropTypes.func,
};
export default EditProfile;