Skip to content

Commit 623f883

Browse files
committed
This has a lot of fixes for the url routing, first instance checking, and even using the local storage to record the last instance.
1 parent 61a2f2b commit 623f883

11 files changed

Lines changed: 126 additions & 183 deletions

File tree

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: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,38 @@ require('app')
66
function ControllerInstanceLayout(
77
configLogoutURL,
88
fetchUser,
9-
fetchOrgs,
109
$stateParams,
1110
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) {
27+
return errs.handler(err);
28+
}
2029
thisUser = user;
30+
fetchInstances(
31+
$stateParams.userName
32+
);
2133
});
2234

23-
function fetchInstances(account) {
35+
function fetchInstances(account, cb) {
36+
if (!account) { return; }
2437
async.series([
2538
function (cb) {
26-
$scope.dataInstanceLayout.state.loadingInstances = true;
39+
$rootScope.dataApp.state.loadingInstances = true;
40+
$rootScope.dataApp.data.instances = null;
2741
$rootScope.safeApply(cb);
2842
},
2943
function (cb) {
@@ -37,7 +51,7 @@ function ControllerInstanceLayout(
3751
if ($rootScope.dataApp.data.instances !== instances) {
3852
$rootScope.dataApp.data.instances = instances;
3953
}
40-
$scope.dataInstanceLayout.state.loadingInstances = false;
54+
$rootScope.dataApp.state.loadingInstances = false;
4155
$rootScope.safeApply(cb);
4256
} else {
4357
cb();
@@ -47,17 +61,12 @@ function ControllerInstanceLayout(
4761
cb(err);
4862
})
4963
.go();
50-
}
64+
},
65+
cb
5166
], function (err) {
5267
if (err) { throw err; }
5368
});
5469
}
55-
var dataInstanceLayout = $scope.dataInstanceLayout = {
56-
data: {},
57-
state: {},
58-
actions: {}
59-
};
60-
dataInstanceLayout.data.logoutURL = configLogoutURL();
6170

6271
var instanceListUnwatcher = $scope.$on('INSTANCE_LIST_FETCH', function(event, username) {
6372
fetchInstances(username);
@@ -67,8 +76,4 @@ function ControllerInstanceLayout(
6776
instanceListUnwatcher();
6877
});
6978

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

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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require('app')
2+
.controller('ControllerInstanceHome', ControllerInstanceHome);
3+
/**
4+
* @ngInject
5+
*/
6+
function ControllerInstanceHome(
7+
$filter,
8+
$stateParams,
9+
$state,
10+
$scope,
11+
$localStorage,
12+
$rootScope,
13+
keypather
14+
) {
15+
var userName = $stateParams.userName;
16+
var instanceName = keypather.get($localStorage, 'lastInstancePerUser.' + userName);
17+
if (!instanceName) {
18+
$scope.loading = true;
19+
var unwatch = $rootScope.$watch('dataApp.data.instances', function(n) {
20+
if (n) {
21+
unwatch();
22+
if (userName === $rootScope.dataApp.data.activeAccount.oauthName()) {
23+
$scope.loading = false;
24+
var models = $filter('orderBy')(n.models, 'attrs.name');
25+
var name = keypather.get(n, 'models[0].attrs.name');
26+
goToInstance(userName, name);
27+
}
28+
}
29+
});
30+
} else {
31+
goToInstance(userName, instanceName);
32+
}
33+
function goToInstance(username, instanceName) {
34+
if (instanceName) {
35+
$state.go('instance.instance', {
36+
instanceName: instanceName,
37+
userName: username
38+
}, {location: 'replace'});
39+
} else {
40+
$state.go('instance.new', {
41+
userName: username
42+
}, {location: 'replace'});
43+
}
44+
}
45+
46+
}

client/directives/accountsSelect/directiveAccountsSelect.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ require('app')
77
* @ngInject
88
*/
99
function accountsSelect (
10-
$state
10+
$state,
11+
$rootScope
1112
) {
1213
return {
1314
restrict: 'E',
@@ -33,10 +34,11 @@ function accountsSelect (
3334
var username = userOrOrg.oauthName();
3435
//
3536
$scope.data.activeAccount = userOrOrg;
37+
$scope.data.instances = null;
38+
$rootScope.safeApply();
3639
$scope.$emit('INSTANCE_LIST_FETCH', username);
37-
$state.go('^.instance', {
38-
userName: username,
39-
instanceName: ''
40+
$state.go('^.home', {
41+
userName: username
4042
});
4143
};
4244
}

0 commit comments

Comments
 (0)