Repository navigation
Expand file tree
/
Copy pathwithStore.tsx
More file actions
74 lines (58 loc) · 1.71 KB
/
Copy pathwithStore.tsx
File metadata and controls
74 lines (58 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { inject, Provider } from 'mobx-react';
import React from 'react';
import { getUser } from './api/public';
import { getInitialData } from './api/team-member';
import { getStore, initStore, Store } from './store';
export default function withStore(BaseComponent) {
BaseComponent = inject('store')(BaseComponent);
class App extends React.Component {
public static async getInitialProps(ctx) {
const props: any = {};
// if store initialized already do not load data again
if (getStore()) {
if (BaseComponent.getInitialProps) {
Object.assign(props, (await BaseComponent.getInitialProps(ctx)) || {});
}
return props;
}
let user = null;
try {
user = ctx.req ? ctx.req.user : await getUser();
} catch (error) {
console.log(error);
}
if (BaseComponent.getInitialProps) {
Object.assign(props, (await BaseComponent.getInitialProps(ctx)) || {});
}
const { teamSlug, discussionSlug } = props;
let initialData = {};
if (user) {
try {
initialData = await getInitialData({
request: ctx.req,
data: { teamSlug, discussionSlug },
});
} catch (error) {
console.log(error);
}
}
Object.assign(props, {
initialState: { user, teamSlug, currentUrl: ctx.asPath, ...initialData },
});
return props;
}
public store: Store;
constructor(props) {
super(props);
this.store = initStore(props.initialState);
}
public render() {
return (
<Provider store={this.store}>
<BaseComponent {...this.props} />
</Provider>
);
}
}
return App;
}