Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 177bab0

Browse files
authored
chore(ts-js): more ts day8 (#1026)
* chore(ts-js): some global store && re-org spec * chore(ts-js): sharedModeal convert
1 parent 8b4b8b4 commit 177bab0

File tree

28 files changed

+147
-110
lines changed

28 files changed

+147
-110
lines changed

config/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// export * from './config.json'
22
// import * as CONFIG from './config.json'
33

4+
import type { TThemeName } from '@/spec'
5+
import { DEFAULT_THEME as DEFAULT_THEME_CONFIG } from './config.json'
6+
47
export { default as LABEL_POOL } from './label_pool'
58
export { default as SEO } from './next_seo'
69

10+
export const DEFAULT_THEME = DEFAULT_THEME_CONFIG as TThemeName
11+
712
// explicit export to avoid eslint warning
813
export {
914
GRAPHQL_ENDPOINT,
10-
DEFAULT_THEME,
1115
SENIOR_AMOUNT_THRESHOLD,
1216
SPONSOR_AMOUNT_THRESHOLD,
1317
PAGE_SIZE,

src/spec/account.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export type TUser = {
2+
id: string
3+
login: string
4+
nickname: string
5+
avatar: string
6+
}
7+
export type TAccount = TUser & {
8+
customization?: {
9+
theme: string
10+
bannerLayout: string
11+
}
12+
isLogin?: boolean
13+
isValidSession?: boolean
14+
// ...
15+
}
16+
17+
export type TMembership = 'seniorMember' | 'sponsorMember' | 'donateMember'

src/spec/index.ts

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export type {
1414
export type { TButton } from './comp'
1515

1616
export type { TTheme, TThemeMap, TThemeName } from './theme'
17+
export type { TAccount, TUser, TMembership } from './account'
18+
export type { TTestable, TActive, TSpace, TGAEvent, TSEO, TLink } from './utils'
1719

1820
export type TRoute = {
1921
communityPath?: string
@@ -22,23 +24,6 @@ export type TRoute = {
2224
subPath?: string
2325
}
2426

25-
export type TTestable = {
26-
testid: string
27-
}
28-
29-
export type TActive = {
30-
active?: boolean
31-
show?: boolean
32-
visible?: boolean
33-
}
34-
35-
export type TSpace = {
36-
top?: number
37-
bottom?: number
38-
left?: number
39-
right?: number
40-
}
41-
4227
export type TRootStore = RootStoreType
4328

4429
// c# like
@@ -51,19 +36,6 @@ export type TCommunity = {
5136
raw: string
5237
}
5338

54-
export type TUser = {
55-
id: string
56-
login: string
57-
nickname: string
58-
avatar: string
59-
}
60-
export type TAccount = TUser & {
61-
customization?: {
62-
theme: string
63-
bannerLayout: string
64-
}
65-
// ...
66-
}
6739
export type TArticle = {
6840
id: string
6941
title: string
@@ -82,20 +54,4 @@ export type TViewing = {
8254
post: TArticle
8355
}
8456

85-
// google analytis format
86-
export type GA_EVENT = {
87-
action: string
88-
category: string
89-
label: string
90-
value: string
91-
}
92-
93-
export type TSEO = {
94-
url: string
95-
title: string
96-
description: string
97-
}
98-
99-
export type TLink = {
100-
href: string
101-
}
57+
export type TThread = string

src/spec/theme.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type TThemeName =
44
| 'purple'
55
| 'yellow'
66
| 'github'
7-
| 'Green'
7+
| 'green'
88
| 'ironGreen'
99
| 'monokai'
1010

src/spec/utils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export type TTestable = {
2+
testid: string
3+
}
4+
5+
export type TActive = {
6+
active?: boolean
7+
show?: boolean
8+
visible?: boolean
9+
}
10+
11+
export type TSpace = {
12+
top?: number
13+
bottom?: number
14+
left?: number
15+
right?: number
16+
}
17+
18+
// google analytis format
19+
export type TGAEvent = {
20+
action: string
21+
category: string
22+
label: string
23+
value: string
24+
}
25+
26+
export type TSEO = {
27+
url: string
28+
title: string
29+
description: string
30+
}
31+
32+
export type TLink = {
33+
href: string
34+
}

src/stores/AccountStore/index.js renamed to src/stores/AccountStore/index.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
*
44
*/
55

6-
import { types as T, getParent } from 'mobx-state-tree'
6+
import { types as T, getParent, Instance } from 'mobx-state-tree'
77
import { merge, clone, remove, insert, findIndex, propEq } from 'ramda'
88

9+
import type { TRootStore, TCommunity } from '@/spec'
910
import { markStates, buildLog, stripMobx, BStore } from '@/utils'
1011
import { User, EmptyUser, PagedCommunities } from '@/model'
1112

@@ -18,9 +19,6 @@ const AccountStore = T.model('AccountStore', {
1819
userSubscribedCommunities: T.maybeNull(PagedCommunities),
1920
})
2021
.views((self) => ({
21-
get root() {
22-
return getParent(self)
23-
},
2422
get accountInfo() {
2523
return {
2624
...stripMobx(self.user),
@@ -48,72 +46,81 @@ const AccountStore = T.model('AccountStore', {
4846
},
4947
}))
5048
.actions((self) => ({
51-
logout() {
52-
self.root.drawer.close()
53-
self.sessionCleanup()
49+
logout(): void {
50+
const root = getParent(self) as TRootStore
51+
const { sessionCleanup } = self as TStore
52+
root.drawer.close()
53+
sessionCleanup()
5454
},
55-
isMemberOf(type) {
55+
isMemberOf(type): boolean {
5656
const { achievement } = stripMobx(self.user)
5757
if (!achievement) return false
5858
return achievement[type] || false
5959
},
60-
updateAccount(sobj) {
60+
updateAccount(sobj): void {
6161
const user = merge(stripMobx(self.user), { ...sobj })
62+
const { mark } = self as TStore
6263

63-
self.mark({ user })
64+
mark({ user })
6465
},
6566
updateSession({ isValid, user }) {
6667
self.isValidSession = isValid
68+
69+
const { setSession, updateAccount, sessionCleanup } = self as TStore
70+
6771
if (isValid) {
68-
self.setSession(user, BStore.get('token'))
69-
return self.updateAccount(user || {})
72+
setSession(user, BStore.get('token'))
73+
return updateAccount(user || {})
7074
}
71-
return self.sessionCleanup()
75+
return sessionCleanup()
7276
},
73-
setSession(user, token) {
77+
setSession(user: string, token: string): void {
7478
// log('setSession user: ', user)
7579
// log('setSession token: ', token)
7680

7781
BStore.set('user', user)
7882
BStore.set('token', token)
7983
BStore.cookie.set('jwtToken', token)
8084
},
81-
sessionCleanup() {
85+
sessionCleanup(): void {
86+
// @ts-ignore
8287
self.user = EmptyUser
8388
self.isValidSession = false
8489
BStore.remove('user')
8590
BStore.remove('token')
8691
BStore.cookie.remove('jwtToken')
8792
},
88-
loadSubscribedCommunities(data) {
93+
loadSubscribedCommunities(data): void {
8994
// self.user.subscribedCommunities = data
9095
self.userSubscribedCommunities = data
9196
},
92-
addSubscribedCommunity(community) {
97+
addSubscribedCommunity(community: TCommunity): void {
9398
const {
9499
userSubscribedCommunities: { entries },
95100
} = self
96-
101+
// @ts-ignore
97102
self.userSubscribedCommunities.entries = insert(0, community, entries)
98103
self.userSubscribedCommunities.totalCount += 1
99104
},
100105

101-
removeSubscribedCommunity(community) {
106+
removeSubscribedCommunity(community: TCommunity): void {
102107
const {
103108
userSubscribedCommunities: { entries },
104109
} = self
105110

106111
const index = findIndex(propEq('id', community.id), entries)
112+
// @ts-ignore
107113
self.userSubscribedCommunities.entries = remove(index, 1, entries)
108114
self.userSubscribedCommunities.totalCount -= 1
109115
},
110116
updateC11N(options) {
111117
const curCustomization = clone(self.accountInfo.customization)
112118
self.user.customization = merge(curCustomization, options)
113119
},
114-
mark(sobj) {
120+
mark(sobj: Record<string, unknown>): void {
115121
markStates(sobj, self)
116122
},
117123
}))
118124

125+
export type TStore = Instance<typeof AccountStore>
119126
export default AccountStore
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)