|
| 1 | +import React, { Component } from "react"; |
| 2 | +import { connect } from "react-redux"; |
| 3 | +import PropTypes from "prop-types"; |
| 4 | +import { Link } from "react-router-dom"; |
| 5 | +import ProfileHeader from "./ProfileHeader"; |
| 6 | +import ProfileAbout from "./ProfileAbout"; |
| 7 | +import ProfileCreds from "./ProfileCreds"; |
| 8 | +import ProfileGithub from "./ProfileGithub"; |
| 9 | +import Spinner from "../common/Spinner"; |
| 10 | +import { getProfileByHandle } from "../../actions/profileActions"; |
| 11 | + |
| 12 | +class Profile extends Component { |
| 13 | + componentDidMount() { |
| 14 | + if (this.props.match.params.handle) { |
| 15 | + this.props.getProfileByHandle(this.props.match.params.handle); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + UNSAFE_componentWillReceiveProps(nextProps) { |
| 20 | + if (nextProps.profile.profile === null && this.props.profile.loading) { |
| 21 | + this.props.history.push("/not-found"); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + render() { |
| 26 | + const { profile, loading } = this.props.profile; |
| 27 | + let profileContent; |
| 28 | + |
| 29 | + if (profile === null || loading) { |
| 30 | + profileContent = <Spinner />; |
| 31 | + } else { |
| 32 | + profileContent = ( |
| 33 | + <div> |
| 34 | + <div className="row"> |
| 35 | + <div className="col-md-6"> |
| 36 | + <Link to="/profiles" className="btn btn-success mb-3 float-left"> |
| 37 | + Back To Profiles |
| 38 | + </Link> |
| 39 | + <div className="col-md-6" /> |
| 40 | + </div> |
| 41 | + </div> |
| 42 | + <ProfileHeader profile={profile} /> |
| 43 | + <ProfileAbout profile={profile} /> |
| 44 | + <ProfileCreds |
| 45 | + education={profile.education} |
| 46 | + experience={profile.experience} |
| 47 | + /> |
| 48 | + |
| 49 | + {profile.githubusername ? ( |
| 50 | + <ProfileGithub username={profile.githubusername} /> |
| 51 | + ) : null} |
| 52 | + </div> |
| 53 | + ); |
| 54 | + } |
| 55 | + return ( |
| 56 | + <div className="profile"> |
| 57 | + <div className="container"> |
| 58 | + <div className="row"> |
| 59 | + <div className="col-md-12">{profileContent}</div> |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + ); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +Profile.propTypes = { |
| 68 | + getProfileByHandle: PropTypes.func.isRequired, |
| 69 | + profile: PropTypes.object.isRequired, |
| 70 | +}; |
| 71 | + |
| 72 | +const mapStatetoProps = (state) => ({ |
| 73 | + profile: state.profile, |
| 74 | +}); |
| 75 | + |
| 76 | +export default connect(mapStatetoProps, { getProfileByHandle })(Profile); |
0 commit comments