diff options
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/authentication/controllers/WorkspacesMenu.tsx | 56 | ||||
| -rw-r--r-- | src/server/database.ts | 10 | ||||
| -rw-r--r-- | src/server/index.ts | 88 |
3 files changed, 68 insertions, 86 deletions
diff --git a/src/server/authentication/controllers/WorkspacesMenu.tsx b/src/server/authentication/controllers/WorkspacesMenu.tsx index b565af193..ebc4f5a10 100644 --- a/src/server/authentication/controllers/WorkspacesMenu.tsx +++ b/src/server/authentication/controllers/WorkspacesMenu.tsx @@ -4,62 +4,37 @@ import { observable, action, configure, reaction, computed, ObservableMap, runIn import { observer } from "mobx-react"; import * as request from 'request' import './WorkspacesMenu.css' +import { Document } from '../../../fields/Document'; +import { Server } from '../../../client/Server'; +import { Field } from '../../../fields/Field'; export interface WorkspaceMenuProps { - active: string; - load: (workspaceId: string) => void; - new: () => string; + active: Document; + open: (workspace: Document) => void; + new: () => void; + allWorkspaces: Document[]; } @observer export class WorkspacesMenu extends React.Component<WorkspaceMenuProps> { static Instance: WorkspacesMenu; @observable private workspacesExposed: boolean = false; - @observable private workspaceIds: Array<string> = []; - @observable private selectedWorkspaceId: string = ""; constructor(props: WorkspaceMenuProps) { super(props); WorkspacesMenu.Instance = this; - this.loadExistingWorkspace = this.loadExistingWorkspace.bind(this); this.addNewWorkspace = this.addNewWorkspace.bind(this); - this.selectedWorkspaceId = this.props.active; } @action addNewWorkspace() { - let newId = this.props.new(); - this.selectedWorkspaceId = newId; - this.props.load(newId); + this.props.new(); this.toggle(); } @action - loadExistingWorkspace = (e: React.MouseEvent<HTMLLIElement, MouseEvent>) => { - let id = e.currentTarget.innerHTML; - this.props.load(id); - this.selectedWorkspaceId = id; - } - - @action toggle() { - if (this.workspacesExposed) { - this.workspacesExposed = !this.workspacesExposed; - } else { - request.get(window.location.origin + "/getAllWorkspaceIds", this.idCallback) - } - } - - @action.bound - idCallback: request.RequestCallback = (error, response, body) => { - this.workspaceIds = []; - let ids: Array<string> = JSON.parse(body) as Array<string>; - if (ids) { - for (let i = 0; i < ids.length; i++) { - this.workspaceIds.push(ids[i]); - } - this.workspacesExposed = !this.workspacesExposed; - } + this.workspacesExposed = !this.workspacesExposed; } render() { @@ -78,8 +53,7 @@ export class WorkspacesMenu extends React.Component<WorkspaceMenuProps> { transition: "all 1s ease", zIndex: 15, padding: 10, - }} - > + }}> <img src="https://bit.ly/2IBBkxk" style={{ @@ -90,16 +64,16 @@ export class WorkspacesMenu extends React.Component<WorkspaceMenuProps> { }} onClick={this.addNewWorkspace} /> - {this.workspaceIds.map(s => + {this.props.allWorkspaces.map(s => <li className={"ids"} - key={s} + key={s.Id} style={{ listStyleType: "none", - color: s === this.selectedWorkspaceId ? "darkblue" : "black", + color: s.Id === this.props.active.Id ? "darkblue" : "black", cursor: "grab" }} - onClick={this.loadExistingWorkspace} - >{s}</li> + onClick={() => this.props.open(s)} + >{s.Title}</li> )} </div> ); diff --git a/src/server/database.ts b/src/server/database.ts index 07c5819ab..1553dd94e 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -70,6 +70,16 @@ export class Database { let collection = this.db.collection('documents'); let cursor = collection.find({ _id: { "$in": ids } }) cursor.toArray((err, docs) => { + if (err) { + console.log("Error"); + console.log(err.message); + console.log(err.errmsg); + console.log(ids); + console.log(["afca93a8-c6bd-4b58-967e-07784c5b12c8"]); + console.log("MAKES SENSE: " + (ids instanceof Array)); + } + console.log(typeof ids); + console.log("DATABASE: " + docs); fn(docs); }) }; diff --git a/src/server/index.ts b/src/server/index.ts index e6f08bc29..40e1f6686 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -80,60 +80,58 @@ app.use((req, res, next) => { // AUTHENTICATION ROUTING +enum Method { + Get, + Post +} + +function addSecureRoute(method: Method, + route: string, + handler: (user: DashUserModel, req: express.Request, res: express.Response) => void, + nope: (res: express.Response) => any) { + route = "/" + route; + switch (method) { + case Method.Get: + app.get(route, (req, res) => { + const dashUser: DashUserModel = req.user; + if (!dashUser) return nope(res); + handler(dashUser, req, res); + }); + break; + case Method.Post: + app.post(route, (req, res) => { + const dashUser: DashUserModel = req.user; + if (!dashUser) return nope(res); + handler(dashUser, req, res); + }); + break; + } +} + // *** // Look for the definitions of these get and post // functions in the exports of user.ts -// /home defines destination after a successful log in -app.get("/home", (req, res) => { - // if user is not logged in, redirect to log in page - const dashUser: DashUserModel = req.user; - if (!dashUser) { - return res.redirect("/login"); - } - // otherwise, connect them to Dash - // TODO: store and manage users' workspaces - // if (dashUser.allWorkspaceIds.length > 0) { - // if (!dashUser.didSelectSessionWorkspace) { - // return res.redirect("/workspaces"); - // } - // } +addSecureRoute(Method.Get, "home", (user, req, res) => { res.sendFile(path.join(__dirname, '../../deploy/index.html')); -}); - -// app.get("/workspaces", getWorkspaces); +}, res => res.redirect("/login")) -app.get("/getActiveWorkspaceId", (req, res) => { - const dashUser: DashUserModel = req.user; - if (!dashUser) { - return; - } - res.send(dashUser.activeWorkspaceId || ""); -}); +addSecureRoute(Method.Get, "getActiveWorkspaceId", (user, req, res) => { + console.log(`/getActiveWorkspaceId in index.ts ${user.activeWorkspaceId}`); + res.send(user.activeWorkspaceId || ""); +}, () => { }); -app.get("/getAllWorkspaceIds", (req, res) => { - const dashUser: DashUserModel = req.user; - if (!dashUser) { - return; - } - res.send(JSON.stringify(dashUser.allWorkspaceIds as Array<String>)); -}) +addSecureRoute(Method.Get, "getAllWorkspaceIds", (user, req, res) => { + res.send(JSON.stringify(user.allWorkspaceIds as Array<String>)); +}, () => { }); -app.post("/setActiveWorkspaceId", (req, res) => { - const dashUser: DashUserModel = req.user; - if (!dashUser) { - return; - } - dashUser.update({ $set: { activeWorkspaceId: req.body.target } }, () => { }); -}) +addSecureRoute(Method.Post, "setActiveWorkspaceId", (user, req) => { + user.update({ $set: { activeWorkspaceId: req.body.target } }, () => { }); +}, () => { }); -app.post("/addWorkspaceId", (req, res) => { - const dashUser: DashUserModel = req.user; - if (!dashUser) { - return; - } - dashUser.update({ $push: { allWorkspaceIds: req.body.target } }, () => { }); -}) +addSecureRoute(Method.Post, "addWorkspaceId", (user, req) => { + user.update({ $push: { allWorkspaceIds: req.body.target } }, () => { }); +}, () => { }); // anyone attempting to navigate to localhost at this port will // first have to login |
