Skip to content

Commit

Permalink
Add User look up function to Admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Ambatte committed Oct 28, 2022
1 parent 45151dd commit b84f083
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client/admin/admin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const createClass = require('create-react-class');
const BrewCleanup = require('./brewCleanup/brewCleanup.jsx');
const BrewLookup = require('./brewLookup/brewLookup.jsx');
const BrewCompress = require ('./brewCompress/brewCompress.jsx');
const UserLookup = require('./userLookup/userLookup.jsx');
const Stats = require('./stats/stats.jsx');

const Admin = createClass({
Expand All @@ -30,6 +31,8 @@ const Admin = createClass({
<BrewCleanup />
<hr />
<BrewCompress />
<hr />
<UserLookup />
</div>
</div>;
}
Expand Down
76 changes: 76 additions & 0 deletions client/admin/userLookup/userLookup.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require('./userLookup.less');
const React = require('react');
const createClass = require('create-react-class');
const cx = require('classnames');
const _ = require('lodash');

const request = require('superagent');
const Moment = require('moment');


const UserLookup = createClass({
getDefaultProps() {
return {};
},
getInitialState() {
return {
query : '',
foundUser : null,
searching : false,
error : null
};
},
handleChange(e){
this.setState({ query: e.target.value });
},
lookup(){
this.setState({ searching: true, error: null });

request.get(`/admin/userlookup/${this.state.query}`)
.then((res)=>this.setState({ foundUser: res.body }))
.catch((err)=>this.setState({ error: err }))
.finally(()=>this.setState({ searching: false }));
},

renderFoundUser(){
const user = this.state.foundUser;
return <div className='foundUser'>
<dl>
<dt>Username</dt>
<dd>{user.username}</dd>

<dt>Last Activity</dt>
<dd>{user.lastActivity} : {Moment(user.lastActivity).fromNow()}</dd>

<dt>Options</dt>
{Object.entries(user.options).map((opt, idx)=>{
return <dd key={idx}>- {opt.join(' : ')}</dd>;
})}
</dl>
</div>;
},

render(){
return <div className='userLookup'>
<h2>User Lookup</h2>
<input type='text' value={this.state.query} onChange={this.handleChange} placeholder='username' />
<button onClick={this.lookup}>
<i className={cx('fas', {
'fa-search' : !this.state.searching,
'fa-spin fa-spinner' : this.state.searching,
})} />
</button>

{this.state.error
&& <div className='error'>{this.state.error.toString()}</div>
}

{this.state.foundUser
? this.renderFoundUser()
: <div className='noUser'>No user found.</div>
}
</div>;
}
});

module.exports = UserLookup;
30 changes: 30 additions & 0 deletions client/admin/userLookup/userLookup.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

.userLookup{
input{
height : 33px;
margin-bottom : 20px;
padding : 0px 10px;
font-family : monospace;
}
button{
vertical-align : middle;
height : 37px;
}
dl{
@maxItemWidth : 132px;
dt{
float : left;
clear : left;
width : @maxItemWidth;
text-align : right;
&::after {
content: " : ";
}
}
dd{
height : 1em;
margin-left : @maxItemWidth + 6px;
padding : 0 0 0.5em 0;
}
}
}
10 changes: 10 additions & 0 deletions server/admin.api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const HomebrewModel = require('./homebrew.model.js').model;
const UserInfoModel = require('./userinfo.model.js').model;
const router = require('express').Router();
const Moment = require('moment');
// const render = require('vitreum/steps/render');
Expand Down Expand Up @@ -64,6 +65,15 @@ router.get('/admin/lookup/:id', mw.adminOnly, (req, res, next)=>{
});
});

/* Searches for matching edit or share id, also attempts to partial match */
router.get('/admin/userlookup/:id', mw.adminOnly, (req, res, next)=>{
UserInfoModel.findOne({ $or : [
{ username: { '$regex': req.params.id, '$options': 'i' } }
] }).exec((err, user)=>{
return res.json(user);
});
});

/* Find 50 brews that aren't compressed yet */
router.get('/admin/finduncompressed', mw.adminOnly, (req, res)=>{
uncompressedBrewQuery.exec((err, objs)=>{
Expand Down

0 comments on commit b84f083

Please sign in to comment.