Skip to content

Commit

Permalink
passed this.store.socket.id to server async-labs#23
Browse files Browse the repository at this point in the history
  • Loading branch information
tima101 committed May 1, 2019
1 parent 6a6cc28 commit 5d44246
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 56 deletions.
8 changes: 7 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
"coverageDirectory": "./.coverage"
},
"dependencies": {
"@types/dotenv": "^6.1.1",
"@types/mongoose": "5.3.23",
"@types/node": "11.11.6",
"aws-sdk": "2.428.0",
"compression": "1.7.4",
"connect-mongo": "2.0.3",
"cors": "2.8.5",
"cors": "^2.8.5",
"dotenv": "7.0.0",
"express": "4.16.4",
"express-session": "1.15.6",
Expand All @@ -52,10 +53,15 @@
"winston": "3.2.1"
},
"devDependencies": {
"@types/compression": "^0.0.36",
"@types/connect-mongo": "^0.0.41",
"@types/cors": "^2.8.5",
"@types/express": "4.16.1",
"@types/express-session": "^1.15.12",
"@types/handlebars": "4.1.0",
"@types/lodash": "4.14.123",
"@types/passport": "1.0.0",
"@types/request": "^2.48.1",
"@types/socket.io": "^2.1.2",
"@types/stripe": "6.25.6",
"husky": "1.3.1",
Expand Down
10 changes: 2 additions & 8 deletions api/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ const options = {
useFindAndModify: false,
};

mongoose.connect(
MONGO_URL,
options,
);
mongoose.connect(MONGO_URL, options);

const server = express();

Expand Down Expand Up @@ -124,9 +121,6 @@ server.get('*', (_, res) => {
res.sendStatus(403);
});

server.listen(port, err => {
if (err) {
throw err;
}
http.listen(port, () => {
logger.info(`> Ready on ${ROOT_URL}`);
});
76 changes: 75 additions & 1 deletion api/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,49 @@
dependencies:
"@types/node" "*"

"@types/caseless@*":
version "0.12.2"
resolved "https://registry.yarnpkg.com/@types/caseless/-/caseless-0.12.2.tgz#f65d3d6389e01eeb458bd54dc8f52b95a9463bc8"
integrity sha512-6ckxMjBBD8URvjB6J3NcnuAn5Pkl7t3TizAg+xdlzzQGSPSmBcXf8KoIH0ua/i+tio+ZRUHEXp0HEmvaR4kt0w==

"@types/compression@^0.0.36":
version "0.0.36"
resolved "https://registry.yarnpkg.com/@types/compression/-/compression-0.0.36.tgz#7646602ffbfc43ea48a8bf0b2f1d5e5f9d75c0d0"
integrity sha512-B66iZCIcD2eB2F8e8YDIVtCUKgfiseOR5YOIbmMN2tM57Wu55j1xSdxdSw78aVzsPmbZ6G+hINc+1xe1tt4NBg==
dependencies:
"@types/express" "*"

"@types/connect-mongo@^0.0.41":
version "0.0.41"
resolved "https://registry.yarnpkg.com/@types/connect-mongo/-/connect-mongo-0.0.41.tgz#56ef68492ac94efcfeae425ae9badd0bf75aaa89"
integrity sha512-l2ork/LNrZftIaYQstJozRHwijmWz7QRbNMIo3UKVDcfsFeh5hyrHHwmurcqOEEstnw1kxaJF4Yr93AykfPsLg==
dependencies:
"@types/express" "*"
"@types/express-session" "*"
"@types/mongodb" "*"
"@types/mongoose" "*"

"@types/connect@*":
version "3.4.32"
resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.32.tgz#aa0e9616b9435ccad02bc52b5b454ffc2c70ba28"
integrity sha512-4r8qa0quOvh7lGD0pre62CAb1oni1OO6ecJLGCezTmhQ8Fz50Arx9RUszryR8KlgK6avuSXvviL6yWyViQABOg==
dependencies:
"@types/node" "*"

"@types/cors@^2.8.5":
version "2.8.5"
resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.5.tgz#c0c54c4e643e1d943d447292f2baf9dc82cfc8ec"
integrity sha512-GmK8AKu8i+s+EChK/uZ5IbrXPcPaQKWaNSGevDT/7o3gFObwSUQwqb1jMqxuo+YPvj0ckGzINI+EO7EHcmJjKg==
dependencies:
"@types/express" "*"

"@types/dotenv@^6.1.1":
version "6.1.1"
resolved "https://registry.yarnpkg.com/@types/dotenv/-/dotenv-6.1.1.tgz#f7ce1cc4fe34f0a4373ba99fefa437b0bec54b46"
integrity sha512-ftQl3DtBvqHl9L16tpqqzA4YzCSXZfi7g8cQceTz5rOlYtk/IZbFjAv3mLOQlNIgOaylCQWQoBdDQHPgEBJPHg==
dependencies:
"@types/node" "*"

"@types/express-serve-static-core@*":
version "4.16.2"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.16.2.tgz#5ee8a22e602005be6767df6b2cba9879df3f75aa"
Expand All @@ -339,6 +375,14 @@
"@types/node" "*"
"@types/range-parser" "*"

"@types/express-session@*", "@types/express-session@^1.15.12":
version "1.15.12"
resolved "https://registry.yarnpkg.com/@types/express-session/-/express-session-1.15.12.tgz#1126704826e80f8381da4fbbb35a199f550d1433"
integrity sha512-DHZXzWy6Nu5Ng0syXUiVFRpZ6/1DOXoTCWa6RG3itGrub2ioBYvgtDbkT6VHHNo3iOdHRROyWANsMBJVaflblQ==
dependencies:
"@types/express" "*"
"@types/node" "*"

"@types/express@*", "@types/[email protected]":
version "4.16.1"
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.16.1.tgz#d756bd1a85c34d87eaf44c888bad27ba8a4b7cf0"
Expand All @@ -348,6 +392,13 @@
"@types/express-serve-static-core" "*"
"@types/serve-static" "*"

"@types/form-data@*":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@types/form-data/-/form-data-2.2.1.tgz#ee2b3b8eaa11c0938289953606b745b738c54b1e"
integrity sha512-JAMFhOaHIciYVh8fb5/83nmuO/AHwmto+Hq7a9y8FzLDcC1KCU344XDOMEmahnrTFlHjgh4L0WJFczNIX2GxnQ==
dependencies:
"@types/node" "*"

"@types/[email protected]":
version "4.1.0"
resolved "https://registry.yarnpkg.com/@types/handlebars/-/handlebars-4.1.0.tgz#3fcce9bf88f85fe73dc932240ab3fb682c624850"
Expand Down Expand Up @@ -378,6 +429,14 @@
"@types/bson" "*"
"@types/node" "*"

"@types/mongoose@*":
version "5.3.27"
resolved "https://registry.yarnpkg.com/@types/mongoose/-/mongoose-5.3.27.tgz#bfa893deab793118e59d7c3ea5deb5e2e84c0599"
integrity sha512-1bQcQaYw4kN9mMuJC54FcfjyYe2gtxbUj9OAvrUA/5G2rSv0fuvu2jplizXqyADgBcXzPVEglDNzT3YrztWLoQ==
dependencies:
"@types/mongodb" "*"
"@types/node" "*"

"@types/[email protected]":
version "5.3.23"
resolved "https://registry.yarnpkg.com/@types/mongoose/-/mongoose-5.3.23.tgz#5a37bb73313f4892af9e2e6b9641a6f7b164200c"
Expand All @@ -403,6 +462,16 @@
resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.3.tgz#7ee330ba7caafb98090bece86a5ee44115904c2c"
integrity sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==

"@types/request@^2.48.1":
version "2.48.1"
resolved "https://registry.yarnpkg.com/@types/request/-/request-2.48.1.tgz#e402d691aa6670fbbff1957b15f1270230ab42fa"
integrity sha512-ZgEZ1TiD+KGA9LiAAPPJL68Id2UWfeSO62ijSXZjFJArVV+2pKcsVHmrcu+1oiE3q6eDGiFiSolRc4JHoerBBg==
dependencies:
"@types/caseless" "*"
"@types/form-data" "*"
"@types/node" "*"
"@types/tough-cookie" "*"

"@types/serve-static@*":
version "1.13.2"
resolved "https://registry.yarnpkg.com/@types/serve-static/-/serve-static-1.13.2.tgz#f5ac4d7a6420a99a6a45af4719f4dcd8cd907a48"
Expand Down Expand Up @@ -430,6 +499,11 @@
dependencies:
"@types/node" "*"

"@types/tough-cookie@*":
version "2.3.5"
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-2.3.5.tgz#9da44ed75571999b65c37b60c9b2b88db54c585d"
integrity sha512-SCcK7mvGi3+ZNz833RRjFIxrn4gI1PPR3NtuIS+6vMkvmsGjosqTJwRt5bAEFLRz+wtJMWv8+uOnZf2hi2QXTg==

"@types/yargs@^12.0.2", "@types/yargs@^12.0.9":
version "12.0.10"
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-12.0.10.tgz#17a8ec65cd8e88f51b418ceb271af18d3137df67"
Expand Down Expand Up @@ -1298,7 +1372,7 @@ [email protected], core-util-is@~1.0.0:
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=

[email protected]:
cors@^2.8.5:
version "2.8.5"
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
Expand Down
2 changes: 1 addition & 1 deletion app/components/posts/PostEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Mention, MentionsInput } from 'react-mentions';

import env from '../../lib/env';

// import getRootUrl from '../../lib/api/getRootUrl';
// import getApiUrl from '../../lib/api/getRootUrl';
import {
getSignedRequestForUpload,
uploadFileUsingSignedPutRequest,
Expand Down
2 changes: 1 addition & 1 deletion app/lib/api/getRootUrl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import env from '../env';

export default function getRootUrl() {
export default function getApiUrl() {
const port = process.env.PORT || 8000;
const dev = process.env.NODE_ENV !== 'production';
const { PRODUCTION_URL_API } = env;
Expand Down
17 changes: 15 additions & 2 deletions app/lib/api/sendRequestAndGetResponse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'isomorphic-unfetch';

import getRootUrl from './getRootUrl';
import { getStore } from '../store';

import getApiUrl from './getRootUrl';

import { makeQueryString } from './makeQueryString';

Expand All @@ -27,7 +29,7 @@ export default async function sendRequestAndGetResponse(path, opts: any = {}) {
console.log(`${path}${qs}`);

const response = await fetch(
externalServer ? `${path}${qs}` : `${getRootUrl()}${path}${qs}`,
externalServer ? `${path}${qs}` : `${getApiUrl()}${path}${qs}`,
Object.assign({ method: 'POST', credentials: 'include' }, opts, { headers }),
);

Expand All @@ -39,11 +41,22 @@ export default async function sendRequestAndGetResponse(path, opts: any = {}) {

try {
const data = JSON.parse(text);
const store = getStore();

if (data.error) {
if (response.status === 201 && data.error === 'You need to log in.' && !externalServer) {
if (store && store.currentUser && store.currentUser.isLoggedIn && !store.isServer) {
store.currentUser.logout();
}
}

throw new Error(data.error);
}

if (store && store.currentUser && !store.currentUser.isLoggedIn && !store.isServer) {
store.currentUser.login();
}

return data;
} catch (err) {
if (err instanceof SyntaxError) {
Expand Down
4 changes: 2 additions & 2 deletions app/lib/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as mobx from 'mobx';
import { action, decorate, IObservableArray, observable, runInAction } from 'mobx';
import * as io from 'socket.io-client';

import getRootUrl from '../api/getRootUrl';
import getApiUrl from '../api/getRootUrl';

import { addTeam } from '../api/team-leader';
import { getTeamList } from '../api/team-member';
Expand Down Expand Up @@ -278,7 +278,7 @@ function initStore(initialState = {}) {
}
}

const socket = io(getRootUrl());
const socket = io(getApiUrl());

store = new Store({ initialState, socket, isServer: false });

Expand Down
72 changes: 36 additions & 36 deletions app/lib/store/team.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { action, computed, decorate, IObservableArray, observable, runInAction } from 'mobx';
import { decorate, IObservableArray, observable, runInAction } from 'mobx';
import Router from 'next/router';

import {
Expand Down Expand Up @@ -385,41 +385,41 @@ class Team {
}

decorate(Team, {
name: observable,
slug: observable,
avatarUrl: observable,
memberIds: observable,
members: observable,
invitedUsers: observable,
isLoadingMembers: observable,
isInitialMembersLoaded: observable,

isSubscriptionActive: observable,
stripeSubscription: observable,
isPaymentFailed: observable,

edit: action,
setInitialMembers: action,
loadInitialMembers: action,
inviteMember: action,
removeMember: action,

currentDiscussion: observable,
currentDiscussionSlug: observable,
isLoadingDiscussions: observable,
discussions: observable,

setInitialDiscussions: action,
setCurrentDiscussion: action,
setInitialDiscussionSlug: action,
loadDiscussions: action,
addDiscussionToLocalCache: action,
editDiscussionFromLocalCache: action,
removeDiscussionFromLocalCache: action,
addDiscussion: action,
deleteDiscussion: action,

orderedDiscussions: computed,
// name: observable,
// slug: observable,
// avatarUrl: observable,
// memberIds: observable,
// members: observable,
// invitedUsers: observable,
// isLoadingMembers: observable,
// isInitialMembersLoaded: observable,

// isSubscriptionActive: observable,
// stripeSubscription: observable,
// isPaymentFailed: observable,

// edit: action,
// setInitialMembers: action,
// loadInitialMembers: action,
// inviteMember: action,
// removeMember: action,

// currentDiscussion: observable,
// currentDiscussionSlug: observable,
// isLoadingDiscussions: observable,
// discussions: observable,

// setInitialDiscussions: action,
// setCurrentDiscussion: action,
// setInitialDiscussionSlug: action,
// loadDiscussions: action,
// addDiscussionToLocalCache: action,
// editDiscussionFromLocalCache: action,
// removeDiscussionFromLocalCache: action,
// addDiscussion: action,
// deleteDiscussion: action,

// orderedDiscussions: computed,
});

export { Team };
14 changes: 14 additions & 0 deletions app/lib/store/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class User {

public darkTheme: boolean = true;

public isLoggedIn: boolean = false;

constructor(params) {
this.store = params.store;

Expand All @@ -57,6 +59,7 @@ class User {
this.stripeCard = params.stripeCard;
this.stripeListOfInvoices = params.stripeListOfInvoices;
this.darkTheme = !!params.darkTheme;
this.isLoggedIn = !!params.isLoggedIn;
}

public async updateProfile({ name, avatarUrl }: { name: string; avatarUrl: string }) {
Expand Down Expand Up @@ -120,6 +123,14 @@ class User {
await toggleTheme({ darkTheme });
window.location.reload();
}

public login() {
this.isLoggedIn = true;
}

public logout() {
this.isLoggedIn = false;
}
}

decorate(User, {
Expand All @@ -132,9 +143,12 @@ decorate(User, {
hasCardInformation: observable,
stripeCard: observable,
stripeListOfInvoices: observable,
isLoggedIn: observable,

updateProfile: action,
toggleTheme: action,
login: action,
logout: action,
});

export { User };
Loading

0 comments on commit 5d44246

Please sign in to comment.