-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import { Card } from 'antd'; | ||
import NavImage from './navimage'; | ||
import {Link} from "react-router-dom"; | ||
|
||
const { Meta } = Card; | ||
|
||
class UserCard extends React.Component { | ||
|
||
/** Renders all the structure with the data using the user ID */ | ||
render() { | ||
const userID = this.props.ID; | ||
return ( | ||
<Link to={`/user/${userID}`}> | ||
<Card | ||
style={{ width: 320, textAlign: "center", paddingTop: "50px"}} | ||
cover={<NavImage src={this.props.avatarURL} to={`/users/${userID}`} />} | ||
hoverable={true}> | ||
<Meta title={this.props.username} description={this.props.email} /> | ||
</Card> | ||
</Link> | ||
); | ||
} | ||
} | ||
|
||
export default UserCard; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import { PageHeader } from 'antd'; | ||
import UsersGrid from './usersgrid'; | ||
import { Image } from 'antd' | ||
import Icon from './icon.png'; | ||
|
||
/** | ||
* @function | ||
* @name User | ||
* Function to show the users page | ||
* @param {object} props | ||
* @returns {object} Page Structure | ||
*/ | ||
function User(props) { | ||
return ( | ||
<div className="site-layout-content"> | ||
<div style={{ padding: '2% 20%' }}> | ||
<Image width={200} alt="TLD" src={Icon} /> | ||
<PageHeader | ||
title="Users" | ||
subTitle="All registered users appear here"/> | ||
</div> | ||
<UsersGrid/> | ||
</div> | ||
); | ||
} | ||
|
||
export default User; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import React from 'react'; | ||
import { Col, Row, Input, Select } from 'antd'; | ||
import UserCard from './usercard'; | ||
import { status, json } from '../utilities/requestHandlers'; | ||
import { withRouter } from 'react-router-dom'; | ||
import UserContext from '../contexts/user'; | ||
|
||
|
||
class UsersGrid extends React.Component { | ||
|
||
static contextType = UserContext; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
users: [], | ||
search:{} | ||
} | ||
} | ||
|
||
/** | ||
* @function | ||
* @name componentDidMount | ||
* Function to get all the users in the DB | ||
* @returns {object} Clickable User Cards with their data | ||
*/ | ||
componentDidMount() { | ||
const username = this.context.user.username | ||
const password = this.context.user.password | ||
let url = "https://animal-hello-3000.codio-box.uk/api/v1/users" | ||
if (this.props.query) {url = `https://animal-hello-3000.codio-box.uk/api/v1/users?query=${this.props.query}`} | ||
fetch(url, { | ||
method: "GET", | ||
headers: { | ||
"Authorization": "Basic " + btoa(username + ":" + password) | ||
}, | ||
}) | ||
.then(status) | ||
.then(json) | ||
.then(data => { | ||
this.setState({ users: data }) | ||
}) | ||
.catch(err => { | ||
console.log("Not found") | ||
}); | ||
} | ||
|
||
/** | ||
* @function | ||
* @name searchHandler | ||
* Function to handle the search query | ||
* @param {object} value - Query for the search | ||
* @returns {object} Output from the search | ||
*/ | ||
searchHandler = value => { | ||
console.log(value) | ||
this.setState({search:value}) | ||
} | ||
|
||
/** Renders the page using User Card and filters the search query */ | ||
render() { | ||
const { Search } = Input; | ||
|
||
if (!this.context.user.loggedIn) { | ||
return ( | ||
<h2>Login first in order to see applications!</h2> | ||
) | ||
}if (this.context.user.role!=="admin") { | ||
return ( | ||
<h2>You have to be a user in order to see applications!</h2> | ||
) | ||
}if(this.state.search.length){ //If anything is written in the search bar does the search | ||
const searchedUser = this.state.users.filter(user=>user.username.toLowerCase() === this.state.search.toLowerCase() || user.email.toLowerCase() === this.state.search.toLowerCase()).map(user => { | ||
return ( | ||
<div style={{padding:"10px"}} key={user.ID}> | ||
<Col span={6}> | ||
<UserCard {...user} /> | ||
</Col> | ||
</div> | ||
) | ||
}); | ||
return ( | ||
<div style={{ padding: '3% 20%' }}> | ||
<Search placeholder="Search by Company Name, Company Reference Number or Email" | ||
allowClear | ||
enterButton="Search" | ||
size="large" | ||
onSearch={this.searchHandler}/> | ||
|
||
<Row type="flex" justify="space-around"> | ||
{searchedUser} | ||
</Row> | ||
</div> | ||
); | ||
}else{ //If search bar is empty shows all applications | ||
const allUsers = this.state.users.map(user => { | ||
return ( | ||
<div style={{padding:"10px"}} key={user.ID}> | ||
<Col span={6}> | ||
<UserCard {...user} /> | ||
</Col> | ||
</div> | ||
) | ||
}); | ||
return ( | ||
<div> | ||
<div style={{ padding: '3% 20%' }}> | ||
<Search placeholder="Search by Company Name, Company Reference Number or Email" | ||
allowClear | ||
enterButton="Search" | ||
size="large" | ||
onSearch={this.searchHandler}/> | ||
|
||
</div> | ||
<Row type="flex" justify="space-around"> | ||
{allUsers} | ||
</Row> | ||
</div> | ||
); | ||
} | ||
} | ||
} | ||
|
||
export default withRouter(UsersGrid); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import React from 'react'; | ||
import { withRouter } from 'react-router'; | ||
import { Image, Row, Col, Typography, Space, Button, Form, Select } 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; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.deleteHandler = this.deleteHandler.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 deleteHandler | ||
* Function that deletes the user using its id | ||
* @returns {object} the information that the user was deleted | ||
*/ | ||
deleteHandler() { | ||
const username = this.context.user.username | ||
const password = this.context.user.password | ||
fetch(`https://animal-hello-3000.codio-box.uk/api/v1/users/${this.state.user.ID}`, { | ||
method: "DELETE", | ||
headers: { | ||
"Authorization": "Basic " + btoa(username + ":" + password) | ||
} | ||
}) | ||
.then(status) | ||
.then(user => { | ||
alert(`Deleted user for company ${this.state.user.companyName}`); | ||
this.props.history.push('/adminSpace') | ||
}) | ||
.catch(error => { | ||
console.log('Delete failed' + error); | ||
}); | ||
} | ||
|
||
/** Renders all the structure with the data */ | ||
render() { | ||
if (!this.context.user.loggedIn) { | ||
return ( | ||
<h2>Login first in order to see users!</h2> | ||
) | ||
}if (this.context.user.role!=="admin") { | ||
return ( | ||
<h2>You have to be a user in order to see users!</h2> | ||
) | ||
}if (!this.state.user) { | ||
return <h3>Loading user...</h3> | ||
}else { | ||
|
||
const user = this.state.user; | ||
|
||
let adminSpace = <Space></Space> | ||
if(this.context.user.role === "admin"){ //Only admins can change it! | ||
adminSpace = <Space> | ||
<Button danger onClick={this.deleteHandler}>Delete user</Button> | ||
</Space> | ||
} | ||
|
||
|
||
/** Returns the data about the user */ | ||
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> | ||
</Col> | ||
<Col span={6} align="center"> | ||
{adminSpace} | ||
</Col> | ||
</Row> | ||
</div> | ||
|
||
); | ||
} | ||
} | ||
} | ||
|
||
export default withRouter(User); |