diff --git a/saas/app/lib/api/team-member.ts b/saas/app/lib/api/team-member.ts index 548955b8..24faf72b 100644 --- a/saas/app/lib/api/team-member.ts +++ b/saas/app/lib/api/team-member.ts @@ -36,10 +36,10 @@ export const getInitialDataApiMethod = (options: any = {}) => ), ); -export const getTeamListApiMethod = () => - sendRequestAndGetResponse(`${BASE_PATH}/teams`, { - method: 'GET', - }); +// export const getTeamListApiMethod = () => +// sendRequestAndGetResponse(`${BASE_PATH}/teams`, { +// method: 'GET', +// }); export const getTeamMembersApiMethod = (teamId: string) => sendRequestAndGetResponse(`${BASE_PATH}/teams/get-members`, { diff --git a/saas/app/lib/store/discussion.ts b/saas/app/lib/store/discussion.ts index c5cb3996..d028d30d 100644 --- a/saas/app/lib/store/discussion.ts +++ b/saas/app/lib/store/discussion.ts @@ -1,4 +1,4 @@ -import { action, decorate, IObservableArray, observable, runInAction, computed } from 'mobx'; +import { action, IObservableArray, observable, runInAction, computed, makeObservable } from 'mobx'; import { addPostApiMethod, @@ -26,6 +26,31 @@ class Discussion { public notificationType: string; constructor(params) { + makeObservable(this, { + name: observable, + slug: observable, + memberIds: observable, + posts: observable, + isLoadingPosts: observable, + + editDiscussion: action, + changeLocalCache: action, + + setInitialPosts: action, + loadPosts: action, + addPost: action, + addPostToLocalCache: action, + deletePost: action, + + addDiscussionToLocalCache: action, + editDiscussionFromLocalCache: action, + deleteDiscussionFromLocalCache: action, + editPostFromLocalCache: action, + deletePostFromLocalCache: action, + + members: computed, + }); + this._id = params._id; this.createdUserId = params.createdUserId; this.store = params.store; @@ -236,29 +261,4 @@ class Discussion { } } -decorate(Discussion, { - name: observable, - slug: observable, - memberIds: observable, - posts: observable, - isLoadingPosts: observable, - - editDiscussion: action, - changeLocalCache: action, - - setInitialPosts: action, - loadPosts: action, - addPost: action, - addPostToLocalCache: action, - deletePost: action, - - addDiscussionToLocalCache: action, - editDiscussionFromLocalCache: action, - deleteDiscussionFromLocalCache: action, - editPostFromLocalCache: action, - deletePostFromLocalCache: action, - - members: computed, -}); - export { Discussion }; diff --git a/saas/app/lib/store/index.ts b/saas/app/lib/store/index.ts index dd08c9be..96c65491 100644 --- a/saas/app/lib/store/index.ts +++ b/saas/app/lib/store/index.ts @@ -1,11 +1,10 @@ -import * as mobx from 'mobx'; -import { action, decorate, IObservableArray, observable } from 'mobx'; +import { action, configure, IObservableArray, observable, makeObservable } from 'mobx'; import { useStaticRendering } from 'mobx-react'; // @ts-expect-error no exported member io socket.io-client import { io } from 'socket.io-client'; import { addTeamApiMethod, getTeamInvitationsApiMethod } from '../api/team-leader'; -import { getTeamListApiMethod, getTeamMembersApiMethod } from '../api/team-member'; +import { getTeamMembersApiMethod } from '../api/team-member'; import { User } from './user'; import { Team } from './team'; @@ -14,7 +13,7 @@ const dev = process.env.NODE_ENV !== 'production'; useStaticRendering(typeof window === 'undefined'); -mobx.configure({ enforceActions: 'observed' }); +configure({ enforceActions: 'observed' }); class Store { public isServer: boolean; @@ -22,7 +21,7 @@ class Store { public currentUser?: User = null; public currentUrl = ''; - public currentTeam?: Team; + public currentTeam?: Team = null; public teams: IObservableArray = observable([]); @@ -37,6 +36,16 @@ class Store { isServer: boolean; socket?: SocketIOClient.Socket; }) { + makeObservable(this, { + currentUser: observable, + currentUrl: observable, + currentTeam: observable, + + changeCurrentUrl: action, + setCurrentUser: action, + setCurrentTeam: action, + }); + this.isServer = !!isServer; this.setCurrentUser(initialState.user); @@ -45,12 +54,16 @@ class Store { // console.log(initialState.user); - if (initialState.teamSlug || (initialState.user && initialState.user.defaultTeamSlug)) { - this.setCurrentTeam( - initialState.teamSlug || initialState.user.defaultTeamSlug, - initialState.teams, - ); - } + // if (initialState.teamSlug || (initialState.user && initialState.user.defaultTeamSlug)) { + // this.setCurrentTeam( + // initialState.teamSlug || initialState.user.defaultTeamSlug, + // initialState.teams, + // ); + // } + + console.log(initialState.team); + + this.setCurrentTeam(initialState.team); this.socket = socket; @@ -84,40 +97,25 @@ class Store { return team; } - public async setCurrentTeam(slug: string, initialTeams: any[]) { + public async setCurrentTeam(team) { if (this.currentTeam) { - if (this.currentTeam.slug === slug) { + if (this.currentTeam.slug === team.slug) { return; } } - let found = false; - - const teams = initialTeams || (await getTeamListApiMethod()).teams; - - console.log(teams.length); + this.currentTeam = new Team({ ...team, store: this }); - for (const team of teams) { - if (team.slug === slug) { - found = true; - this.currentTeam = new Team({ ...team, store: this }); + console.log(team); - const users = - team.initialMembers || (await getTeamMembersApiMethod(this.currentTeam._id)).users; + const users = + team.initialMembers || (await getTeamMembersApiMethod(this.currentTeam._id)).users; - const invitations = - team.initialInvitations || - (await getTeamInvitationsApiMethod(this.currentTeam._id)).invitations; + const invitations = + team.initialInvitations || + (await getTeamInvitationsApiMethod(this.currentTeam._id)).invitations; - this.currentTeam.setInitialMembersAndInvitations(users, invitations); - - break; - } - } - - if (!found) { - this.currentTeam = null; - } + this.currentTeam.setInitialMembersAndInvitations(users, invitations); } public addTeamToLocalCache(data): Team { @@ -148,16 +146,6 @@ class Store { } } -decorate(Store, { - currentUser: observable, - currentUrl: observable, - currentTeam: observable, - - changeCurrentUrl: action, - setCurrentUser: action, - setCurrentTeam: action, -}); - let store: Store = null; function initializeStore(initialState = {}) { diff --git a/saas/app/lib/store/post.ts b/saas/app/lib/store/post.ts index d8911878..bd446c53 100644 --- a/saas/app/lib/store/post.ts +++ b/saas/app/lib/store/post.ts @@ -1,4 +1,4 @@ -import { action, computed, decorate, observable, runInAction } from 'mobx'; +import { action, computed, observable, runInAction, makeObservable } from 'mobx'; import { editPostApiMethod } from '../api/team-member'; @@ -22,6 +22,18 @@ export class Post { public lastUpdatedAt: Date; constructor(params) { + makeObservable(this, { + content: observable, + htmlContent: observable, + isEdited: observable, + lastUpdatedAt: observable, + + editPost: action, + changeLocalCache: action, + + user: computed, + }); + this._id = params._id; this.createdUserId = params.createdUserId; this.createdAt = params.createdAt; @@ -65,15 +77,3 @@ export class Post { return this.discussion.team.members.get(this.createdUserId) || null; } } - -decorate(Post, { - content: observable, - htmlContent: observable, - isEdited: observable, - lastUpdatedAt: observable, - - editPost: action, - changeLocalCache: action, - - user: computed, -}); diff --git a/saas/app/lib/store/team.ts b/saas/app/lib/store/team.ts index 47e67eea..2b18f073 100644 --- a/saas/app/lib/store/team.ts +++ b/saas/app/lib/store/team.ts @@ -1,4 +1,4 @@ -import { action, computed, decorate, IObservableArray, observable, runInAction } from 'mobx'; +import { action, computed, IObservableArray, observable, runInAction, makeObservable } from 'mobx'; import Router from 'next/router'; import { cancelSubscriptionApiMethod, @@ -48,6 +48,33 @@ class Team { public isPaymentFailed: boolean; constructor(params) { + makeObservable(this, { + name: observable, + slug: observable, + avatarUrl: observable, + memberIds: observable, + members: observable, + invitations: observable, + currentDiscussion: observable, + currentDiscussionSlug: observable, + isLoadingDiscussions: observable, + discussions: observable, + + setInitialMembersAndInvitations: action, + updateTheme: action, + inviteMember: action, + removeMember: action, + setInitialDiscussions: action, + loadDiscussions: action, + addDiscussion: action, + addDiscussionToLocalCache: action, + deleteDiscussion: action, + deleteDiscussionFromLocalCache: action, + getDiscussionBySlug: action, + + orderedDiscussions: computed, + }); + this._id = params._id; this.teamLeaderId = params.teamLeaderId; this.slug = params.slug; @@ -293,31 +320,4 @@ class Team { } } -decorate(Team, { - name: observable, - slug: observable, - avatarUrl: observable, - memberIds: observable, - members: observable, - invitations: observable, - currentDiscussion: observable, - currentDiscussionSlug: observable, - isLoadingDiscussions: observable, - discussions: observable, - - setInitialMembersAndInvitations: action, - updateTheme: action, - inviteMember: action, - removeMember: action, - setInitialDiscussions: action, - loadDiscussions: action, - addDiscussion: action, - addDiscussionToLocalCache: action, - deleteDiscussion: action, - deleteDiscussionFromLocalCache: action, - getDiscussionBySlug: action, - - orderedDiscussions: computed, -}); - export { Team }; diff --git a/saas/app/lib/store/user.ts b/saas/app/lib/store/user.ts index 59647234..8d45a5c9 100644 --- a/saas/app/lib/store/user.ts +++ b/saas/app/lib/store/user.ts @@ -1,4 +1,4 @@ -import { action, decorate, observable, runInAction } from 'mobx'; +import { action, observable, runInAction, makeObservable } from 'mobx'; import * as NProgress from 'nprogress'; @@ -41,6 +41,21 @@ class User { }; constructor(params) { + makeObservable(this, { + slug: observable, + email: observable, + displayName: observable, + avatarUrl: observable, + // darkTheme: observable, + defaultTeamSlug: observable, + stripeCard: observable, + stripeListOfInvoices: observable, + + updateProfile: action, + toggleTheme: action, + getListOfInvoices: action, + }); + this.store = params.store; this._id = params._id; this.slug = params.slug; @@ -92,19 +107,4 @@ class User { } } -decorate(User, { - slug: observable, - email: observable, - displayName: observable, - avatarUrl: observable, - // darkTheme: observable, - defaultTeamSlug: observable, - stripeCard: observable, - stripeListOfInvoices: observable, - - updateProfile: action, - toggleTheme: action, - getListOfInvoices: action, -}); - export { User }; diff --git a/saas/app/package.json b/saas/app/package.json index a4bb2f9d..4e3cf144 100644 --- a/saas/app/package.json +++ b/saas/app/package.json @@ -26,8 +26,8 @@ "keycode": "^2.2.0", "lru-cache": "^6.0.0", "marked": "^1.2.7", - "mobx": "^5.15.7", - "mobx-react": "^6.3.0", + "mobx": "^6.3.1", + "mobx-react": "^6.3.1", "moment": "^2.29.1", "next": "^9.1.2", "nprogress": "0.2.0", diff --git a/saas/app/pages/_app.tsx b/saas/app/pages/_app.tsx index 5362dc36..b29e563f 100644 --- a/saas/app/pages/_app.tsx +++ b/saas/app/pages/_app.tsx @@ -62,7 +62,7 @@ class MyApp extends App { console.log(error); } - let initialData = {}; + let initialData; if (userObj) { try { @@ -79,9 +79,24 @@ class MyApp extends App { // console.log(teamSlug); + const selectedTeamSlug = teamSlug || userObj.defaultTeamSlug; + + const team = initialData.teams.find((t) => { + return t.slug === selectedTeamSlug; + }); + + console.log('App', teamSlug, userObj.defaultTeamSlug, initialData.teams); + console.log('App', team); + return { ...appProps, - initialState: { user: userObj, currentUrl: ctx.asPath, teamSlug, ...initialData }, + initialState: { + user: userObj, + currentUrl: ctx.asPath, + team, + teamSlug, + ...initialData, + }, }; } diff --git a/saas/app/yarn.lock b/saas/app/yarn.lock index 9f4e4673..d9f81675 100644 --- a/saas/app/yarn.lock +++ b/saas/app/yarn.lock @@ -4889,17 +4889,17 @@ mobx-react-lite@^2.2.0: resolved "https://registry.yarnpkg.com/mobx-react-lite/-/mobx-react-lite-2.2.2.tgz#87c217dc72b4e47b22493daf155daf3759f868a6" integrity sha512-2SlXALHIkyUPDsV4VTKVR9DW7K3Ksh1aaIv3NrNJygTbhXe2A9GrcKHZ2ovIiOp/BXilOcTYemfHHZubP431dg== -mobx-react@^6.3.0: +mobx-react@^6.3.1: version "6.3.1" resolved "https://registry.yarnpkg.com/mobx-react/-/mobx-react-6.3.1.tgz#204f9756e42e19d91cb6598837063b7e7de87c52" integrity sha512-IOxdJGnRSNSJrL2uGpWO5w9JH5q5HoxEqwOF4gye1gmZYdjoYkkMzSGMDnRCUpN/BNzZcFoMdHXrjvkwO7KgaQ== dependencies: mobx-react-lite "^2.2.0" -mobx@^5.15.7: - version "5.15.7" - resolved "https://registry.yarnpkg.com/mobx/-/mobx-5.15.7.tgz#b9a5f2b6251f5d96980d13c78e9b5d8d4ce22665" - integrity sha512-wyM3FghTkhmC+hQjyPGGFdpehrcX1KOXsDuERhfK2YbJemkUhEB+6wzEN639T21onxlfYBmriA1PFnvxTUhcKw== +mobx@^6.3.1: + version "6.3.1" + resolved "https://registry.yarnpkg.com/mobx/-/mobx-6.3.1.tgz#6d2e519e7bb0275dd5b9b1fa81475894e85aad99" + integrity sha512-by0VQNBltxLKcQ0uIz2h2mnVv1ZV0ief8mRRw+0k7mL2vSS3806rZg42bt8Vy78szaODtij/5iWIGvc46ZC3Cw== moment@^2.29.1: version "2.29.1"