Skip to content

Commit 1d6ab2a

Browse files
committed
recursive user browsing
1 parent 2146c96 commit 1d6ab2a

File tree

8 files changed

+146
-89
lines changed

8 files changed

+146
-89
lines changed

App/Mixins/ListHelper.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ var {
1414
ListView
1515
} = React;
1616

17+
var CurrentUserStore = require('../Stores/CurrentUserStore');
1718
var NavigationListener = require('../Mixins/NavigationListener');
19+
var NavBarHelper = require('../Mixins/NavBarHelper');
1820

1921
var Loading = require('../Screens/Loading');
2022
var Text = require('../Components/Text');
2123
var SegmentedControl = require('../Components/SegmentedControl');
2224
var SimpleList = require('../Components/SimpleList');
2325

2426
var ListHelper = {
25-
mixins: [NavigationListener],
27+
mixins: [NavigationListener, NavBarHelper],
2628

2729
getInitialState: function() {
2830
return this.getListState();
@@ -56,14 +58,27 @@ var ListHelper = {
5658
this.props.store.removeChangeListener(this.onListChange);
5759
},
5860

61+
getNavBarState: function() {
62+
var title = this.props.username ? this.props.username : "Dashboard";
63+
return { title: title };
64+
},
65+
66+
getUsername: function() {
67+
if (!this.username) {
68+
this.username = this.props.username || CurrentUserStore.get().data.username;
69+
}
70+
return this.username;
71+
},
72+
5973
renderItems: function() {
6074
return (
6175
<SimpleList
62-
{...this.props.listProps}
6376
style={styles.flex}
77+
currentRoute={this.props.currentRoute}
6478
getItemProps={this.getItemProps}
6579
items={this.state.items}
6680
reloadList={this.reloadList}
81+
{...this.props.listProps}
6782
/>
6883
);
6984
},

App/Mixins/NavBarHelper.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// parent must implement getNavBarState()
2+
3+
var TimerMixin = require('react-timer-mixin');
4+
5+
var Dispatcher = require('../Dispatcher');
6+
var AppConstants = require('../Constants/AppConstants');
7+
8+
var NavBarHelper = {
9+
mixins: [TimerMixin],
10+
11+
componentDidMount: function() {
12+
this.updateNavTitle();
13+
},
14+
15+
componentDidUpdate: function() {
16+
this.updateNavTitle();
17+
},
18+
19+
updateNavTitle: function() {
20+
var updates = this.getNavBarState();
21+
if (!updates) return;
22+
23+
if(typeof updates.title !== "undefined") {
24+
this.props.currentRoute.title = updates.title;
25+
}
26+
if (typeof updates.navRightDisabled !== "undefined") {
27+
this.props.currentRoute.navRight.disabled = updates.navRightDisabled;
28+
}
29+
if (typeof updates.navRightIcon !== "undefined") {
30+
this.props.currentRoute.navRight.icon = updates.navRightIcon;
31+
}
32+
33+
// if called during componentDidLoad, nav bar not loaded yet
34+
// requestAnimationFrame to allow it to finish
35+
var route = this.props.currentRoute;
36+
this.requestAnimationFrame(function() {
37+
Dispatcher.dispatch({
38+
actionType: AppConstants.NAVBAR_UPDATE,
39+
route: route
40+
});
41+
});
42+
},
43+
};
44+
45+
module.exports = NavBarHelper;

App/Navigation/Router.js

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,69 @@ parseUri.options = {
3434
}
3535
};
3636

37+
38+
39+
var listRoute = function(route, defaultRoute) {
40+
var username = route.passProps ? route.passProps.username : null;
41+
route.parse = function(path) {
42+
switch(path) {
43+
case '_post':
44+
return Routes.CreatePost();
45+
case '_settings':
46+
// only on 'Dashboard'
47+
if(username) return null;
48+
return Routes.Settings();
49+
default:
50+
if (!defaultRoute) return null;
51+
return defaultRoute(path);
52+
}
53+
}
54+
55+
if(!route.navRight) {
56+
route.navRight = {
57+
subPath: '_post',
58+
label: '+' // TODO: icon font
59+
};
60+
}
61+
62+
if(!route.navLeft && !username) {
63+
route.navLeft = {
64+
subPath: '_settings',
65+
label: 'Me' // TODO: icon font
66+
};
67+
}
68+
return route;
69+
};
70+
71+
var userRoute = function(username) {
72+
var route = {}
73+
route._notAddressable = true;
74+
route._routerAppend = 'posts';
75+
76+
route.parse = function(path) {
77+
switch(path) {
78+
case 'posts':
79+
return listRoute(Routes.PostList(username), function(postId) {
80+
// TOOD: show post
81+
return null;
82+
});
83+
case 'follows':
84+
return listRoute(Routes.FollowList(username), function(follow) {
85+
// it's a user
86+
return userRoute(follow);
87+
});
88+
default:
89+
return null;
90+
};
91+
};
92+
return route;
93+
};
94+
3795
var LoggedIn = {
3896
parse: function(host) {
3997
switch (host) {
40-
case 'dashboard':
41-
var dashboard = Routes.Dashboard();
42-
dashboard._routerAppend = 'posts';
43-
dashboard.parse = function(path) {
44-
switch(path) {
45-
case 'settings':
46-
return Routes.Settings();
47-
case 'post':
48-
return Routes.CreatePost();
49-
case 'posts':
50-
var posts = Routes.PostList('bleonard');
51-
posts._routerReplace = true;
52-
return posts;
53-
case 'follows':
54-
var follows = Routes.FollowList('bleonard');
55-
follows._routerReplace = true;
56-
return follows;
57-
default:
58-
return null;
59-
};
60-
};
61-
return dashboard;
98+
case 'dashboard':
99+
return userRoute(null);
62100
default:
63101
return null;
64102
}
@@ -120,7 +158,16 @@ var Router = {
120158
if (route._routerReplace) {
121159
stack[stack.length-1] = route;
122160
}
123-
else if (!route._notAddressable) {
161+
else if (route._notAddressable) {
162+
if (route._routerAppend && i === (pieces.length-1)) {
163+
// add soemthing to route on the last one (kind of like a forward)
164+
var child = route.parse(route._routerAppend);
165+
child.routePath = route.routePath + "/" + route._routerAppend;
166+
route = child;
167+
stack.push(route);
168+
}
169+
}
170+
else {
124171
if (route._routerAppend && i === (pieces.length-1)) {
125172
// add soemthing to route on the last one (kind of like a forward)
126173
route.routePath = route.routePath + "/" + route._routerAppend;

App/Navigation/Routes.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,10 @@ var Routes = {
1414
};
1515
},
1616

17-
Dashboard: function() {
18-
return {
19-
component: require('../Screens/Dashboard'),
20-
title: 'Dashboard',
21-
navLeft: {
22-
subPath: 'settings',
23-
label: 'Me' // TODO: icon font
24-
},
25-
navRight: {
26-
subPath: 'post',
27-
label: '+' // TODO: icon font
28-
}
29-
};
30-
},
31-
3217
PostList: function(username) {
3318
return {
3419
component: require('../Screens/PostList'),
3520
title: '', // set to name
36-
navRight: {
37-
subPath: 'post',
38-
label: '+' // TODO: icon font
39-
},
4021
passProps: {
4122
username: username
4223
}
@@ -47,10 +28,6 @@ var Routes = {
4728
return {
4829
component: require('../Screens/FollowList'),
4930
title: '', // set to name
50-
navRight: {
51-
subPath: 'post',
52-
label: '+' // TODO: icon font
53-
},
5431
passProps: {
5532
username: username
5633
}

App/Screens/Dashboard.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

App/Screens/FollowList.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var FollowList = React.createClass({
1212
return {
1313
store: FollowListStore,
1414
listProps: {
15-
noTap: true
15+
nextIcon: true
1616
},
1717
segment: {
1818
items: [
@@ -31,22 +31,24 @@ var FollowList = React.createClass({
3131
},
3232

3333
getListItems: function() {
34-
return FollowListStore.get(this.props.username);
34+
return FollowListStore.get(this.getUsername());
3535
},
3636

3737
isListChange: function(username) {
38-
return this.props.username == username;
38+
return this.getUsername() == username;
3939
},
4040

4141
getItemProps: function(follow) {
4242
return {
43-
title: follow.data.username
43+
key: follow.data.id,
44+
title: follow.data.username,
45+
subPath: follow.data.username
4446
}
4547
},
4648

4749
reloadList: function() {
48-
console.log("reloading follows: " + this.props.username);
49-
FollowActions.fetchList(this.props.username, function(error) {
50+
console.log("reloading follows: " + this.getUsername());
51+
FollowActions.fetchList(this.getUsername(), function(error) {
5052
// TODO: handle error
5153
if (error) {
5254
alert(error.message);

App/Screens/PostList.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,23 @@ var PostList = React.createClass({
3131
},
3232

3333
getListItems: function() {
34-
return PostListStore.get(this.props.username);
34+
return PostListStore.get(this.getUsername());
3535
},
3636

3737
isListChange: function(username) {
38-
return this.props.username == username;
38+
return this.getUsername() == username;
3939
},
4040

4141
getItemProps: function(post) {
4242
return {
43+
key: post.data.id,
4344
title: post.data.content
4445
}
4546
},
4647

4748
reloadList: function() {
48-
console.log("reloading posts: " + this.props.username);
49-
PostActions.fetchList(this.props.username, function(error) {
49+
console.log("reloading posts: " + this.getUsername());
50+
PostActions.fetchList(this.getUsername(), function(error) {
5051
// TODO: handle error
5152
if (error) {
5253
alert(error.message);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"react-native-keyboardevents": "^0.4.5",
1616
"react-native-keychain": "^0.2.5",
1717
"react-native-refreshable-listview": "^1.3.0",
18+
"react-timer-mixin": "^0.13.3",
1819
"superagent": "^1.4.0"
1920
}
2021
}

0 commit comments

Comments
 (0)