| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- 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 (
- <>
- <BackdropComponent
- handleClose={closeEditModalHandler}
- isLoading
- position="fixed"
- />
- <EditProfileContainer component="form" onSubmit={formik.handleSubmit}>
- {!showBasic && (
- <BackButton onClick={showDetailsHandler}>
- <ArrowBack />
- </BackButton>
- )}
- <ProfileImageContainer>
- <ProfileImagePicker
- image={profileImage}
- setImage={setImage}
- ></ProfileImagePicker>
- <ProfileHeader>{props.profile.company.name}</ProfileHeader>
- </ProfileImageContainer>
- <CloseButton onClick={closeEditModalHandler}>
- <CloseIcon />
- </CloseButton>
- {showBasic && (
- <BasicInfo>
- <InputFieldLabel leftText={t("common.labelFirm").toUpperCase()} />
- <InputField
- name="firmName"
- value={formik.values.firmName}
- onChange={formik.handleChange}
- error={formik.touched.firmName && formik.errors.firmName}
- margin="normal"
- fullWidth
- />
- <InputFieldLabel leftText={t("common.labelPIB")} />
- <InputField
- name="firmPIB"
- type="number"
- value={formik.values.firmPIB}
- onChange={formik.handleChange}
- error={formik.touched.firmPIB && formik.errors.firmPIB}
- margin="normal"
- fullWidth
- disabled
- />
- <InputFieldLabelLocation
- leftText={t("common.labelLocation").toUpperCase()}
- />
- <AutoSuggestTextField
- editLocation
- data={locations.map((item) => ({ name: item.city }))}
- value={formik.values.firmLocation}
- onChange={(event, { newValue }) =>
- formik.setFieldValue("firmLocation", newValue)
- }
- />
- {/* <InputField
- name="firmLocation"
- value={formik.values.firmLocation}
- onChange={formik.handleChange}
- error={formik.touched.firmLocation && formik.errors.firmLocation}
- margin="normal"
- fullWidth
- /> */}
- </BasicInfo>
- )}
- {showDetails && (
- <DetailsInfo>
- <InputFieldLabel
- leftText={t("editProfile.website").toUpperCase()}
- labelWebsite
- />
- <InputField
- name="firmWebsite"
- value={formik.values.firmWebsite}
- onChange={formik.handleChange}
- margin="normal"
- fullWidth
- />
- <InputFieldLabel
- leftText={t("editProfile.applink").toUpperCase()}
- />
- <InputField
- name="firmApplink"
- values={formik.values.firmApplink}
- margin="normal"
- fullWidth
- />
- <InputFieldLabel
- leftText={t("editProfile.phoneNumber").toUpperCase()}
- />
- <InputField
- type="number"
- name="firmPhone"
- value={formik.values.firmPhone}
- onChange={(event) => {
- 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);
- }}
- />
- </DetailsInfo>
- )}
-
- {formik.errors.firmName && formik.touched.firmName ? (
- <ErrorMessage>{formik.errors.firmName}</ErrorMessage>
- ) : formik.errors.firmPIB && formik.touched.firmPIB ? (
- <ErrorMessage>{formik.errors.firmPIB}</ErrorMessage>
- ) : formik.errors.firmLocation && formik.touched.firmLocation ? (
- <ErrorMessage>{formik.errors.firmLocation}</ErrorMessage>
- ) : formik.errors.firmPhone && formik.touched.firmPhone ? (
- <ErrorMessage>{formik.errors.firmPhone}</ErrorMessage>
- ) : (
- <></>
- )}
-
- {!isMobile ? (
- <ButtonsContainer>
- <SaveButton
- type="submit"
- variant="contained"
- height="48px"
- width="335px"
- buttoncolor={selectedTheme.primaryPurple}
- textcolor="white"
- >
- {t("editProfile.saveChanges")}
- </SaveButton>
- </ButtonsContainer>
- ) : (
- <ButtonsContainer>
- <SaveButton
- height="44px"
- width="155px"
- buttoncolor={selectedTheme.primaryPurple}
- textcolor={selectedTheme.primaryPurple}
- onClick={showDetailsHandler}
- >
- {showDetails
- ? t("editProfile.showBasic")
- : t("editProfile.showDetails")}
- </SaveButton>
- <SaveButton
- type="submit"
- variant="contained"
- height="44px"
- width="155px"
- buttoncolor={selectedTheme.primaryPurple}
- textcolor="white"
- >
- {t("common.save")}
- </SaveButton>
- </ButtonsContainer>
- )}
- </EditProfileContainer>
- </>
- );
- };
-
- EditProfile.propTypes = {
- children: PropTypes.node,
- profile: PropTypes.any,
- closeModalHandler: PropTypes.func,
- setImage: PropTypes.func,
- reFetchProfile: PropTypes.func,
- };
-
- export default EditProfile;
|