|
| 1 | +import React, { Component, useState, useRef, useEffect } from "react"; |
| 2 | +import { Link } from "react-router-dom"; |
| 3 | +import { useDispatch } from "react-redux"; |
| 4 | +import "../styles/editProfile.sass"; |
| 5 | +import { editProfileSubmit } from "../redux/actions/edit-profileActions"; |
| 6 | + |
| 7 | +const EditProfile = (props) => { |
| 8 | + const [name, setName] = useState(""); |
| 9 | + const [role, setRole] = useState(""); |
| 10 | + const [hobbies, setHobbies] = useState([]); |
| 11 | + const [skills, setSkills] = useState([]); |
| 12 | + const [userId, setUserId] = useState(props.location.userId); |
| 13 | + const hobbyRef = useRef(); |
| 14 | + const skillRef = useRef(); |
| 15 | + |
| 16 | + const addHobby = (e) => { |
| 17 | + e.preventDefault(); |
| 18 | + console.log("hobby is:", hobbyRef.current.value); |
| 19 | + let prevHobbies = hobbies; |
| 20 | + if (hobbyRef.current.value != "") { |
| 21 | + prevHobbies.push(hobbyRef.current.value); |
| 22 | + } |
| 23 | + setHobbies([...prevHobbies]); |
| 24 | + console.log("his hobby is:", hobbies); |
| 25 | + hobbyRef.current.value = ""; |
| 26 | + }; |
| 27 | + |
| 28 | + const addSkills = (e) => { |
| 29 | + e.preventDefault(); |
| 30 | + let prevSkills = skills; |
| 31 | + if (skillRef.current.value != "") { |
| 32 | + prevSkills.push(skillRef.current.value); |
| 33 | + } |
| 34 | + setSkills([...prevSkills]); |
| 35 | + skillRef.current.value = ""; |
| 36 | + }; |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + document.title = `You clicked times`; |
| 40 | + console.log("This is USeEffect"); |
| 41 | + }); |
| 42 | + |
| 43 | + const dispatch = useDispatch(); |
| 44 | + |
| 45 | + const saveEdit = (e) => { |
| 46 | + e.preventDefault(); |
| 47 | + const EditedProfile = { |
| 48 | + name: name, |
| 49 | + role: role, |
| 50 | + hobbies: hobbies, |
| 51 | + skills: skills, |
| 52 | + }; |
| 53 | + console.log("USER EDITED DETAILS ARE: ", EditedProfile); |
| 54 | + dispatch(editProfileSubmit(EditedProfile, userId)); |
| 55 | + props.history.push("/login"); |
| 56 | + }; |
| 57 | + |
| 58 | + return ( |
| 59 | + <form> |
| 60 | + <div className="form-group"> |
| 61 | + <label for="inputFirstName"> Full Name </label>{" "} |
| 62 | + <input |
| 63 | + type="text" |
| 64 | + name="name" |
| 65 | + className="form-control" |
| 66 | + id="inputFullName" |
| 67 | + placeholder="Full Name" |
| 68 | + onChange={(e) => { |
| 69 | + setName(e.target.value); |
| 70 | + }} |
| 71 | + />{" "} |
| 72 | + </div>{" "} |
| 73 | + <div className="form-row"> |
| 74 | + <div className="form-group col-md-6"> |
| 75 | + <label for="inputRole"> Role </label>{" "} |
| 76 | + <input |
| 77 | + type="text" |
| 78 | + name="role" |
| 79 | + className="form-control" |
| 80 | + id="inputRole" |
| 81 | + placeholder="Web Developer" |
| 82 | + onChange={(e) => { |
| 83 | + setRole(e.target.value); |
| 84 | + }} |
| 85 | + />{" "} |
| 86 | + </div>{" "} |
| 87 | + <div className="form-group col-md-4"> |
| 88 | + <label for="inputAddress2"> Hobbies </label>{" "} |
| 89 | + <input |
| 90 | + type="text" |
| 91 | + name="hobbies" |
| 92 | + className="form-control" |
| 93 | + id="inputHobbies" |
| 94 | + placeholder="Dancing, Hiking or Reading" |
| 95 | + ref={hobbyRef} |
| 96 | + />{" "} |
| 97 | + </div>{" "} |
| 98 | + <div className="form-group col-md-2"> |
| 99 | + <button className="btn btn-primary" onClick={addHobby}> |
| 100 | + Add Hobbies{" "} |
| 101 | + </button>{" "} |
| 102 | + </div>{" "} |
| 103 | + </div>{" "} |
| 104 | + <div className="form-group"> |
| 105 | + {" "} |
| 106 | + {hobbies.length > 0 |
| 107 | + ? hobbies.map((h) => { |
| 108 | + console.log({ |
| 109 | + h, |
| 110 | + }); |
| 111 | + return ( |
| 112 | + <span id="hobbyTag" className="tags"> |
| 113 | + {" "} |
| 114 | + {h}{" "} |
| 115 | + </span> |
| 116 | + ); |
| 117 | + }) |
| 118 | + : null}{" "} |
| 119 | + </div>{" "} |
| 120 | + <div className="form-row"> |
| 121 | + <div className="form-group col-md-6"> |
| 122 | + <label for="exampleFormControlSelect2"> Skills </label>{" "} |
| 123 | + <input |
| 124 | + type="text" |
| 125 | + name="skills" |
| 126 | + className="form-control" |
| 127 | + id="inputSkills" |
| 128 | + placeholder="HTML, Javascript..." |
| 129 | + ref={skillRef} |
| 130 | + />{" "} |
| 131 | + </div>{" "} |
| 132 | + <div className="form-group col-md-2"> |
| 133 | + <button className="btn btn-primary" onClick={addSkills}> |
| 134 | + Add Skills{" "} |
| 135 | + </button>{" "} |
| 136 | + </div>{" "} |
| 137 | + </div>{" "} |
| 138 | + <div className="form-group"> |
| 139 | + {" "} |
| 140 | + {skills.map((s) => { |
| 141 | + return skills.length >= 1 ? ( |
| 142 | + <span id="skillTag" className="tags"> |
| 143 | + {" "} |
| 144 | + {s}{" "} |
| 145 | + </span> |
| 146 | + ) : null; |
| 147 | + })}{" "} |
| 148 | + </div>{" "} |
| 149 | + <div className="form-row"> |
| 150 | + <div className="form-group col-md-1"> |
| 151 | + <button className="btn btn-primary" onClick={saveEdit}> |
| 152 | + Save{" "} |
| 153 | + </button>{" "} |
| 154 | + </div>{" "} |
| 155 | + <div className="form-group col-md-1"> |
| 156 | + <button className="btn btn-primary"> Cancel </button>{" "} |
| 157 | + </div>{" "} |
| 158 | + </div>{" "} |
| 159 | + </form> |
| 160 | + ); |
| 161 | +}; |
| 162 | + |
| 163 | +export default EditProfile; |
0 commit comments