Skip to content

Commit ca496c6

Browse files
committed
Remove mobx
1 parent 0a938b9 commit ca496c6

File tree

14 files changed

+214
-395
lines changed

14 files changed

+214
-395
lines changed

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
"dependencies": {
2525
"hoist-non-react-statics": "~3.3.2",
2626
"jszip": "~3.2.2",
27-
"mobx": "~5.15.4",
28-
"mobx-react": "~6.1.8",
2927
"preact": "~10.3.4",
3028
"preact-compat": "~3.19.0",
3129
"prop-types": "~15.7.2",
@@ -72,7 +70,6 @@
7270
"json-loader": "~0.5.7",
7371
"lodash": "~4.17.15",
7472
"mini-css-extract-plugin": "~0.9.0",
75-
"mobx-react-devtools": "~6.1.1",
7673
"optimize-css-assets-webpack-plugin": "~5.0.3",
7774
"postcss-import": "~12.0.1",
7875
"postcss-loader": "~3.0.0",

src/components/Confirm.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ interface State {
1010
positive: boolean
1111
}
1212

13+
// const context
14+
1315
export default class Confirm extends React.PureComponent<Props> {
1416
state: State = {
1517
buttons: [],

src/components/Tabs.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Page from './page'
66
const s = require('./style/tabs.css')
77

88
interface Props {
9-
data: TabData[]
9+
children: TabData[]
1010
name: string
1111
}
1212

@@ -20,11 +20,17 @@ export default class Tabs extends React.PureComponent<Props> {
2020

2121
render() {
2222
const active = this.state.active
23-
const tab = this.props.data[active]
23+
const tab = this.props.children[active]
2424

2525
return (
26-
<Page shadow tabsTop name={this.props.name} tabbar={map(this.props.data, this.renderButton)} fixed={tab.fixed}>
27-
{map(this.props.data, this.renderTab)}
26+
<Page
27+
shadow
28+
tabsTop
29+
name={this.props.name}
30+
tabbar={map(this.props.children, this.renderButton)}
31+
fixed={tab.fixed}
32+
>
33+
{map(this.props.children, this.renderTab)}
2834
</Page>
2935
)
3036
}

src/components/list-tab.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface Props {
1414
onLoadMore?: () => void
1515
}
1616

17-
export default class ListTab extends React.PureComponent<Props> {
17+
export default class ListTab extends React.Component<Props> {
1818
constructor(props: Props) {
1919
super(props)
2020
this.renderRow = this.renderRow.bind(this)

src/components/rsql.tsx

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import * as React from 'react'
2-
import { runInAction } from 'mobx'
3-
import { RSQLStore } from 'models/rsql'
2+
import { Loading } from './loading'
43
const request = require('utils/request').default
54
const queryParams = require('utils/queryParams').default
6-
const each = require('utils/each').default
75

86
const ENDPOINT = '/api/sql'
97

108
interface Props {
11-
variables: any
12-
children: (params: any) => any
13-
store: RSQLStore
14-
queries: any
9+
queries: any[]
10+
children: (data: any[], totals?: number[], loadMores?: boolean[]) => any
11+
variables?: any
12+
mapData?: (data: any[]) => any[]
13+
loading?: React.ReactNode
1514
}
1615

1716
export class RsqlFetcher extends React.Component<Props> {
@@ -23,19 +22,29 @@ export class RsqlFetcher extends React.Component<Props> {
2322

2423
state = {
2524
variables: this.props.variables,
26-
data: {},
25+
data: [],
26+
totals: [],
27+
loadMores: [],
28+
fetching: true,
29+
}
30+
31+
componentDidMount() {
32+
this.fetchData()
2733
}
2834

2935
render() {
30-
return this.props.children(this.state.data)
36+
if (this.state.fetching) {
37+
return this.props.loading || <Loading />
38+
}
39+
40+
return this.props.children(this.state.data, this.state.totals, this.state.loadMores)
3141
}
3242

33-
async fetchData() {
34-
const store: RSQLStore = this.props.store
43+
async fetchData(fetching = true) {
3544
const variables = this.state.variables
3645
const promiseList = []
3746
const totalsPromiseList = []
38-
store.setFetching(true)
47+
this.setState({ fetching })
3948

4049
for (let q of this.props.queries) {
4150
const query = q.query(variables)
@@ -44,10 +53,10 @@ export class RsqlFetcher extends React.Component<Props> {
4453
query.limit += offset
4554
}
4655

47-
const promise = request(`${ENDPOINT}?${queryParams(query)}`).then((data: any[]) => ({ key: q.prop, data }))
56+
const promise = request(`${ENDPOINT}?${queryParams(query)}`)
4857
promiseList.push(promise)
4958

50-
if (q.pagination && store.setTotal) {
59+
if (q.pagination) {
5160
const totalParams = queryParams({
5261
fields: ['COUNT(*) as count'],
5362
table: query.table,
@@ -59,14 +68,14 @@ export class RsqlFetcher extends React.Component<Props> {
5968
}
6069
}
6170

62-
const result: any = await Promise.all(promiseList)
63-
const totals: any = await Promise.all(totalsPromiseList)
64-
runInAction('set book data', () => {
65-
each(result, (qr: any) => store.setData(qr.key, qr.data))
66-
each(totals, (tr: any) => store.setTotal(tr.key, tr.count))
67-
store.setFetching(false)
68-
this.setState({})
69-
})
71+
let data: any = await Promise.all(promiseList)
72+
const totals: any = await Promise.all(totalsPromiseList).then(result => result.map(r => r?.count ?? 0))
73+
74+
if (this.props.mapData) {
75+
data = this.props.mapData(data)
76+
}
77+
78+
this.setState({ data, totals, fetching: false })
7079
}
7180

7281
setVariables(newVariables: any) {
@@ -76,28 +85,51 @@ export class RsqlFetcher extends React.Component<Props> {
7685
}
7786

7887
async loadMore(type: string, count: number = 20) {
79-
const store: RSQLStore = this.props.store
8088
const variables = this.state.variables
89+
const index = this.getIndex(type)
8190

82-
const q = this.props.queries.find(q => q.prop === type)
91+
if (index < 0) return
92+
const q = this.props.queries[index]
8393

84-
if (!q) {
85-
return
86-
}
87-
store.setLoadMore(type, true)
94+
this.setLoadMore(index, true)
8895

8996
const query = q.query(variables)
9097
let offset = (this.offsets[q.prop] || 0) as number
91-
query.offset = (store as any)[type].length
98+
query.offset = this.state.data[index].length
9299
query.limit = count
93100
this.offsets[q.prop] = offset + count
94101

95-
const result = await request(`${ENDPOINT}?${queryParams(query)}`)
102+
const data = await request(`${ENDPOINT}?${queryParams(query)}`)
96103

97-
runInAction(() => {
98-
store.appendData(q.prop, result)
99-
store.setLoadMore(type, false)
100-
this.setState({})
101-
})
104+
this.appendData(index, data)
105+
this.setLoadMore(index, false)
106+
}
107+
108+
getData(prop: string) {
109+
return this.state.data[this.getIndex(prop)]
110+
}
111+
112+
updateData(prop: string, data) {
113+
this.state.data[this.getIndex(prop)] = data
114+
115+
this.setState({ data: [...this.state.data] })
116+
}
117+
118+
private getIndex(prop: string) {
119+
return this.props.queries.findIndex(q => q.prop === prop)
120+
}
121+
122+
private setLoadMore(index: number, value: boolean) {
123+
const loadMores = [...this.state.loadMores]
124+
loadMores[index] = value
125+
126+
this.setState({ loadMores })
127+
}
128+
129+
private appendData(index: number, data: any[]) {
130+
const result = [...this.state.data]
131+
result[index] = result[index].concat(data)
132+
133+
this.setState({ data: result })
102134
}
103135
}

src/main.tsx

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
11
import * as React from 'react'
22
import * as ReactDOM from 'react-dom'
3-
import { Provider as MobxProvider } from 'mobx-react'
43

5-
// Both configureStore and Root are required conditionally.
6-
import BookStore from './stores/BookStore'
7-
import HomePageStore from './stores/HomePageStore'
84
require('./theme/global.css')
95
require('./theme/variables.css')
106

11-
const homePageStore = new HomePageStore()
12-
const stores = {
13-
bookStore: new BookStore(homePageStore),
14-
homePageStore,
15-
}
16-
177
const rootEl = document.getElementById('root')
188

199
// necessary for hot reloading
2010
let renderDom = () => {
2111
const Root = require('./containers/Root/index').default
22-
const Provider: any = MobxProvider
2312

24-
ReactDOM.render(
25-
<Provider {...stores}>
26-
<Root />
27-
</Provider>,
28-
rootEl,
29-
)
13+
ReactDOM.render(<Root />, rootEl)
3014
}
3115

3216
if ((module as any).hot) {

0 commit comments

Comments
 (0)