From 04916d89315a4d31cfcc710e36beff2c6d56a06c Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sun, 15 Jan 2023 13:54:19 +1300 Subject: [PATCH] Initial notificationAdd functionality --- .../notificationAdd/notificationAdd.jsx | 99 +++++++++++++++++++ .../notificationAdd/notificationAdd.less | 20 ++++ server/admin.api.js | 17 ++-- server/notifications.model.js | 30 +++--- 4 files changed, 147 insertions(+), 19 deletions(-) create mode 100644 client/admin/notificationUtils/notificationAdd/notificationAdd.jsx create mode 100644 client/admin/notificationUtils/notificationAdd/notificationAdd.less diff --git a/client/admin/notificationUtils/notificationAdd/notificationAdd.jsx b/client/admin/notificationUtils/notificationAdd/notificationAdd.jsx new file mode 100644 index 000000000..78438644a --- /dev/null +++ b/client/admin/notificationUtils/notificationAdd/notificationAdd.jsx @@ -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
+

Add

+ {fields.map((field, idx)=>{ + return
+ + this.handleChange(e, field)} placeholder={field} /> +
; + })} + {this.state.notificationResult} + {/* + + + */} + + + {this.state.error + &&
{this.state.error.toString()}
+ } +
; + } +}); + +module.exports = NotificationAdd; diff --git a/client/admin/notificationUtils/notificationAdd/notificationAdd.less b/client/admin/notificationUtils/notificationAdd/notificationAdd.less new file mode 100644 index 000000000..32cd61d25 --- /dev/null +++ b/client/admin/notificationUtils/notificationAdd/notificationAdd.less @@ -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; + } +} \ No newline at end of file diff --git a/server/admin.api.js b/server/admin.api.js index 7190f7076..906bfdcae 100644 --- a/server/admin.api.js +++ b/server/admin.api.js @@ -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)=>{ diff --git a/server/notifications.model.js b/server/notifications.model.js index 0465822d5..721c4a89d 100644 --- a/server/notifications.model.js +++ b/server/notifications.model.js @@ -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){