Skip to content

Instantly share code, notes, and snippets.

@SuEric
Last active March 12, 2018 14:44
Show Gist options
  • Save SuEric/7a9b8b009fc378fb7f385412d3755e5a to your computer and use it in GitHub Desktop.
Save SuEric/7a9b8b009fc378fb7f385412d3755e5a to your computer and use it in GitHub Desktop.
UnoSquare JS interview
// CONTAINER
/*
* HomePage
*
*/
import React from 'react';
import Helmet from 'react-helmet';
import List from '../../components/List';
import request from '../../utils/request.js';
class HomePage extends React.PureComponent { // eslint-disable-line react/prefer-stateless-function
constructor(props, context) {
super(props, context);
this.state = {
ips: []
};
}
componentDidMount() {
// Get IPS
request('ip_address/', {method: 'GET'})
.then((ips) => {
this.setState({ips});
});
}
render() {
return (
<div>
<List ips={this.state.ips}/>
</div>
);
}
}
export default HomePage;
// COMPONENTS
/*
* List Component
*/
import React from 'react';
import ListItem from './Item';
const List = function({ ips }) {
return (
<div>
<ul>
{ips.map((ip) => <ListItem key={ip} value={ip} />)}
</ul>
</div>
);
}
List.propTypes = {
ips: React.PropTypes.array.isRequired,
}
export default List;
// Second component
/*
* ListItem Component
*/
import React from 'react';
const ListItem = function({value}) {
return (
<div>
<li>{value}</li>
</div>
);
}
ListItem.propTypes = {
value: React.PropTypes.string.isRequired,
}
export default ListItem;
// BACKEND
// CONTROLLER
import fs from 'fs';
class IPAddressController {
static get (req, res) {
const fileContenet = fs.readFileSync('./assets/test.txt', { encoding: 'utf8'}).trim();
const ips = fileContenet.split('\n');
res.json(
[...new Set(ips)].sort((a, b) => b-a)
);
}
}
export default IPAddressController;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment