From 7a5d0ec573c0c08926ba7454ec317847bee202c9 Mon Sep 17 00:00:00 2001 From: Chris Blanchard Date: Tue, 21 Jul 2015 01:24:14 +0100 Subject: [PATCH] Added gatherers on sidebar --- .gitignore | 4 +++- config/socketio.js | 38 +++++++++++++++++++++------------ lib/chat/index.js | 0 lib/react/app.jsx | 47 ++++++++++++++++++++++++++++++++++------- public/js/app.js | 47 ++++++++++++++++++++++++++++++++++------- views/partials/menu.hbs | 4 ---- 6 files changed, 105 insertions(+), 35 deletions(-) create mode 100644 lib/chat/index.js diff --git a/.gitignore b/.gitignore index b512c09..b416250 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -node_modules \ No newline at end of file +node_modules +npm-debug.log +.module-cache/ \ No newline at end of file diff --git a/config/socketio.js b/config/socketio.js index 5750a6f..946afef 100644 --- a/config/socketio.js +++ b/config/socketio.js @@ -4,26 +4,36 @@ module.exports = function (io) { var root = io.of("/"); var authorised = io.of("/authorised"); - var emitCount = function () { - root.emit('gatherCount', { - count: root.sockets.length + // Authorisation + root.use(function (socket, next) { + console.log(socket) + socket._user = { + username: "Chris (" + socket.id.slice(0,5) + ")", + steamId: "11111111", + email: "cablanchard@gmail.com", + bans: [] + }; + next(); + }); + + var refreshGatherers = function (socket) { + var receiver = (socket !== undefined) ? socket : root; + + receiver.emit('gatherCount', { + count: root.sockets.length, + gatherers: root.sockets.map(function (socket) { + return socket._user + }) }); }; - var onConnection = function (socket) { - emitCount(); - }; - - var onDisconnect = function (socket) { - emitCount(); - }; - io.on('connection', function (socket) { - - onConnection(socket); + refreshGatherers(); + socket.on('refreshGathers', refreshGatherers.bind(null, socket)); + socket.on('disconnect', function (socket) { - onDisconnect(); + refreshGatherers(); }); }); }; diff --git a/lib/chat/index.js b/lib/chat/index.js new file mode 100644 index 0000000..e69de29 diff --git a/lib/react/app.jsx b/lib/react/app.jsx index 8afb6b9..730307a 100644 --- a/lib/react/app.jsx +++ b/lib/react/app.jsx @@ -1,10 +1,4 @@ -var MenuCounter = React.createClass({ - componentDidMount: function () { - socket.on('gatherCount', this.updateCount) - }, - updateCount: function (data) { - this.setProps({count: data.count}); - }, +var GatherCounter = React.createClass({ render: function () { return (
  • @@ -14,4 +8,41 @@ var MenuCounter = React.createClass({ } }); -React.render(, document.getElementById('side-menu')); \ No newline at end of file +var Gatherer = React.createClass({ + render: function () { + return ( +
  • + {this.props.gatherer.username} +
  • + ); + } +}); + +var GathererMenu = React.createClass({ + componentDidMount: function () { + socket.on('gatherCount', this.updateGatherers); + }, + updateGatherers: function (data) { + this.setProps({ + count: data.count, + gatherers: data.gatherers + }); + }, + render: function () { + var gatherers = this.props.gatherers.map(function (gatherer) { + return ( + + ); + }); + return ( + + ); + } +}); + + + +React.render(, document.getElementById('side-menu')); \ No newline at end of file diff --git a/public/js/app.js b/public/js/app.js index 7031260..a502a26 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -1,10 +1,4 @@ -var MenuCounter = React.createClass({displayName: "MenuCounter", - componentDidMount: function () { - socket.on('gatherCount', this.updateCount) - }, - updateCount: function (data) { - this.setProps({count: data.count}); - }, +var GatherCounter = React.createClass({displayName: "GatherCounter", render: function () { return ( React.createElement("li", null, @@ -14,4 +8,41 @@ var MenuCounter = React.createClass({displayName: "MenuCounter", } }); -React.render(React.createElement(MenuCounter, {count: 0}), document.getElementById('side-menu')); \ No newline at end of file +var Gatherer = React.createClass({displayName: "Gatherer", + render: function () { + return ( + React.createElement("li", null, + React.createElement("a", {href: "#"}, this.props.gatherer.username) + ) + ); + } +}); + +var GathererMenu = React.createClass({displayName: "GathererMenu", + componentDidMount: function () { + socket.on('gatherCount', this.updateGatherers); + }, + updateGatherers: function (data) { + this.setProps({ + count: data.count, + gatherers: data.gatherers + }); + }, + render: function () { + var gatherers = this.props.gatherers.map(function (gatherer) { + return ( + React.createElement(Gatherer, {gatherer: gatherer}) + ); + }); + return ( + React.createElement("ul", {className: "nav", id: "side-menu"}, + React.createElement(GatherCounter, React.__spread({}, this.props)), + gatherers + ) + ); + } +}); + + + +React.render(React.createElement(GathererMenu, {count: 0, gatherers: []}), document.getElementById('side-menu')); \ No newline at end of file diff --git a/views/partials/menu.hbs b/views/partials/menu.hbs index 01bc7e7..6e648a8 100644 --- a/views/partials/menu.hbs +++ b/views/partials/menu.hbs @@ -33,10 +33,6 @@