Skip to content

Commit

Permalink
Changes to user account
Browse files Browse the repository at this point in the history
novaisea committed Jul 4, 2021
1 parent e647bd1 commit aaee713
Showing 5 changed files with 123 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ class App extends React.Component {
<Content >
<Switch>
<Route path="/" children={<Home />} exact/>
<Route path="/account" children={<Account />} />
<Route path="/account/:id" children={<Account />} />
<Route path="/register" children={<Register />} />
<Route path="/login" children={<Login />} />
<Route path="/upload" children={<Uploader />} />
162 changes: 118 additions & 44 deletions src/components/account.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,133 @@
import React from 'react';
import { withRouter } from 'react-router';
import { Image, Row, Col, Typography, Space, Button, Form, Select, Input } from 'antd';
import { status, json } from '../utilities/requestHandlers';
import { Link } from "react-router-dom";

import UserContext from '../contexts/user';
import { Image, Card } from 'antd'
import {useContext} from 'react';

/**
* @function
* @name Account
* Function that shows the user informations
* @param {object} props - Gets the user information if logged in
* @returns {object} Returns the data of the user
*/
function Account(props) {
const context = useContext(UserContext);
const user = context.user;
console.log("current user in UserContext is", user);
const { Title, Paragraph } = Typography;

const [profile, setProfile] = React.useState({});
const { Option } = Select;

if (!user.loggedIn) {
<h2>Please login in order to see your account details</h2>
}
class Account extends React.Component {

static contextType = UserContext;

else if (!profile.username) {
let headers = new Headers();
headers.append('Authorization', 'Basic ' + btoa(user.username + ":" + user.password));

fetch(user.links.self, {headers:headers})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error(response.text());
}
constructor(props) {
super(props);
this.edit = this.edit.bind(this)
this.state = {
user: {}
}
}

/**
* @function
* @name componentDidMount
* Function to get the data of an user by providing its id
* @returns {object} the object containing the user information
*/
componentDidMount() {
const id = this.props.match.params.id; // available using withRouter()
const username = this.context.user.username
const password = this.context.user.password
fetch(`https://animal-hello-3000.codio-box.uk/api/v1/users/${id}`, {
method: 'GET',
headers: {
"Authorization": "Basic " + btoa(username + ":" + password)
},
})
.then(status)
.then(json)
.then(user => {
this.setState({user:user})
})
.catch(err => {
console.log(`Fetch error for user ${id}`)
});
};

/**
* @function
* @name edit
* Function to edit an user status
* @param {string} values - The status value that is going to change
* @returns {object} The values of the user that was edited
*/
edit = (values) =>{ //Edit user
const id = this.props.match.params.id;
console.log('Form data: ', values);
const {confirm, ...data} = values;
fetch(`https://animal-hello-3000.codio-box.uk/api/v1/users/${id}`, {
method: "PUT",
body: JSON.stringify(data),
headers: {
"Authorization": "Basic " + btoa(this.context.user.username + ":" + this.context.user.password),
"Content-Type": "application/json"
}
})
.then(status)
.then(json)
.then(data => {
console.log(data);
setProfile(data);
alert("User Updated!")
})
.catch(err => console.error(err));
}
.catch(errorResponse => {
console.log(errorResponse)
})
}

/** Renders all the structure with the data */
render() {

const user = this.state.user;


/** Returns the data about the user */
return (
<>
<div style= {{ padding: '2% 20%' }}>
<h1>{user.role} Account</h1>
<Card hoverable style={{ width: '450px' }}>
<p>Username: {user.username} </p>
<p>Email: {user.email}</p>
<Image width={200} src={user.avatarURL} />
{Object.keys(profile).map(key => <li key={key}>{key}: {profile[key]}</li>)}
</Card>
</div>
</>
return(
<div>
<Row type="flex" justify="space-around" align="middle">
<Col span={6} align="center">
<Image width={200} src={user.avatarURL} />
</Col>
<Col span={12}>
<Title>{user.firstName} {user.lastName}</Title>
<Paragraph>Username: {user.username}</Paragraph>
<Paragraph>Email: {user.email}</Paragraph>
<Paragraph>Date Registered: {user.dateRegistered}</Paragraph>
<Paragraph>Role: {user.role}</Paragraph>

<h1>Change your account details here:</h1>
<Form name="update" onFinish={this.edit} >
<Form.Item rules={[{required:false, message:'Change your new name'}]} name="firstName" label="First name:">
<Input />
</Form.Item>
<Form.Item rules={[{required:false, message:'Change your new name'}]} name="lastName" label="Last name:">
<Input />
</Form.Item>
<Form.Item rules={[{required:false, message:'Change your new username'}]} name="username" label="Username:">
<Input />
</Form.Item>
<Form.Item rules={[{required:false,type:'email', message:'Input a valid email'}]} name="email" label="Email:">
<Input />
</Form.Item>
<Form.Item name= "submit">
<Button type="primary" htmlType="submit">Change Details</Button>
</Form.Item>
<Form.Item>
<Button> <Link to ='/'>Reload</Link></Button>
</Form.Item>
</Form>
</Col>
<Col span={6} align="center">
</Col>
</Row>
</div>

);
}
}


export default Account;
export default withRouter(Account);
4 changes: 2 additions & 2 deletions src/components/nav.js
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ class Nav extends React.Component {
<Menu theme="dark" mode="horizontal" defaultSelectedKeys={['1']}>
<Menu.Item key="1" disabled={this.context.user.role!== "admin"}><Link to="/adminSpace">Applications</Link></Menu.Item>
<Menu.Item key="2" disabled={this.context.user.role!== "admin"}><Link to="/users">Users</Link></Menu.Item>
<Menu.Item key="3" disabled={!this.context.user.loggedIn}><Link to="/account">Account</Link></Menu.Item>
<Menu.Item key="3" disabled={!this.context.user.loggedIn}><Link to={`/account/${this.context.user.ID}`}>Account</Link></Menu.Item>
<Menu.Item key="4" disabled={!this.context.user.loggedIn} onClick={this.context.logout}> <Link to="/">Logout</Link></Menu.Item>
</Menu>
)
@@ -25,7 +25,7 @@ class Nav extends React.Component {
<Menu theme="dark" mode="horizontal" defaultSelectedKeys={['1']}>
<Menu.Item key="1" disabled={!this.context.user.loggedIn}><Link to="/">Home</Link></Menu.Item>
<Menu.Item key="2" disabled={this.context.user.role!== "user"}><Link to="/newapplication">Add Application</Link></Menu.Item>
<Menu.Item key="3" disabled={!this.context.user.loggedIn}><Link to="/account">Account</Link></Menu.Item>
<Menu.Item key="3" disabled={!this.context.user.loggedIn}><Link to={`/account/${this.context.user.ID}`}>Account</Link></Menu.Item>
<Menu.Item key="4" disabled={!this.context.user.loggedIn}><Link to="/contact">Contact Us</Link></Menu.Item>
<Menu.Item key="5" disabled={!this.context.user.loggedIn} onClick={this.context.logout}> <Link to="/">Logout</Link></Menu.Item>
</Menu>
2 changes: 1 addition & 1 deletion src/components/usersgrid.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Col, Row, Input, Select } from 'antd';
import { Col, Row, Input } from 'antd';
import UserCard from './usercard';
import { status, json } from '../utilities/requestHandlers';
import { withRouter } from 'react-router-dom';
5 changes: 1 addition & 4 deletions src/components/usersinfo.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import React from 'react';
import { withRouter } from 'react-router';
import { Image, Row, Col, Typography, Space, Button, Form, Select } from 'antd';
import { Image, Row, Col, Typography, Space, Button } from 'antd';
import { status, json } from '../utilities/requestHandlers';
import { Link } from "react-router-dom";

import UserContext from '../contexts/user';

const { Title, Paragraph } = Typography;

const { Option } = Select;

class User extends React.Component {

static contextType = UserContext;

0 comments on commit aaee713

Please sign in to comment.