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.

ProfileCard.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import React from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. EditButton,
  5. ProfileCardWrapper,
  6. ProfileCardContainer,
  7. ProfileCardHeader,
  8. HeaderTitle,
  9. EditIcon,
  10. ProfileInfoContainer,
  11. RemoveButton,
  12. RemoveIcon,
  13. BlockButton,
  14. BlockIcon,
  15. ButtonsContainer,
  16. BlockLabelIcon,
  17. // MessageButton,
  18. // MessageIcon,
  19. ProfileInfoAndContactContainer,
  20. UnblockButton,
  21. UnblockIcon,
  22. } from "./ProfileCard.styled";
  23. import PersonOutlineIcon from "@mui/icons-material/PersonOutline";
  24. import { useRouteMatch } from "react-router-dom";
  25. import { fetchProfile } from "../../../store/actions/profile/profileActions";
  26. import { useDispatch, useSelector } from "react-redux";
  27. import { useEffect } from "react";
  28. import { selectProfile } from "../../../store/selectors/profileSelectors";
  29. import { selectUserId } from "../../../store/selectors/loginSelectors";
  30. import { fetchProfileOffers } from "../../../store/actions/offers/offersActions";
  31. import ProfileMainInfo from "./ProfileMainInfo/ProfileMainInfo";
  32. import ProfileContact from "./ProfileContact/ProfileContact";
  33. import ProfileStats from "./ProfileStats/ProfileStats";
  34. import { useTranslation } from "react-i18next";
  35. import { selectIsLoadingByActionType } from "../../../store/selectors/loadingSelectors";
  36. import { PROFILE_SCOPE } from "../../../store/actions/profile/profileActionConstants";
  37. import SkeletonProfileCard from "./SkeletonProfileCard/SkeletonProfileCard";
  38. import { useMemo } from "react";
  39. import companyData from "../../../notFoundData/companyData";
  40. import UserLabeledCard from "../LabeledCard/User/UserLabeledCard";
  41. import history from "../../../store/utils/history";
  42. import { HOME_PAGE } from "../../../constants/pages";
  43. import useIsMobile from "../../../hooks/useIsMobile";
  44. import BlockedProfile from "./BlockedProfile/BlockedProfile";
  45. import {
  46. toggleDeleteCategoryModal,
  47. toggleEditProfileModal,
  48. } from "../../../store/actions/modal/modalActions";
  49. import {
  50. USERS_BLOCK_TYPE,
  51. USERS_DELETE_TYPE,
  52. USERS_UNBLOCK_TYPE,
  53. } from "../../../constants/adminTypeConstants";
  54. import { Tooltip } from "@mui/material";
  55. const ProfileCard = (props) => {
  56. const isLoading = useSelector(selectIsLoadingByActionType(PROFILE_SCOPE));
  57. const routeMatch = useRouteMatch();
  58. const dispatch = useDispatch();
  59. const { isMobile } = useIsMobile();
  60. const profileFromRedux = useSelector(selectProfile);
  61. const userId = useSelector(selectUserId);
  62. const idProfile = useMemo(() => {
  63. return routeMatch.params.idProfile;
  64. }, [routeMatch.params.idProfile]);
  65. const { t } = useTranslation();
  66. const profile = useMemo(() => {
  67. if (profileFromRedux) {
  68. if (profileFromRedux._id === routeMatch.params.idProfile) {
  69. return profileFromRedux;
  70. }
  71. }
  72. return companyData;
  73. }, [profileFromRedux]);
  74. const isMyProfile = useMemo(
  75. () => userId === idProfile && !props.isAdmin,
  76. [userId, idProfile]
  77. );
  78. useEffect(() => {
  79. if (idProfile?.length > 0) {
  80. reFetchProfile();
  81. }
  82. }, [idProfile]);
  83. useEffect(() => {
  84. if (profile && isMyProfile !== undefined) {
  85. if (!isMyProfile && profile._blocked && !props.isAdmin)
  86. history.push(HOME_PAGE);
  87. }
  88. }, [profile, isMyProfile, props.isAdmin]);
  89. const reFetchProfile = () => {
  90. dispatch(fetchProfile(idProfile));
  91. dispatch(fetchProfileOffers(idProfile));
  92. };
  93. let percentOfSucceededExchanges;
  94. if (profile?.statistics?.exchanges?.succeeded === 0) {
  95. percentOfSucceededExchanges = 0;
  96. } else {
  97. percentOfSucceededExchanges = Math.ceil(
  98. (profile?.statistics?.exchanges?.total /
  99. profile?.statistics?.exchanges?.succeeded) *
  100. 100
  101. );
  102. }
  103. const removeUser = () => {
  104. dispatch(
  105. toggleDeleteCategoryModal({
  106. customId: profile?._id,
  107. type: USERS_DELETE_TYPE,
  108. customLabeledCard: <UserLabeledCard user={profile} />,
  109. customLabeledCardHeight: "90px",
  110. })
  111. );
  112. };
  113. const blockUser = () => {
  114. dispatch(
  115. toggleDeleteCategoryModal({
  116. customId: profile?._id,
  117. type: USERS_BLOCK_TYPE,
  118. customLabeledCard: <UserLabeledCard user={profile} />,
  119. customLabeledCardHeight: "90px",
  120. customLabeledCardIcon: <BlockLabelIcon />,
  121. })
  122. );
  123. };
  124. const unblockUser = () => {
  125. dispatch(
  126. toggleDeleteCategoryModal({
  127. customId: profile?._id,
  128. type: USERS_UNBLOCK_TYPE,
  129. customLabeledCard: <UserLabeledCard user={profile} />,
  130. customLabeledCardHeight: "90px",
  131. })
  132. );
  133. };
  134. const handleEditProfile = () => {
  135. if (!profile?._blocked) {
  136. dispatch(
  137. toggleEditProfileModal({
  138. profile: profile,
  139. isAdmin: props.isAdmin,
  140. reFetchProfile: reFetchProfile,
  141. })
  142. );
  143. }
  144. };
  145. return (
  146. <>
  147. {isLoading ? (
  148. <SkeletonProfileCard />
  149. ) : (
  150. <>
  151. <ProfileCardContainer isAdmin={props.isAdmin}>
  152. <ProfileCardHeader>
  153. <PersonOutlineIcon color="action" sx={{ mr: 0.9 }} />
  154. <HeaderTitle>
  155. {isMyProfile
  156. ? t("profile.myProfile")
  157. : t("profile.companyProfile")}
  158. </HeaderTitle>
  159. </ProfileCardHeader>
  160. <ProfileCardWrapper
  161. variant="outlined"
  162. isAdmin={props.isAdmin}
  163. isMyProfile={isMyProfile}
  164. blocked={!props.isAdmin && profile?._blocked}
  165. >
  166. <ButtonsContainer>
  167. {profile?._blocked && !isMobile && <BlockedProfile />}
  168. {props.isAdmin && (
  169. <>
  170. {profile?._blocked ? (
  171. <UnblockButton onClick={unblockUser}>
  172. <UnblockIcon />
  173. </UnblockButton>
  174. ) : (
  175. <BlockButton onClick={blockUser}>
  176. <BlockIcon />
  177. </BlockButton>
  178. )}
  179. <RemoveButton onClick={removeUser}>
  180. <RemoveIcon />
  181. </RemoveButton>
  182. </>
  183. )}
  184. {(isMyProfile || props.isAdmin) && (
  185. <Tooltip title={t("editProfile.tooltip")}>
  186. <EditButton
  187. onClick={handleEditProfile}
  188. isAdmin={props.isAdmin}
  189. disabled={!props.isAdmin && profile?._blocked}
  190. >
  191. <EditIcon />
  192. </EditButton>
  193. </Tooltip>
  194. )}
  195. </ButtonsContainer>
  196. <ProfileInfoContainer>
  197. <ProfileInfoAndContactContainer isAdmin={props.isAdmin}>
  198. {profile?._blocked && !props.isAdmin && isMobile && (
  199. <BlockedProfile aboveTitle isAdmin={props.isAdmin} />
  200. )}
  201. {/* Profile Main Info */}
  202. <ProfileMainInfo
  203. isAdmin={props.isAdmin}
  204. profile={profile}
  205. isMyProfile={isMyProfile}
  206. isBlocked={!props.isAdmin && profile?._blocked}
  207. />
  208. {/* Profile Contact */}
  209. <ProfileContact
  210. profile={profile}
  211. isAdmin={props.isAdmin}
  212. isMyProfile={
  213. (!props.isAdmin && profile?._blocked) || isMyProfile
  214. }
  215. />
  216. </ProfileInfoAndContactContainer>
  217. {/* Profile Stats */}
  218. <ProfileStats
  219. profile={profile}
  220. percentOfSucceededExchanges={percentOfSucceededExchanges}
  221. />
  222. </ProfileInfoContainer>
  223. </ProfileCardWrapper>
  224. </ProfileCardContainer>
  225. </>
  226. )}
  227. </>
  228. );
  229. };
  230. ProfileCard.propTypes = {
  231. children: PropTypes.node,
  232. isAdmin: PropTypes.bool,
  233. };
  234. export default ProfileCard;