You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditProfile.js 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import React, { useState, useEffect } from "react";
  2. import PropTypes from "prop-types";
  3. import BackdropComponent from "../../MUI/BackdropComponent";
  4. import {
  5. EditProfileContainer,
  6. ProfileImageContainer,
  7. InputFieldLabel,
  8. InputField,
  9. BackButton,
  10. CloseButton,
  11. SaveButton,
  12. ProfileHeader,
  13. BasicInfo,
  14. DetailsInfo,
  15. ButtonsContainer,
  16. ErrorMessage,
  17. ProfileImagePicker,
  18. InputFieldLabelLocation,
  19. } from "./EditProfile.styled";
  20. import selectedTheme from "../../../themes";
  21. import { useFormik } from "formik";
  22. import { ReactComponent as ArrowBack } from "../../../assets/images/svg/arrow-back.svg";
  23. import { ReactComponent as CloseIcon } from "../../../assets/images/svg/close-modal.svg";
  24. import { useTranslation } from "react-i18next";
  25. import {
  26. editMineProfile,
  27. fetchMineProfile,
  28. } from "../../../store/actions/profile/profileActions";
  29. import { useDispatch, useSelector } from "react-redux";
  30. import { selectUserId } from "../../../store/selectors/loginSelectors";
  31. import editProfileValidation from "../../../validations/editProfileValidation";
  32. import useIsMobile from "../../../hooks/useIsMobile";
  33. import AutoSuggestTextField from "../../TextFields/AutoSuggestTextField/AutoSuggestTextField";
  34. import { selectLocations } from "../../../store/selectors/locationsSelectors";
  35. const EditProfile = (props) => {
  36. const [profileImage, setProfileImage] = useState(props.profile.image);
  37. const [showBasic, setShowBasic] = useState(true);
  38. const [showDetails, setShowDetails] = useState(true);
  39. const { t } = useTranslation();
  40. const dispatch = useDispatch();
  41. const { isMobile } = useIsMobile();
  42. const userId = useSelector(selectUserId);
  43. const locations = useSelector(selectLocations);
  44. useEffect(() => {
  45. if (isMobile) {
  46. setShowDetails(false);
  47. } else {
  48. setShowDetails(true);
  49. }
  50. }, [isMobile]);
  51. const handleApiResponseSuccess = () => {
  52. dispatch(fetchMineProfile(userId));
  53. props.reFetchProfile();
  54. };
  55. const handleSubmit = (values) => {
  56. console.log(values);
  57. dispatch(editMineProfile({ ...values, handleApiResponseSuccess }));
  58. props.closeModalHandler();
  59. };
  60. const formik = useFormik({
  61. initialValues: {
  62. firmName: `${props?.profile?.company?.name}`,
  63. firmPIB: `${props?.profile?.company?.PIB}`,
  64. firmLocation: `${props?.profile?.company?.contacts?.location ?? ""}`,
  65. firmWebsite: `${props?.profile?.company?.contacts?.web ?? ""}`,
  66. firmApplink: "",
  67. firmPhone: `${props?.profile?.company?.contacts?.telephone ?? ""}`,
  68. firmLogo: profileImage,
  69. },
  70. validationSchema: editProfileValidation,
  71. onSubmit: handleSubmit,
  72. validateOnBlur: true,
  73. enableReinitialize: true,
  74. });
  75. const closeEditModalHandler = () => {
  76. props.closeModalHandler();
  77. };
  78. const showDetailsHandler = () => {
  79. setShowDetails(!showDetails);
  80. setShowBasic(!showBasic);
  81. };
  82. const setImage = (image) => {
  83. setProfileImage(image);
  84. };
  85. return (
  86. <>
  87. <BackdropComponent
  88. handleClose={closeEditModalHandler}
  89. isLoading
  90. position="fixed"
  91. />
  92. <EditProfileContainer component="form" onSubmit={formik.handleSubmit}>
  93. {!showBasic && (
  94. <BackButton onClick={showDetailsHandler}>
  95. <ArrowBack />
  96. </BackButton>
  97. )}
  98. <ProfileImageContainer>
  99. <ProfileImagePicker
  100. image={profileImage}
  101. setImage={setImage}
  102. ></ProfileImagePicker>
  103. <ProfileHeader>{props.profile.company.name}</ProfileHeader>
  104. </ProfileImageContainer>
  105. <CloseButton onClick={closeEditModalHandler}>
  106. <CloseIcon />
  107. </CloseButton>
  108. {showBasic && (
  109. <BasicInfo>
  110. <InputFieldLabel leftText={t("common.labelFirm").toUpperCase()} />
  111. <InputField
  112. name="firmName"
  113. value={formik.values.firmName}
  114. onChange={formik.handleChange}
  115. error={formik.touched.firmName && formik.errors.firmName}
  116. margin="normal"
  117. fullWidth
  118. />
  119. <InputFieldLabel leftText={t("common.labelPIB")} />
  120. <InputField
  121. name="firmPIB"
  122. type="number"
  123. value={formik.values.firmPIB}
  124. onChange={formik.handleChange}
  125. error={formik.touched.firmPIB && formik.errors.firmPIB}
  126. margin="normal"
  127. fullWidth
  128. disabled
  129. />
  130. <InputFieldLabelLocation
  131. leftText={t("common.labelLocation").toUpperCase()}
  132. />
  133. <AutoSuggestTextField
  134. editLocation
  135. data={locations.map((item) => ({ name: item.city }))}
  136. value={formik.values.firmLocation}
  137. onChange={(event, { newValue }) =>
  138. formik.setFieldValue("firmLocation", newValue)
  139. }
  140. />
  141. {/* <InputField
  142. name="firmLocation"
  143. value={formik.values.firmLocation}
  144. onChange={formik.handleChange}
  145. error={formik.touched.firmLocation && formik.errors.firmLocation}
  146. margin="normal"
  147. fullWidth
  148. /> */}
  149. </BasicInfo>
  150. )}
  151. {showDetails && (
  152. <DetailsInfo>
  153. <InputFieldLabel
  154. leftText={t("editProfile.website").toUpperCase()}
  155. labelWebsite
  156. />
  157. <InputField
  158. name="firmWebsite"
  159. value={formik.values.firmWebsite}
  160. onChange={formik.handleChange}
  161. margin="normal"
  162. fullWidth
  163. />
  164. <InputFieldLabel
  165. leftText={t("editProfile.applink").toUpperCase()}
  166. />
  167. <InputField
  168. name="firmApplink"
  169. values={formik.values.firmApplink}
  170. margin="normal"
  171. fullWidth
  172. />
  173. <InputFieldLabel
  174. leftText={t("editProfile.phoneNumber").toUpperCase()}
  175. />
  176. <InputField
  177. type="number"
  178. name="firmPhone"
  179. value={formik.values.firmPhone}
  180. onChange={(event) => {
  181. formik.setFieldValue("firmPhone", event.target.value);
  182. }}
  183. error={formik.touched.firmPhone && formik.errors.firmPhone}
  184. margin="normal"
  185. fullWidth
  186. onInput={(e) => {
  187. e.target.value =
  188. e.target.value[0] === "0" && e.target.value.length > 1
  189. ? "0" +
  190. String(
  191. Math.max(0, parseInt(e.target.value))
  192. .toString()
  193. .slice(0, 14)
  194. )
  195. : Math.max(0, parseInt(e.target.value))
  196. .toString()
  197. .slice(0, 14);
  198. }}
  199. />
  200. </DetailsInfo>
  201. )}
  202. {formik.errors.firmName && formik.touched.firmName ? (
  203. <ErrorMessage>{formik.errors.firmName}</ErrorMessage>
  204. ) : formik.errors.firmPIB && formik.touched.firmPIB ? (
  205. <ErrorMessage>{formik.errors.firmPIB}</ErrorMessage>
  206. ) : formik.errors.firmLocation && formik.touched.firmLocation ? (
  207. <ErrorMessage>{formik.errors.firmLocation}</ErrorMessage>
  208. ) : formik.errors.firmPhone && formik.touched.firmPhone ? (
  209. <ErrorMessage>{formik.errors.firmPhone}</ErrorMessage>
  210. ) : (
  211. <></>
  212. )}
  213. {!isMobile ? (
  214. <ButtonsContainer>
  215. <SaveButton
  216. type="submit"
  217. variant="contained"
  218. height="48px"
  219. width="335px"
  220. buttoncolor={selectedTheme.primaryPurple}
  221. textcolor="white"
  222. >
  223. {t("editProfile.saveChanges")}
  224. </SaveButton>
  225. </ButtonsContainer>
  226. ) : (
  227. <ButtonsContainer>
  228. <SaveButton
  229. height="44px"
  230. width="155px"
  231. buttoncolor={selectedTheme.primaryPurple}
  232. textcolor={selectedTheme.primaryPurple}
  233. onClick={showDetailsHandler}
  234. >
  235. {showDetails
  236. ? t("editProfile.showBasic")
  237. : t("editProfile.showDetails")}
  238. </SaveButton>
  239. <SaveButton
  240. type="submit"
  241. variant="contained"
  242. height="44px"
  243. width="155px"
  244. buttoncolor={selectedTheme.primaryPurple}
  245. textcolor="white"
  246. >
  247. {t("common.save")}
  248. </SaveButton>
  249. </ButtonsContainer>
  250. )}
  251. </EditProfileContainer>
  252. </>
  253. );
  254. };
  255. EditProfile.propTypes = {
  256. children: PropTypes.node,
  257. profile: PropTypes.any,
  258. closeModalHandler: PropTypes.func,
  259. setImage: PropTypes.func,
  260. reFetchProfile: PropTypes.func,
  261. };
  262. export default EditProfile;