Skip to content

Commit 44f1a39

Browse files
authored
Merge pull request #2025 from angular-fullstack/refactor/client-types
Refactor/client types
2 parents 6a9b25a + 60d3bb2 commit 44f1a39

File tree

8 files changed

+137
-46
lines changed

8 files changed

+137
-46
lines changed

Diff for: templates/app/client/app/account(auth)/login/login(html).html

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h1>Login</h1>
2828
Please enter a valid email.
2929
</p>
3030

31-
<p class="help-block">{{ vm.errors.other }}</p>
31+
<p class="help-block">{{ vm.errors.login }}</p>
3232
</div>
3333

3434
<div>

Diff for: templates/app/client/app/account(auth)/login/login(jade).jade

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
.form-group.has-error
2828
p.help-block(ng-show='form.email.$error.required && form.password.$error.required && vm.submitted')
2929
| Please enter your email and password.
30-
p.help-block {{ vm.errors.other }}
30+
p.help-block {{ vm.errors.login }}
3131

3232
div
3333
button.btn.btn-inverse.btn-lg.btn-login(type='submit')

Diff for: templates/app/client/app/account(auth)/login/login.controller.js

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
'use strict';
2+
// @flow
3+
<%_ if(filters.flow) { -%>
4+
type User = {
5+
name: string;
6+
email: string;
7+
password: string;
8+
};
9+
<%_ } -%>
10+
<%_ if(filters.ts) { -%>
11+
interface User {
12+
name: string;
13+
email: string;
14+
password: string;
15+
}
16+
<%_ } -%>
217

318
export default class LoginController {
19+
user: User = {
20+
name: '',
21+
email: '',
22+
password: ''
23+
};
24+
errors = {login: undefined};
25+
submitted = false;
26+
Auth;
27+
<%_ if(filters.ngroute) { -%>
28+
$location;
29+
<%_ } if(filters.uirouter) { -%>
30+
$state;<% } %>
31+
432
/*@ngInject*/
533
constructor(Auth<% if (filters.ngroute) { %>, $location<% } %><% if (filters.uirouter) { %>, $state<% } %>) {
6-
this.user = {};
7-
this.errors = {};
8-
this.submitted = false;
9-
10-
this.Auth = Auth;<% if (filters.ngroute) { %>
11-
this.$location = $location;<% } if (filters.uirouter) { %>
34+
this.Auth = Auth;
35+
<%_ if(filters.ngroute) { -%>
36+
this.$location = $location;
37+
<%_ } if(filters.uirouter) { -%>
1238
this.$state = $state;<% } %>
1339
}
1440

@@ -25,7 +51,7 @@ export default class LoginController {
2551
<% if (filters.ngroute) { %>this.$location.path('/');<% } %><% if (filters.uirouter) { %>this.$state.go('main');<% } %>
2652
})
2753
.catch(err => {
28-
this.errors.other = err.message;
54+
this.errors.login = err.message;
2955
});
3056
}
3157
}

Diff for: templates/app/client/app/account(auth)/settings/settings.controller.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
'use strict';
2+
// @flow
3+
<%_ if(filters.flow) { -%>
4+
type User = {
5+
oldPassword: string;
6+
newPassword: string;
7+
confirmPassword: string;
8+
};
9+
<%_ } -%>
10+
<%_ if(filters.ts) { -%>
11+
interface User {
12+
oldPassword: string;
13+
newPassword: string;
14+
confirmPassword: string;
15+
}
16+
<%_ } -%>
217

318
export default class SettingsController {
4-
errors = {};
19+
user: User = {
20+
oldPassword: '',
21+
newPassword: '',
22+
confirmPassword: ''
23+
};
24+
errors = {other: undefined};
25+
message = '';
526
submitted = false;
27+
Auth;
628

729
/*@ngInject*/
830
constructor(Auth) {
@@ -12,7 +34,7 @@ export default class SettingsController {
1234
changePassword(form) {
1335
this.submitted = true;
1436

15-
if (form.$valid) {
37+
if(form.$valid) {
1638
this.Auth.changePassword(this.user.oldPassword, this.user.newPassword)
1739
.then(() => {
1840
this.message = 'Password successfully changed.';

Diff for: templates/app/client/app/account(auth)/signup/signup.controller.js

+37-12
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,69 @@
11
'use strict';
2+
// @flow
3+
<%_ if(filters.flow) { -%>
4+
type User = {
5+
name: string;
6+
email: string;
7+
password: string;
8+
};
9+
<%_ } -%>
10+
<%_ if(filters.ts) { -%>
11+
interface User {
12+
name: string;
13+
email: string;
14+
password: string;
15+
}
16+
<%_ } -%>
217

318
export default class SignupController {
4-
//start-non-standard
5-
user = {};
19+
user: User = {
20+
name: '',
21+
email: '',
22+
password: ''
23+
};
624
errors = {};
725
submitted = false;
8-
//end-non-standard
26+
Auth;
27+
<%_ if(filters.ngroute) { -%>
28+
$location;
29+
<%_ } if(filters.uirouter) { -%>
30+
$state;<% } %>
931

1032
/*@ngInject*/
1133
constructor(Auth<% if (filters.ngroute) { %>, $location<% } %><% if (filters.uirouter) { %>, $state<% } %>) {
12-
this.Auth = Auth;<% if (filters.ngroute) { %>
13-
this.$location = $location;<% } if (filters.uirouter) { %>
34+
this.Auth = Auth;
35+
<%_ if(filters.ngroute) { -%>
36+
this.$location = $location;
37+
<%_ } if(filters.uirouter) { -%>
1438
this.$state = $state;<% } %>
1539
}
1640

1741
register(form) {
1842
this.submitted = true;
1943

20-
if (form.$valid) {
21-
this.Auth.createUser({
44+
if(form.$valid) {
45+
return this.Auth.createUser({
2246
name: this.user.name,
2347
email: this.user.email,
2448
password: this.user.password
2549
})
2650
.then(() => {
2751
// Account created, redirect to home
28-
<% if (filters.ngroute) { %>this.$location.path('/');<% } %><% if (filters.uirouter) { %>this.$state.go('main');<% } %>
52+
<% if(filters.ngroute) { %>this.$location.path('/');<% } -%>
53+
<% if(filters.uirouter) { %>this.$state.go('main');<% } -%>
2954
})
3055
.catch(err => {
3156
err = err.data;
3257
this.errors = {};
33-
<% if (filters.mongooseModels) { %>
58+
<%_ if(filters.mongooseModels) { -%>
3459
// Update validity of form fields that match the mongoose errors
3560
angular.forEach(err.errors, (error, field) => {
3661
form[field].$setValidity('mongoose', false);
3762
this.errors[field] = error.message;
38-
});<% }
39-
if (filters.sequelizeModels) { %>
63+
});<% } %>
64+
<%_ if(filters.sequelizeModels) { -%>
4065
// Update validity of form fields that match the sequelize errors
41-
if (err.name) {
66+
if(err.name) {
4267
angular.forEach(err.fields, field => {
4368
form[field].$setValidity('mongoose', false);
4469
this.errors[field] = err.message;

Diff for: templates/app/client/app/main/main.component.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ import uiRouter from 'angular-ui-router';<% } _%>
77
import routing from './main.routes';
88

99
export class MainController {
10+
$http;
11+
<%_ if(filters.socketio) { -%>
12+
socket;<% } %>
13+
awesomeThings = [];
14+
<%_ if(filters.models) { -%>
15+
newThing = '';<% } %>
16+
1017
/*@ngInject*/
1118
constructor($http<% if(filters.socketio) { %>, $scope, socket<% } %>) {
12-
this.$http = $http;<% if (filters.socketio) { %>
13-
this.socket = socket;<% } %>
14-
this.awesomeThings = [];
15-
<%_ if (filters.socketio) { _%>
19+
this.$http = $http;
20+
<%_ if(filters.socketio) { -%>
21+
this.socket = socket;
1622

1723
$scope.$on('$destroy', function() {
1824
socket.unsyncUpdates('thing');

Diff for: templates/app/client/components/auth(auth)/auth.service.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
'use strict';
22
// @flow
3+
class User {
4+
_id: string = '';
5+
name: string = '';
6+
email: string = '';
7+
role: string = '';
8+
$promise = undefined;
9+
}
310

411
export function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
512
'ngInject';
613
var safeCb = Util.safeCb;
7-
var currentUser = {};
14+
var currentUser: User = new User();
815
var userRoles = appConfig.userRoles || [];
916
/**
1017
* Check if userRole is >= role
@@ -25,10 +32,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
2532
* Authenticate user and save token
2633
*
2734
* @param {Object} user - login info
28-
* @param {Function} callback - optional, function(error, user)
35+
* @param {Function} callback - function(error, user)
2936
* @return {Promise}
3037
*/
31-
login({email, password}, callback: Function) {
38+
login({email, password}, callback?: Function) {
3239
return $http.post('/auth/local', { email, password })
3340
.then(res => {
3441
$cookies.put('token', res.data.token);
@@ -51,17 +58,17 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
5158
*/
5259
logout() {
5360
$cookies.remove('token');
54-
currentUser = {};
61+
currentUser = new User();
5562
},
5663

5764
/**
5865
* Create a new user
5966
*
6067
* @param {Object} user - user info
61-
* @param {Function} callback - optional, function(error, user)
68+
* @param {Function} callback - function(error, user)
6269
* @return {Promise}
6370
*/
64-
createUser(user, callback) {
71+
createUser(user, callback?: Function) {
6572
return User.save(user,
6673
function(data) {
6774
$cookies.put('token', data.token);
@@ -79,10 +86,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
7986
*
8087
* @param {String} oldPassword
8188
* @param {String} newPassword
82-
* @param {Function} callback - optional, function(error, user)
89+
* @param {Function} callback - function(error, user)
8390
* @return {Promise}
8491
*/
85-
changePassword(oldPassword, newPassword, callback) {
92+
changePassword(oldPassword, newPassword, callback?: Function) {
8693
return User.changePassword({ id: currentUser._id }, { oldPassword, newPassword }, function() {
8794
return safeCb(callback)(null);
8895
}, function(err) {
@@ -93,10 +100,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
93100
/**
94101
* Gets all available info on a user
95102
*
96-
* @param {Function} [callback] - funciton(user)
103+
* @param {Function} [callback] - function(user)
97104
* @return {Promise}
98105
*/
99-
getCurrentUser(callback) {
106+
getCurrentUser(callback?: Function) {
100107
var value = currentUser.hasOwnProperty('$promise')
101108
? currentUser.$promise
102109
: currentUser;
@@ -124,10 +131,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
124131
* Check if a user is logged in
125132
*
126133
* @param {Function} [callback] - function(is)
127-
* @return {Bool|Promise}
134+
* @return {Promise}
128135
*/
129-
isLoggedIn(callback) {
130-
return Auth.getCurrentUser()
136+
isLoggedIn(callback?: Function) {
137+
return Auth.getCurrentUser(undefined)
131138
.then(user => {
132139
var is = user.hasOwnProperty('role');
133140
safeCb(callback)(is);
@@ -149,10 +156,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
149156
*
150157
* @param {String} role - the role to check against
151158
* @param {Function} [callback] - function(has)
152-
* @return {Bool|Promise}
159+
* @return {Promise}
153160
*/
154-
hasRole(role, callback) {
155-
return Auth.getCurrentUser()
161+
hasRole(role, callback?: Function) {
162+
return Auth.getCurrentUser(undefined)
156163
.then(user => {
157164
var has = user.hasOwnProperty('role')
158165
? hasRole(user.role, role)

Diff for: templates/app/client/components/navbar/navbar.component.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
'use strict';
22

33
export class NavbarComponent {
4-
//start-non-standard
54
menu = [{
65
'title': 'Home',
76
<% if (filters.uirouter) { %>'state': 'main'<% } else { %>'link': '/'<% } %>
87
}];
9-
8+
<%_ if(!filters.uirouter) { -%>
9+
$location;
10+
<%_ } -%>
11+
<%_ if (filters.auth) { -%>
12+
isLoggedIn: Function;
13+
isAdmin: Function;
14+
getCurrentUser: Function;
15+
<%_ } -%>
1016
isCollapsed = true;
11-
//end-non-standard
1217
<%_ if(filters.ngroute || filters.auth) { _%>
1318

1419
constructor(<% if(!filters.uirouter) { %>$location<% } if(!filters.uirouter && filters.auth) { %>, <% } if (filters.auth) { %>Auth<% } %>) {

0 commit comments

Comments
 (0)