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 7.6KB

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