diff --git a/app/helpers/api.js b/app/helpers/api.js
index 76e6d21..140b046 100644
--- a/app/helpers/api.js
+++ b/app/helpers/api.js
@@ -4,12 +4,20 @@
*/
const schema = require('../util/schema');
+// ID for next task
+let nextTaskId = 0;
+
/**
* Add api stuff
* @param api {Api} Api class
*/
module.exports = (api) => {
api.addPlugin({
+
+ /**
+ * Sends a notification to the client
+ * @param notification {Object} notification to send
+ */
fireNotification: function (notification) {
// Validate
schema({
@@ -22,24 +30,129 @@ module.exports = (api) => {
}
// Fire along
api.sockets.use((socket) => {
- socket.emit('notification', notification);
+ socket.emit(
+ 'notification',
+ notification
+ );
});
});
+ },
+
+ /**
+ * Creates a tasks and sends it to the client
+ * @param task {Object} inital task state
+ * @return Object
+ */
+ createTask: function (task) {
+ const taskToSend = Object.assign({
+ id: nextTaskId,
+ status: "0%",
+ percentage: 0
+ }, task);
+
+ schema({
+ app: { required: true, type: 'string' },
+ status: 'string',
+ percentage: 'number'
+ }, task, (err) => {
+ if (err) {
+ this.logger.throw_noexit(err);
+ }
+ // Emit
+ api.sockets.use((socket) => {
+ socket.emit(
+ 'task:new',
+ taskToSend
+ );
+ });
+ });
+ // Increase next task id
+ nextTaskId++;
+
+ // Task actions
+ return {
+ task: taskToSend,
+
+ /**
+ * Updates the status of a task
+ * @param newStatus {Object} new status of the task
+ */
+ update(newStatus = {}) {
+ this.task = Object.assign(this.task, newStatus);
+ api.sockets.use((socket) => {
+ socket.emit(
+ 'task:update',
+ this.task
+ );
+ });
+ },
+
+ /**
+ * Ends a task
+ * @param newStatus {Object} new status of the task
+ */
+ end(newStatus = {}) {
+ this.task = Object.assign(
+ this.task,
+ { status: 'Done!' },
+ newStatus,
+ { percentage: '1' }
+ );
+ // Send
+ api.sockets.use((socket) => {
+ socket.emit(
+ 'task:end',
+ this.task
+ );
+ });
+ },
+
+ /**
+ * Sets what to do in the case of the user cancelling the task
+ * @param cb {Function} what to do on canel
+ */
+ onCancel(cb) {
+ const taskTmp = this.task;
+ api.sockets.use((socket) => {
+ socket.on(
+ 'task:cancel',
+ (task) => {
+ if (task === taskTmp) {
+ return cb(() => {
+ // Tell it to end
+ socket.emit('task:end', taskTmp);
+ });
+ }
+ }
+ );
+ });
+
+ }
+ };
}
});
// For testing
api.fireNotification({
app: 'Test',
- body: 'test',
+ body: Math.round(Math.random() * 100).toString(),
icon: '/img/home-icon.png'
});
+ const task = api.createTask({
+ app: 'Task',
+ status: 'Doing some stuff...',
+ percentage: '0'
+ });
+ task.update({
+ percentage: '0.6'
+ });
+
// For testing. Remove for final copy
api.app.get('/api/dev/fire/notification', (req, res) => {
api.io.emit('notification', {
app: 'Test',
- body: 'test',
+ body: Math.round(Math.random() * 100).toString(),
icon: '/img/home-icon.png'
});
res.status(200);
diff --git a/client/base/components/notifications.jsx b/client/base/components/notifications.jsx
index 345876f..2f777e7 100644
--- a/client/base/components/notifications.jsx
+++ b/client/base/components/notifications.jsx
@@ -7,6 +7,9 @@ import { SidebarItem } from './navbar/sidebar';
import io from 'socket.io-client';
import '../../sass/notifications.scss';
+// Socket
+const socket = io.connect('/');
+
// Component
export const Notifications = React.createClass({
propTypes: {
@@ -17,9 +20,8 @@ export const Notifications = React.createClass({
updateStatus: PropTypes.func.isRequired
},
componentDidMount() {
- this.socket = io.connect('/');
// Wacth for notification event
- this.socket.on('notification', (notification) => {
+ socket.on('notification', (notification) => {
const date = new Date();
// From http://stackoverflow.com/questions/1760250/how-to-tell-if-browser-tab-is-active (document.hidden)
if (document.hidden) {
@@ -41,12 +43,16 @@ export const Notifications = React.createClass({
const notificationShown = Object.assign({
date: dateString
}, notification);
- this.props.add(notificationShown);
+ // Check if not in already
+ console.log(notification);
+ if (!this.props.notifications.includes(notificationShown)) {
+ this.props.add(notificationShown);
+ }
});
},
render() {
return (
-
+