diff --git a/frontend/src/components/Profile/Profile.js b/frontend/src/components/Profile/Profile.js deleted file mode 100644 index 057b2a9..0000000 --- a/frontend/src/components/Profile/Profile.js +++ /dev/null @@ -1,14 +0,0 @@ - -// This code just for testing the Navbar elements and should be change later - -import React from "react"; - -const Profile = () => { - return ( -
-

Profile

-
- ); -}; - -export default Profile; diff --git a/frontend/src/components/Profile/ProfileForm/ProfileForm.js b/frontend/src/components/Profile/ProfileForm/ProfileForm.js deleted file mode 100644 index e69de29..0000000 diff --git a/frontend/src/components/Profile/index.js b/frontend/src/components/Profile/index.js new file mode 100644 index 0000000..fb45692 --- /dev/null +++ b/frontend/src/components/Profile/index.js @@ -0,0 +1,159 @@ +import React, { Component } from 'react'; +import Fontawesome from 'react-fontawesome'; +import Modal from 'react-modal'; + +import './style.css'; +const customStyles = { + content : { + width: '200px', + top: '50%', + left: '50%', + right: 'auto', + bottom: 'auto', + marginRight: '-50%', + transform: 'translate(-50%, -50%)' + } +}; +class ProfileForm extends Component{ + state = { + modalIsOPen:false, + modalMsg: '', + modalTitle: '', + locations: ['London', 'Gaza', 'Bristol', 'Liverpool'], + formData:{ + cared_for_situation: "Alzheimer", + date_of_birth: "2018-08-04", + location: "LONDON", + looking_for: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ", + offer: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ", + }, + } + + componentDidMount() { + const id = 4; // should get the id from the login token?? + fetch(`api/profile?id=${id}`, { + credentials: 'same-origin', + method: 'GET', + }).then(res=>res.json()) + .then((res) => { + const { sitution: cared_for_situation, age: date_of_birth, location, looking: looking_for, offer} = res[0]; + this.setState({ + cared_for_situation, + date_of_birth, + location, + looking_for, + offer, + }) + }) + .catch((err)=> { + this.openModal('Error', 'Some error happened, please refresh the page'); + }); + } + + openModal = (title, msg) => { + this.setState({ + modalIsOPen: true, + modalMsg: msg, + modalTitle: title, + }); + } + closeModal = () => { + this.setState({modalIsOPen: false}); + } + + handleChange = (e) => { + const { value, name } = e.target; + this.setState((prevState) => { + const formData = JSON.parse(JSON.stringify(prevState.formData)); + formData[name] = value; + return { formData }; + }); + } + + handleSubmit = (e) => { + e.preventDefault(); + const { formData } = this.state; + const id = 4; // should get the id from the login token?? + fetch(`api/profile?id=${id}`, { + credentials: 'same-origin', + headers: { + 'content-type': 'application/json', + }, + method: 'PUT', + body: formData, + }).then(res=>res.json()) + .then((res) => { + this.openModal('Success', 'Your profile has been updated'); + }) + .catch((err) => { + this.openModal('Error', 'Some error happened, please try save the data again'); + }); + } + + handleFocus = (e) => { + switch(e.target.tagName) { + case 'INPUT': + case 'SELECT': + case 'TEXTAREA': + e.target.classList.add('active'); + break; + } + } + + handleBlur = (e) => { + switch(e.target.tagName) { + case 'INPUT': + case 'SELECT': + case 'TEXTAREA': + e.target.classList.remove('active'); + break; + } + } + + render(){ + const { locations }= this.state; + const { location, date_of_birth, cared_for_situation, looking_for, offer } = this.state.formData; + + return ( + + +
+

Error

+

{this.state.modalMsg}

+ +
+
+
+ + + + + + + + + + + +