Skip to content
This repository was archived by the owner on May 31, 2021. It is now read-only.

Commit ee7a1eb

Browse files
committed
better store handling
1 parent 77d48e6 commit ee7a1eb

File tree

4 files changed

+190
-50
lines changed

4 files changed

+190
-50
lines changed

build-ui/js/google-app.js

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build-ui/js/l-p-app.js

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-ui/google-app.js

+88-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*jshint esversion: 6 */
2+
/* globals document */
23
import React from 'react';
34
import ReactDOM from 'react-dom';
45

@@ -26,34 +27,103 @@ import AuthStore from './passport/auth-store';
2627
var auth = new AuthStore();
2728

2829
const AuthRedirect = <Redirect to={{pathname: '/' }}/>;
30+
2931
const workspaceListStore = new WorkspaceListStore();
3032

33+
const singWorkspaceStores = {};
34+
const fixitStores = {};
35+
const singleMapStores = {};
36+
37+
function getWorkspaceStore(workspaceID){
38+
Object.keys(singWorkspaceStores).forEach(function(key, index) {
39+
if(key === workspaceID){
40+
singWorkspaceStores[key].redispatch();
41+
return;
42+
}
43+
singWorkspaceStores[key].undispatch();
44+
});
45+
if(!singWorkspaceStores[workspaceID]){
46+
singWorkspaceStores[workspaceID] = new SingleWorkspaceStore(workspaceID);
47+
}
48+
return singWorkspaceStores[workspaceID];
49+
}
50+
51+
function getFixitStore(workspaceID){
52+
Object.keys(fixitStores).forEach(function(key, index) {
53+
if(key === workspaceID){
54+
fixitStores[key].redispatch();
55+
return;
56+
}
57+
fixitStores[key].undispatch();
58+
});
59+
if(!fixitStores[workspaceID]){
60+
fixitStores[workspaceID] = new FixitStore(workspaceID);
61+
}
62+
return fixitStores[workspaceID];
63+
}
64+
65+
function getSingleMapStore(mapID){
66+
Object.keys(singleMapStores).forEach(function(key, index) {
67+
if(key === mapID){
68+
singleMapStores[key].redispatch();
69+
return;
70+
}
71+
singleMapStores[key].undispatch();
72+
});
73+
if(!singleMapStores[mapID]){
74+
singleMapStores[mapID] = new SingleMapStore(mapID);
75+
}
76+
return singleMapStores[mapID];
77+
}
78+
3179
ReactDOM.render(
3280
<Router>
3381
<Switch>
3482
<Route exact path="/"
3583
component={
3684
(props) =>
37-
(auth.loggedIn(props.history)
38-
? <WorkspaceListPage auth={auth} history={props.history} workspaceListStore={workspaceListStore}/>
85+
(auth.loggedIn(props.history) ? <WorkspaceListPage
86+
auth={auth}
87+
history={props.history}
88+
workspaceListStore={workspaceListStore}/>
3989
: <SplashPage auth={auth} history={props.history}/>)
4090
}/>
41-
<Route exact path="/workspace/:workspaceID"
42-
render={
43-
(props) =>
44-
(auth.loggedIn(props.history)
45-
? <MapListPage singleWorkspaceStore={new SingleWorkspaceStore(props.match.params.workspaceID)} auth={auth} history={props.history}/>
46-
: AuthRedirect)
47-
}/>
48-
<Route exact path="/map/:mapID"
49-
render={
50-
(props) =>
51-
(auth.loggedIn(props.history)
52-
? <MapEditorPage auth={auth} history={props.history} singleMapStore={new SingleMapStore(props.match.params.mapID)}/>
53-
: AuthRedirect)}/>
54-
<Route exact path="/fixit/:workspaceID"
55-
render={props =>
56-
(auth.loggedIn(props.history) ? <FixitPage auth={auth} history={props.history} fixitStore={new FixitStore(props.match.params.workspaceID)}/> : AuthRedirect)}/>
91+
<Route path="/(workspace|fixit)/:workspaceID" render={(props) => {
92+
if(!auth.loggedIn()) {
93+
return AuthRedirect;
94+
}
95+
const workspaceID = props.match.params.workspaceID;
96+
const singleWorkspaceStore = getWorkspaceStore(workspaceID);
97+
const fixitStore = getFixitStore(workspaceID);
98+
99+
return (
100+
<Switch>
101+
<Route exact path="/workspace/:workspaceID">
102+
<MapListPage
103+
auth={auth}
104+
history={props.history}
105+
singleWorkspaceStore={singleWorkspaceStore} />
106+
</Route>
107+
<Route exact path="/fixit/:workspaceID">
108+
<FixitPage
109+
auth={auth}
110+
history={props.history}
111+
singleWorkspaceStore={singleWorkspaceStore}
112+
fixitStore={fixitStore}/>
113+
</Route>
114+
</Switch>
115+
);
116+
}
117+
}>
118+
</Route>
119+
<Route exact path="/map/:mapID"
120+
render={
121+
(props) =>
122+
(auth.loggedIn() ? <MapEditorPage
123+
auth={auth}
124+
history={props.history}
125+
singleMapStore={getSingleMapStore(props.match.params.mapID)}/>
126+
: AuthRedirect)}/>
57127
<Redirect from="*" to="/" />
58128
</Switch>
59129
</Router>, document.getElementById('app-container'));

src-ui/l-p-app.js

+88-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*jshint esversion: 6 */
2+
/* globals document */
23
import React from 'react';
34
import ReactDOM from 'react-dom';
45

@@ -27,16 +28,65 @@ import AuthStore from './passport/auth-store';
2728
var auth = new AuthStore();
2829

2930
const AuthRedirect = <Redirect to={{pathname: '/' }}/>;
31+
3032
const workspaceListStore = new WorkspaceListStore();
3133

34+
const singWorkspaceStores = {};
35+
const fixitStores = {};
36+
const singleMapStores = {};
37+
38+
function getWorkspaceStore(workspaceID){
39+
Object.keys(singWorkspaceStores).forEach(function(key, index) {
40+
if(key === workspaceID){
41+
singWorkspaceStores[key].redispatch();
42+
return;
43+
}
44+
singWorkspaceStores[key].undispatch();
45+
});
46+
if(!singWorkspaceStores[workspaceID]){
47+
singWorkspaceStores[workspaceID] = new SingleWorkspaceStore(workspaceID);
48+
}
49+
return singWorkspaceStores[workspaceID];
50+
}
51+
52+
function getFixitStore(workspaceID){
53+
Object.keys(fixitStores).forEach(function(key, index) {
54+
if(key === workspaceID){
55+
fixitStores[key].redispatch();
56+
return;
57+
}
58+
fixitStores[key].undispatch();
59+
});
60+
if(!fixitStores[workspaceID]){
61+
fixitStores[workspaceID] = new FixitStore(workspaceID);
62+
}
63+
return fixitStores[workspaceID];
64+
}
65+
66+
function getSingleMapStore(mapID){
67+
Object.keys(singleMapStores).forEach(function(key, index) {
68+
if(key === mapID){
69+
singleMapStores[key].redispatch();
70+
return;
71+
}
72+
singleMapStores[key].undispatch();
73+
});
74+
if(!singleMapStores[mapID]){
75+
singleMapStores[mapID] = new SingleMapStore(mapID);
76+
}
77+
return singleMapStores[mapID];
78+
}
79+
3280
ReactDOM.render(
3381
<Router>
3482
<Switch>
3583
<Route exact path="/"
3684
component={
3785
(props) =>
38-
(auth.loggedIn(props.history)
39-
? <WorkspaceListPage auth={auth} history={props.history} workspaceListStore={workspaceListStore}/>
86+
(auth.loggedIn(props.history) ? <WorkspaceListPage
87+
auth={auth}
88+
history={props.history}
89+
workspaceListStore={workspaceListStore}/>
4090
: <SplashPage auth={auth} history={props.history}/>)
4191
}/>
4292
<Route exact path="/login"
@@ -45,22 +95,42 @@ ReactDOM.render(
4595
(auth.loggedIn(props.history) ? AuthRedirect
4696
: <LoginPage auth={auth} history={props.history}/>)
4797
}/>
48-
<Route exact path="/workspace/:workspaceID"
49-
render={
50-
(props) =>
51-
(auth.loggedIn(props.history)
52-
? <MapListPage singleWorkspaceStore={new SingleWorkspaceStore(props.match.params.workspaceID)} auth={auth} history={props.history}/>
53-
: AuthRedirect)
54-
}/>
55-
<Route exact path="/map/:mapID"
56-
render={
57-
(props) =>
58-
(auth.loggedIn(props.history)
59-
? <MapEditorPage auth={auth} history={props.history} singleMapStore={new SingleMapStore(props.match.params.mapID)}/>
60-
: AuthRedirect)}/>
61-
<Route exact path="/fixit/:workspaceID"
62-
render={props =>
63-
(auth.loggedIn(props.history) ? <FixitPage auth={auth} history={props.history} fixitStore={new FixitStore(props.match.params.workspaceID)}/> : AuthRedirect)}/>
98+
<Route path="/(workspace|fixit)/:workspaceID" render={(props) => {
99+
if(!auth.loggedIn()) {
100+
return AuthRedirect;
101+
}
102+
const workspaceID = props.match.params.workspaceID;
103+
const singleWorkspaceStore = getWorkspaceStore(workspaceID);
104+
const fixitStore = getFixitStore(workspaceID);
105+
106+
return (
107+
<Switch>
108+
<Route exact path="/workspace/:workspaceID">
109+
<MapListPage
110+
auth={auth}
111+
history={props.history}
112+
singleWorkspaceStore={singleWorkspaceStore} />
113+
</Route>
114+
<Route exact path="/fixit/:workspaceID">
115+
<FixitPage
116+
auth={auth}
117+
history={props.history}
118+
singleWorkspaceStore={singleWorkspaceStore}
119+
fixitStore={fixitStore}/>
120+
</Route>
121+
</Switch>
122+
);
123+
}
124+
}>
125+
</Route>
126+
<Route exact path="/map/:mapID"
127+
render={
128+
(props) =>
129+
(auth.loggedIn() ? <MapEditorPage
130+
auth={auth}
131+
history={props.history}
132+
singleMapStore={getSingleMapStore(props.match.params.mapID)}/>
133+
: AuthRedirect)}/>
64134
<Redirect from="*" to="/" />
65135
</Switch>
66136
</Router>, document.getElementById('app-container'));

0 commit comments

Comments
 (0)