1
0
mirror of https://github.com/stolksdorf/homebrewery.git synced 2025-12-12 23:45:57 +00:00

Added in the admin page

This commit is contained in:
Scott Tolksdorf
2016-01-06 20:04:45 -05:00
parent 25b53a7c24
commit 46186c0de2
11 changed files with 224 additions and 16 deletions

38
client/admin/admin.jsx Normal file
View File

@@ -0,0 +1,38 @@
var React = require('react');
var _ = require('lodash');
var cx = require('classnames');
var HomebrewAdmin = require('./homebrewAdmin/homebrewAdmin.jsx');
var Admin = React.createClass({
getDefaultProps: function() {
return {
url : "",
admin_key : "",
homebrews : [],
};
},
render : function(){
var self = this;
return(
<div className='admin'>
<header>
<div className='container'>
<i className='fa fa-rocket' />
naturalcrit admin
</div>
</header>
<div className='container'>
<HomebrewAdmin homebrews={this.props.homebrews} admin_key={this.props.admin_key} />
</div>
</div>
);
}
});
module.exports = Admin;

39
client/admin/admin.less Normal file
View File

@@ -0,0 +1,39 @@
@import 'naturalCrit/styles/reset.less';
@import 'naturalCrit/styles/elements.less';
@import 'naturalCrit/styles/animations.less';
@import 'naturalCrit/styles/colors.less';
@import 'naturalCrit/styles/tooltip.less';
@import 'font-awesome/css/font-awesome.css';
html,body, #reactContainer, .naturalCrit{
min-height : 100%;
}
@sidebarWidth : 250px;
body{
background-color : #eee;
font-family : 'Open Sans', sans-serif;
color : #4b5055;
font-weight : 100;
text-rendering : optimizeLegibility;
margin : 0;
padding : 0;
height : 100%;
}
.admin{
header{
background-color : @red;
font-size: 2em;
padding : 20px 0px;
color : white;
margin-bottom: 30px;
i{
margin-right: 30px;
}
}
}

View File

@@ -0,0 +1,58 @@
var React = require('react');
var _ = require('lodash');
var cx = require('classnames');
var Moment = require('moment')
var HomebrewAdmin = React.createClass({
getDefaultProps: function() {
return {
homebrews : [],
admin_key : ''
};
},
renderBrews : function(){
return _.map(this.props.homebrews, (brew)=>{
return <tr className={cx('brewRow', {'isEmpty' : brew.text == ""})} key={brew.sharedId}>
<td>{brew.editId}</td>
<td>{Moment(brew.createdAt).fromNow()}</td>
<td>{Moment(brew.updatedAt).fromNow()}</td>
<td>{Moment(brew.lastViewed).fromNow()}</td>
<td>{brew.views}</td>
<td><a target="_blank" href={'/homebrew/share/' + brew.shareId}>view</a></td>
<td><a href={'/homebrew/remove/' + brew.editId +'?admin_key=' + this.props.admin_key}><i className='fa fa-trash' /></a></td>
</tr>
})
},
render : function(){
var self = this;
return(
<div className='homebrewAdmin'>
<h2>Homebrews - {this.props.homebrews.length}</h2>
<table>
<thead>
<tr>
<th>Edit Id</th>
<th>Created At</th>
<th>Last Updated</th>
<th>Last Viewed</th>
<th>Number of Views</th>
<th>Preview</th>
</tr>
</thead>
<tbody>
{this.renderBrews()}
</tbody>
</table>
</div>
);
}
});
module.exports = HomebrewAdmin;

View File

@@ -0,0 +1,24 @@
.homebrewAdmin{
table{
max-height: 800px;
overflow-y: scroll;
th{
font-weight: 800;
padding: 10px;
}
tr:nth-child(even){
background-color: fade(@green, 10%);
}
tr.isEmpty{
background-color: fade(@red, 30%);
}
td{
min-width: 100px;
text-align: center;
padding: 10px;
}
}
}

View File

@@ -41,7 +41,7 @@ var EditPage = React.createClass({
if(this.state.text === ""){
this.setState({
text : FullClassGen()
text : 'Put stuff huuurr'
})
}
},

View File

@@ -141,7 +141,7 @@ module.exports = function(){
"> - **Speed** " + _.random(0,50) + "ft.",
">___",
">|STR|DEX|CON|INT|WIS|CHA|",
">|:---:|:---:|:---:|:---:|:---:|:---:|:---:|",
">|:---:|:---:|:---:|:---:|:---:|:---:|",
stats,
">___",
"> - **Condition Immunities** " + genList(["groggy", "swagged", "weak-kneed", "buzzed", "groovy", "melancholy", "drunk"], 3),

View File

@@ -5,7 +5,7 @@ var gulp = require("gulp");
var gulp = vitreumTasks(gulp, {
entryPoints: ["./client/naturalCrit", "./client/homebrew"],
entryPoints: ["./client/naturalCrit", "./client/homebrew", "./client/admin"],
DEV: true,

View File

@@ -10,6 +10,7 @@
"license": "BSD-2-Clause",
"dependencies": {
"app-module-path": "^1.0.4",
"basic-auth": "^1.0.3",
"body-parser": "^1.14.2",
"classnames": "^2.2.0",
"express": "^4.13.3",

View File

@@ -19,9 +19,46 @@ mongoose.connection.on('error', function(){
});
//Admin route
process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin';
process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password';
process.env.ADMIN_KEY = process.env.ADMIN_KEY || 'admin_key';
var auth = require('basic-auth');
var HomebrewModel = require('./server/homebrew.model.js').model;
app.get('/admin', function(req, res){
var credentials = auth(req)
if (!credentials || credentials.name !== process.env.ADMIN_USER || credentials.pass !== process.env.ADMIN_PASS) {
res.setHeader('WWW-Authenticate', 'Basic realm="example"')
return res.status(401).send('Access denied')
}
HomebrewModel.find({}, function(err, homebrews){
vitreumRender({
page: './build/admin/bundle.dot',
prerenderWith : './client/admin/admin.jsx',
clearRequireCache : true,
initialProps: {
url: req.originalUrl,
admin_key : process.env.ADMIN_KEY,
homebrews : homebrews,
},
}, function (err, page) {
return res.send(page)
});
});
});
app = require('./server/homebrew.api.js')(app);
app.get('*', function (req, res) {
vitreumRender({
page: './build/naturalCrit/bundle.dot',

View File

@@ -1,12 +1,6 @@
var vitreumRender = require('vitreum/render');
var HomebrewModel = require('./homebrew.model.js').model;
module.exports = function(app){
@@ -49,6 +43,7 @@ module.exports = function(app){
if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
var resEntry = objs[0];
resEntry.text = req.body.text;
resEntry.updatedAt = new Date();
resEntry.save(function(err, obj){
if(err) return res.status(500).send("Error while saving");
return res.status(200).send(obj);
@@ -56,6 +51,21 @@ module.exports = function(app){
});
});
app.get('/homebrew/remove/:id', function(req, res){
if(req.query && req.query.admin_key == process.env.ADMIN_KEY){
HomebrewModel.find({editId : req.params.id}, function(err, objs){
if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
var resEntry = objs[0];
resEntry.remove(function(err){
if(err) return res.status(500).send("Error while removing");
return res.status(200).send();
})
});
}else{
return res.status(401).send('Access denied');
}
});
//Share Page
@@ -67,7 +77,9 @@ module.exports = function(app){
resObj = objs[0];
}
resObj.editId = null;
resObj.lastViewed = new Date();
resObj.views = resObj.views + 1;
resObj.save();
vitreumRender({
page: './build/homebrew/bundle.dot',

View File

@@ -1,5 +1,6 @@
var mongoose = require('mongoose');
var shortid = require('shortid');
var _ = require('lodash');
var HomebrewSchema = mongoose.Schema({
shareId : {type : String, default: shortid.generate},
@@ -7,15 +8,13 @@ var HomebrewSchema = mongoose.Schema({
text : {type : String, default : ""},
createdAt : { type: Date, default: Date.now },
updatedAt : { type: Date}
updatedAt : { type: Date},
lastViewed : { type: Date},
views : {type:Number, default:0}
});
//Schema Options
HomebrewSchema.pre('save', function(done) {
this.updatedAt = new Date();
done();
});
/*
HomebrewSchema.options.toJSON.transform = function (doc, ret, options) {