Skip to content

Commit 9ab5855

Browse files
committed
Merge pull request #374 from CodeNow/Fix-instance-controller-ui-routes
Making instance routing awesome-saucesome
2 parents b87ac7e + 195f4bf commit 9ab5855

File tree

17 files changed

+512
-218
lines changed

17 files changed

+512
-218
lines changed

client/config/routes.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@ module.exports = [
1111
'landing': true
1212
},
1313
}
14-
},
15-
{
14+
}, {
1615
state: 'base',
1716
abstract: true,
18-
url: '^/:userName',
17+
url: '^/:userName/',
18+
templateUrl: 'viewInstanceLayout',
1919
controller: 'ControllerApp'
20-
},
21-
{
20+
}, {
2221
state: 'instance',
2322
abstract: true,
2423
templateUrl: 'viewInstanceLayout',
2524
controller: 'ControllerInstanceLayout'
25+
}, {
26+
state: 'instance.home',
27+
abstract: false,
28+
url: '^/:userName',
29+
templateUrl: 'viewInstanceHome',
30+
controller: 'ControllerInstanceHome'
2631
}, {
2732
state: 'instance.new',
2833
abstract: false,

client/controllers/abstractLayouts/controllerInstanceLayout.js

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,59 @@ require('app')
66
function ControllerInstanceLayout(
77
configLogoutURL,
88
fetchUser,
9-
fetchOrgs,
9+
fetchInstances,
1010
$stateParams,
11-
QueryAssist,
12-
$state,
11+
errs,
1312
$rootScope,
1413
keypather,
1514
async,
1615
$scope
1716
) {
1817
var thisUser;
18+
19+
var dataInstanceLayout = $scope.dataInstanceLayout = {
20+
data: {},
21+
state: {},
22+
actions: {}
23+
};
24+
dataInstanceLayout.data.logoutURL = configLogoutURL();
1925
fetchUser(function(err, user) {
26+
if (err) { return errs.handler(err); }
2027
thisUser = user;
28+
resolveInstanceFetch(
29+
$stateParams.userName
30+
);
2131
});
2232

23-
function fetchInstances(account) {
24-
async.series([
33+
function resolveInstanceFetch(username) {
34+
if (!username) { return; }
35+
async.waterfall([
2536
function (cb) {
26-
$scope.dataInstanceLayout.state.loadingInstances = true;
37+
$rootScope.dataApp.state.loadingInstances = true;
38+
$rootScope.dataApp.data.instances = null;
2739
$rootScope.safeApply(cb);
2840
},
2941
function (cb) {
30-
new QueryAssist(thisUser, cb)
31-
.wrapFunc('fetchInstances', cb)
32-
.query({
33-
githubUsername: account
34-
})
35-
.cacheFetch(function (instances, cached, cb) {
36-
if (account === keypather.get($rootScope, 'dataApp.data.activeAccount.oauthName()')) {
37-
if ($rootScope.dataApp.data.instances !== instances) {
38-
$rootScope.dataApp.data.instances = instances;
39-
}
40-
$scope.dataInstanceLayout.state.loadingInstances = false;
41-
$rootScope.safeApply(cb);
42-
} else {
43-
cb();
44-
}
45-
})
46-
.resolve(function (err, projects, cb) {
47-
cb(err);
48-
})
49-
.go();
42+
fetchInstances(username, true, cb);
43+
},
44+
function (instances, queriedUsername, cb) {
45+
if (username === keypather.get($rootScope, 'dataApp.data.activeAccount.oauthName()')) {
46+
$rootScope.dataApp.data.instances = instances;
47+
$rootScope.dataApp.state.loadingInstances = false;
48+
$rootScope.safeApply(cb);
49+
} else {
50+
cb();
51+
}
5052
}
51-
], function (err) {
52-
if (err) { throw err; }
53-
});
53+
], errs.handler);
5454
}
55-
var dataInstanceLayout = $scope.dataInstanceLayout = {
56-
data: {},
57-
state: {},
58-
actions: {}
59-
};
60-
dataInstanceLayout.data.logoutURL = configLogoutURL();
6155

6256
var instanceListUnwatcher = $scope.$on('INSTANCE_LIST_FETCH', function(event, username) {
63-
fetchInstances(username);
57+
resolveInstanceFetch(username);
6458
});
6559

6660
$scope.$on('$destroy', function () {
6761
instanceListUnwatcher();
6862
});
6963

70-
if (keypather.get($rootScope, 'dataApp.data.activeAccount.oauthName()')) {
71-
fetchInstances(keypather.get($rootScope, 'dataApp.data.activeAccount.oauthName()'));
72-
}
73-
7464
}

client/controllers/controllerApp.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ function ControllerApp(
2929
// used in dev-info box
3030
dataApp.data.configEnvironment = configEnvironment;
3131
dataApp.data.configAPIHost = configAPIHost;
32-
3332
dataApp.data.minimizeNav = false;
3433
dataApp.data.loginURL = configLoginURL();
3534
dataApp.data.logoutURL = configLogoutURL();

client/controllers/home/controllerHome.js

Lines changed: 5 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function ControllerHome(
3838

3939
dataHome.data.hasPass = !!$location.search().password;
4040

41-
verifyUserIsAuth();
41+
$scope.goToInstance = verifyUserIsAuth;
4242

4343
function verifyUserIsAuth() {
4444
async.series([
@@ -50,131 +50,18 @@ function ControllerHome(
5050
cb();
5151
});
5252
},
53-
fetchInstances,
5453
function sendUserSomewhere(cb) {
5554

5655
var thisUser = $scope.user;
5756

58-
if (!keypather.get($localStorage, 'stateParams.instanceName')) {
59-
// no cached previously visited instance
60-
goToFirstInstance();
61-
} else {
62-
goToLastVisitedInstance();
63-
}
64-
65-
clean();
57+
$state.go('instance.home', {
58+
userName: keypather.get($localStorage, 'stateParams.userName') ||
59+
thisUser.oauthName()
60+
});
6661
return cb();
6762

68-
function getEntityName(userOrOrg) {
69-
return (thisUser === userOrOrg) ?
70-
thisUser.attrs.accounts.github.username : // user
71-
userOrOrg.attrs.login; // org
72-
}
73-
74-
// remove attached instances property from user/org models
75-
function clean() {
76-
dataHome.data.orgs.forEach(function (org) {
77-
delete org.instances;
78-
});
79-
}
80-
81-
function goToFirstInstance() {
82-
if (thisUser.instances.models.length === 0) {
83-
return goToSetup();
84-
}
85-
thisUser.instances.models = $filter('orderBy')(thisUser.instances.models, 'attrs.name');
86-
var firstInstance = thisUser.instances.models[0];
87-
var userName = thisUser.attrs.accounts.github.username;
88-
var instanceName = firstInstance.attrs.name;
89-
$state.go('instance.instance', {
90-
userName: userName,
91-
instanceName: instanceName
92-
});
93-
}
94-
95-
function goToSetup() {
96-
$state.go('instance.new', {
97-
userName: thisUser.oauthName()
98-
});
99-
return cb();
100-
}
101-
102-
function goToLastVisitedInstance() {
103-
var instanceName = $localStorage.stateParams.instanceName;
104-
var userOrgName = $localStorage.stateParams.userName;
105-
//verify exists
106-
var org = dataHome.data.orgs.find(function (org) {
107-
return getEntityName(org) === userOrgName;
108-
});
109-
if (!org) {
110-
return goToFirstInstance();
111-
}
112-
var instance = org.instances.find(function (instance) {
113-
return instance.attrs.name === instanceName;
114-
});
115-
if (!instance) {
116-
return goToFirstInstance();
117-
}
118-
// we found the cached org & instance
119-
$state.go('instance.instance', {
120-
userName: userOrgName,
121-
instanceName: instanceName
122-
});
123-
}
12463
}
12564
], errs.handler);
12665
}
12766

128-
/**
129-
* Fetch all user orgs and all instances for user + user-orgs
130-
* temporarily attach 'instances' property to user & org models
131-
*/
132-
function fetchInstances(cb) {
133-
var thisUser = $scope.user;
134-
135-
if (!keypather.get($localStorage, 'stateParams.instanceName')) {
136-
// dont bother finding all orgs, we're just going to send user to first user-instance
137-
dataHome.data.orgs = [thisUser];
138-
fetchAllInstances(dataHome.data.orgs);
139-
} else {
140-
var orgs = thisUser.fetchGithubOrgs(function (err) {
141-
if (err) { throw err; }
142-
dataHome.data.orgs = orgs.models;
143-
dataHome.data.orgs.unshift(thisUser);
144-
fetchAllInstances(dataHome.data.orgs);
145-
});
146-
}
147-
148-
function fetchAllInstances(orgs) {
149-
async.map(orgs, fetchInstancesForOrg, function (err) {
150-
if (err) { throw err; }
151-
cb();
152-
});
153-
}
154-
155-
function fetchInstancesForOrg(userOrOrg, cb) {
156-
var userId = (thisUser === userOrOrg) ?
157-
thisUser.attrs.accounts.github.id : // user
158-
userOrOrg.attrs.id; // org
159-
160-
new QueryAssist(thisUser, cb)
161-
.wrapFunc('fetchInstances')
162-
.query({
163-
owner: {
164-
github: userId
165-
}
166-
})
167-
.cacheFetch(angular.noop)
168-
.resolve(function (userOrOrg, err, instances, cb) {
169-
if (err) {
170-
cb(err);
171-
}
172-
userOrOrg.instances = instances;
173-
cb(null, instances);
174-
}.bind(this, userOrOrg))
175-
.go();
176-
}
177-
178-
}
179-
18067
}

client/controllers/instance/controllerInstance.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function ControllerInstance(
1111
OpenItems,
1212
QueryAssist,
1313
$rootScope,
14+
$localStorage,
1415
$scope,
1516
$state,
1617
$stateParams,
@@ -36,33 +37,15 @@ function ControllerInstance(
3637
// in shows/hides file-menu
3738
in: false
3839
};
39-
if (!$stateParams.instanceName) {
40-
var unwatch = $rootScope.$watch('dataApp.data.instances', function (n, p) {
41-
if (n !== p && n) {
42-
unwatch();
43-
if (n.models.length) {
44-
var models = $filter('orderBy')(n.models, 'attrs.name');
45-
$state.go('instance.instance', {
46-
instanceName: models[0].attrs.name,
47-
userName: $stateParams.userName
48-
}, {location: 'replace'});
49-
} else {
50-
$state.go('instance.new', {
51-
userName: $stateParams.userName
52-
}, {location: 'replace'});
53-
}
54-
}
55-
});
56-
} else if ($stateParams.instanceName && $stateParams.userName) {
40+
if ($stateParams.instanceName && $stateParams.userName) {
5741
async.waterfall([
5842
fetchUser,
5943
fetchInstance
6044
], function (err) {
6145
if (err) {
62-
$state.go('instance.instance', {
63-
instanceName: '',
46+
$state.go('instance.home', {
6447
userName: $stateParams.userName
65-
}, {reload: true});
48+
});
6649
}
6750
errs.handler(err);
6851
});
@@ -127,8 +110,18 @@ function ControllerInstance(
127110
.cacheFetch(function (instances, cached, cb) {
128111
data.instance = keypather.get(instances, 'models[0]');
129112
if (!data.instance) {
113+
keypather.set(
114+
$localStorage,
115+
'lastInstancePerUser.' + $stateParams.userName,
116+
null
117+
);
130118
cb(new Error('Could not find instance on server'));
131119
} else {
120+
keypather.set(
121+
$localStorage,
122+
'lastInstancePerUser.' + $stateParams.userName,
123+
$stateParams.instanceName
124+
);
132125
data.instance.state = {};
133126
$scope.safeApply();
134127
cb();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require('app')
2+
.controller('ControllerInstanceHome', ControllerInstanceHome);
3+
/**
4+
* @ngInject
5+
*/
6+
function ControllerInstanceHome(
7+
$filter,
8+
$stateParams,
9+
$state,
10+
$scope,
11+
fetchInstances,
12+
$localStorage,
13+
$rootScope,
14+
keypather
15+
) {
16+
var userName = $stateParams.userName;
17+
var instanceName = keypather.get($localStorage, 'lastInstancePerUser.' + userName);
18+
if (!instanceName) {
19+
$scope.loading = true;
20+
fetchInstances(userName, false, function(err, instances, account) {
21+
if (account === keypather.get($rootScope, 'dataApp.data.activeAccount.oauthName()')) {
22+
$scope.loading = false;
23+
var models = $filter('orderBy')(instances.models, 'attrs.name');
24+
var name = keypather.get(models, '[0].attrs.name');
25+
goToInstance(userName, name);
26+
}
27+
});
28+
} else {
29+
goToInstance(userName, instanceName);
30+
}
31+
function goToInstance(username, instanceName) {
32+
if (instanceName) {
33+
$state.go('instance.instance', {
34+
instanceName: instanceName,
35+
userName: username
36+
}, {location: 'replace'});
37+
} else {
38+
$state.go('instance.new', {
39+
userName: username
40+
}, {location: 'replace'});
41+
}
42+
}
43+
44+
}

0 commit comments

Comments
 (0)