mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-02 15:02:38 +00:00
Initial notificationAdd functionality
This commit is contained in:
@@ -0,0 +1,99 @@
|
|||||||
|
require('./notificationAdd.less');
|
||||||
|
const React = require('react');
|
||||||
|
const createClass = require('create-react-class');
|
||||||
|
const cx = require('classnames');
|
||||||
|
|
||||||
|
const request = require('superagent');
|
||||||
|
|
||||||
|
const fields = ['dismissKey', 'title', 'text', 'startAt', 'stopAt'];
|
||||||
|
|
||||||
|
|
||||||
|
const NotificationAdd = createClass({
|
||||||
|
displayName : 'NotificationAdd',
|
||||||
|
getDefaultProps() {
|
||||||
|
return {};
|
||||||
|
},
|
||||||
|
getInitialState() {
|
||||||
|
return {
|
||||||
|
query : '',
|
||||||
|
notificationResult : null,
|
||||||
|
searching : false,
|
||||||
|
error : null,
|
||||||
|
|
||||||
|
dismissKey : '',
|
||||||
|
title : '',
|
||||||
|
text : '',
|
||||||
|
startAt : '',
|
||||||
|
stopAt : ''
|
||||||
|
};
|
||||||
|
},
|
||||||
|
handleChange(e, field){
|
||||||
|
const data = {};
|
||||||
|
data[field] = e.target.value;
|
||||||
|
this.setState(data);
|
||||||
|
},
|
||||||
|
saveNotification : async function(){
|
||||||
|
if(!this.state.dismissKey) return 'No notification key!';
|
||||||
|
const data = {
|
||||||
|
dismissKey : this.state.dismissKey,
|
||||||
|
title : this.state.title,
|
||||||
|
text : this.state.text,
|
||||||
|
startAt : this.state.startAt,
|
||||||
|
stopAt : this.state.stopAt
|
||||||
|
};
|
||||||
|
|
||||||
|
const notification = await request.post('/admin/notification/add')
|
||||||
|
.send(data)
|
||||||
|
.then((response)=>{
|
||||||
|
return response.body;
|
||||||
|
});
|
||||||
|
console.log(notification);
|
||||||
|
|
||||||
|
const update = {
|
||||||
|
notificationResult : `Created notification: ${JSON.stringify(notification, null, 2)}`
|
||||||
|
};
|
||||||
|
if(notification.err) {
|
||||||
|
update.notificationResult = err;
|
||||||
|
};
|
||||||
|
if(!notification.err) {
|
||||||
|
update.dismissKey = '';
|
||||||
|
update.title = '';
|
||||||
|
update.text = '';
|
||||||
|
update.startAt = '';
|
||||||
|
update.stopAt = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(update);
|
||||||
|
|
||||||
|
this.setState(update);
|
||||||
|
},
|
||||||
|
|
||||||
|
render(){
|
||||||
|
return <div className='notificationAdd'>
|
||||||
|
<h2>Add</h2>
|
||||||
|
{fields.map((field, idx)=>{
|
||||||
|
return <div key={idx}>
|
||||||
|
<label className='fieldLabel'>{field.toUpperCase()}</label>
|
||||||
|
<input className='fieldInput' type='text' value={this.state[field]} onChange={(e)=>this.handleChange(e, field)} placeholder={field} />
|
||||||
|
</div>;
|
||||||
|
})}
|
||||||
|
{this.state.notificationResult}
|
||||||
|
{/* <label>Dismiss Key:</label>
|
||||||
|
<input type='text' value={this.state.dismissKey} onChange={this.handleChange} placeholder='notification key' />
|
||||||
|
<label>Title:</label>
|
||||||
|
<input type='text' value={this.state.title} onChange={this.handleChange} placeholder='title' /> */}
|
||||||
|
<button onClick={this.saveNotification}>
|
||||||
|
<i className={cx('fas', {
|
||||||
|
'fa-save' : !this.state.searching,
|
||||||
|
'fa-spin fa-spinner' : this.state.searching,
|
||||||
|
})} />
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{this.state.error
|
||||||
|
&& <div className='error'>{this.state.error.toString()}</div>
|
||||||
|
}
|
||||||
|
</div>;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = NotificationAdd;
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
.notificationAdd{
|
||||||
|
input{
|
||||||
|
height : 33px;
|
||||||
|
margin-bottom : 20px;
|
||||||
|
padding : 0px 10px;
|
||||||
|
font-family : monospace;
|
||||||
|
}
|
||||||
|
button{
|
||||||
|
vertical-align : middle;
|
||||||
|
height : 37px;
|
||||||
|
}
|
||||||
|
.fieldLabel{
|
||||||
|
display: inline-block;
|
||||||
|
width: 10%;
|
||||||
|
}
|
||||||
|
.fieldInput{
|
||||||
|
margin-bottom: 5px;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -100,13 +100,18 @@ router.get('/admin/stats', mw.adminOnly, (req, res)=>{
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Searches for matching edit or share id, also attempts to partial match */
|
/* Searches for notification with matching key */
|
||||||
router.get('/admin/notification/lookup/:id', mw.adminOnly, (req, res, next)=>{
|
router.get('/admin/notification/lookup/:id', mw.adminOnly, (req, res, next)=>{
|
||||||
NotificationModel.findOne({ $or : [
|
NotificationModel.findOne({ dismissKey: req.params.id })
|
||||||
{ dismissKey: { '$regex': req.params.id } },
|
.exec((err, notification)=>{
|
||||||
] }).exec((err, notification)=>{
|
return res.json(notification);
|
||||||
return res.json(notification);
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/* Add new notification */
|
||||||
|
router.post('/admin/notification/add', mw.adminOnly, async (req, res, next)=>{
|
||||||
|
const notification = await NotificationModel.addNotification(req.body);
|
||||||
|
return res.json(notification);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/admin', mw.adminOnly, (req, res)=>{
|
router.get('/admin', mw.adminOnly, (req, res)=>{
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ const mongoose = require('mongoose');
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
const NotificationSchema = mongoose.Schema({
|
const NotificationSchema = mongoose.Schema({
|
||||||
dissmissKey : { type: String, index: { unique: true } },
|
dismissKey : { type: String, unique: true, required: true },
|
||||||
title : { type: String, default: '' },
|
title : { type: String, default: '' },
|
||||||
text : { type: String, default: '' },
|
text : { type: String, default: '' },
|
||||||
|
|
||||||
createdAt : { type: Date, default: Date.now },
|
createdAt : { type: Date, default: Date.now },
|
||||||
startAt : { type: Date, default: Date.now },
|
startAt : { type: Date, default: Date.now },
|
||||||
@@ -30,19 +30,23 @@ NotificationSchema.statics.getByKey = function(key, fields=null){
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
NotificationSchema.statics.addNotification = async function(dismissKey, text, title=null, startAt=new Date, stopAt=new Date){
|
NotificationSchema.statics.addNotification = async function(data){
|
||||||
const data = {
|
// console.log('add notification');
|
||||||
dismissKey : dismissKey,
|
if(!data.dismissKey) return 'Dismiss key is required!';
|
||||||
title : title,
|
const defaults = {
|
||||||
text : text,
|
title : '',
|
||||||
startAt : startAt,
|
text : '',
|
||||||
stopAt : stopAt
|
startAt : new Date,
|
||||||
|
stopAt : new Date
|
||||||
};
|
};
|
||||||
|
_.mergeWith(data, defaults, (item)=>{ if(!item) return undefined; });
|
||||||
const newNotification = new Notification(data);
|
const newNotification = new Notification(data);
|
||||||
await newNotification.save()
|
const savedNotification = await newNotification.save()
|
||||||
.catch((err)=>{return err;});
|
.catch((err)=>{
|
||||||
|
return { err: err };
|
||||||
|
});
|
||||||
|
|
||||||
return newNotification;
|
return savedNotification;
|
||||||
};
|
};
|
||||||
|
|
||||||
NotificationSchema.statics.updateNotification = async function(dismissKey, title=null, text=null, startAt=null, stopAt=null){
|
NotificationSchema.statics.updateNotification = async function(dismissKey, title=null, text=null, startAt=null, stopAt=null){
|
||||||
|
|||||||
Reference in New Issue
Block a user