From 635efade0bce6173b43cebdc414ea89f4aa91995 Mon Sep 17 00:00:00 2001 From: new5558 Date: Sun, 22 Dec 2019 03:34:47 +0700 Subject: [PATCH 001/283] build: add now.json --- now.json | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 now.json diff --git a/now.json b/now.json new file mode 100644 index 0000000..036d495 --- /dev/null +++ b/now.json @@ -0,0 +1,4 @@ +{ + "name": "Bangkok JS Frontend", + "version": 2 +} From c698f4f211f4f68ed6f6cd02c4c6d9ce8da4ffc3 Mon Sep 17 00:00:00 2001 From: new5558 Date: Sun, 22 Dec 2019 14:48:54 +0700 Subject: [PATCH 002/283] refactor: mobx observable class to function --- .../hooks/hook.user.tsx | 12 +++------ interfaces/interface.user.ts | 8 ++++++ pages/_app.tsx | 15 ++++++++--- pages/login/index.tsx | 2 +- stores/store.user.tsx | 25 ++++++++----------- 5 files changed, 34 insertions(+), 28 deletions(-) rename contexts/context.user.tsx => commons/hooks/hook.user.tsx (61%) diff --git a/contexts/context.user.tsx b/commons/hooks/hook.user.tsx similarity index 61% rename from contexts/context.user.tsx rename to commons/hooks/hook.user.tsx index 2d14474..3b2cf71 100644 --- a/contexts/context.user.tsx +++ b/commons/hooks/hook.user.tsx @@ -1,6 +1,6 @@ -import { createContext, useContext, useEffect } from 'react'; +import { useContext, useEffect } from 'react'; import { when } from 'mobx'; -import UserStore from '../stores/store.user'; +import { rootContext } from '../../pages/_app'; const authenticate = (): Promise => { return new Promise(resolve => { @@ -8,12 +8,8 @@ const authenticate = (): Promise => { }); }; -const userContext = createContext(new UserStore()); - -export default userContext; - -export const useUserStore = () => { - const user = useContext(userContext); +export default () => { + const { user } = useContext(rootContext); useEffect(() => { when( () => { diff --git a/interfaces/interface.user.ts b/interfaces/interface.user.ts index 17e12dc..3405df6 100644 --- a/interfaces/interface.user.ts +++ b/interfaces/interface.user.ts @@ -3,3 +3,11 @@ export interface UserInfo { username: string; points: number; } + +export interface UserStore { + userInfo: UserInfo | undefined; + token: string; + setUserInfo(userInfo: UserInfo): void; + setToken(token: string): void; + isAuthenticated(): boolean; +} diff --git a/pages/_app.tsx b/pages/_app.tsx index ded5afe..13825d1 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -1,17 +1,24 @@ /* eslint-disable react/jsx-props-no-spreading */ import { AppProps } from 'next/app'; import { NextPage } from 'next'; +import { createContext } from 'react'; +import { useLocalStore } from 'mobx-react-lite'; import Nav from '../components/nav'; -import userContext from '../contexts/context.user'; -import UserStore from '../stores/store.user'; +import createUserStore from '../stores/store.user'; +import { UserStore } from '../interfaces/interface.user'; + +export const rootContext = createContext({ + user: {} as UserStore +}); const App: NextPage = ({ Component, pageProps }) => { + const userStore = useLocalStore(() => ({ user: createUserStore() })); return ( <> - +