0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 12:02:48 +00:00

Initial notificationAdd functionality

This commit is contained in:
G.Ambatte
2023-01-15 13:54:19 +13:00
parent 8adf5ce463
commit 04916d8931
4 changed files with 147 additions and 19 deletions

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -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)=>{
NotificationModel.findOne({ $or : [
{ dismissKey: { '$regex': req.params.id } },
] }).exec((err, notification)=>{
return res.json(notification);
});
NotificationModel.findOne({ dismissKey: req.params.id })
.exec((err, 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)=>{

View File

@@ -2,9 +2,9 @@ const mongoose = require('mongoose');
const _ = require('lodash');
const NotificationSchema = mongoose.Schema({
dissmissKey : { type: String, index: { unique: true } },
title : { type: String, default: '' },
text : { type: String, default: '' },
dismissKey : { type: String, unique: true, required: true },
title : { type: String, default: '' },
text : { type: String, default: '' },
createdAt : { 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){
const data = {
dismissKey : dismissKey,
title : title,
text : text,
startAt : startAt,
stopAt : stopAt
NotificationSchema.statics.addNotification = async function(data){
// console.log('add notification');
if(!data.dismissKey) return 'Dismiss key is required!';
const defaults = {
title : '',
text : '',
startAt : new Date,
stopAt : new Date
};
_.mergeWith(data, defaults, (item)=>{ if(!item) return undefined; });
const newNotification = new Notification(data);
await newNotification.save()
.catch((err)=>{return err;});
const savedNotification = await newNotification.save()
.catch((err)=>{
return { err: err };
});
return newNotification;
return savedNotification;
};
NotificationSchema.statics.updateNotification = async function(dismissKey, title=null, text=null, startAt=null, stopAt=null){