Skip to content

Refactor/client types #2025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ <h1>Login</h1>
Please enter a valid email.
</p>

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

<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
.form-group.has-error
p.help-block(ng-show='form.email.$error.required && form.password.$error.required && vm.submitted')
| Please enter your email and password.
p.help-block {{ vm.errors.other }}
p.help-block {{ vm.errors.login }}

div
button.btn.btn-inverse.btn-lg.btn-login(type='submit')
Expand Down
40 changes: 33 additions & 7 deletions templates/app/client/app/account(auth)/login/login.controller.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
'use strict';
// @flow
<%_ if(filters.flow) { -%>
type User = {
name: string;
email: string;
password: string;
};
<%_ } -%>
<%_ if(filters.ts) { -%>
interface User {
name: string;
email: string;
password: string;
}
<%_ } -%>

export default class LoginController {
user: User = {
name: '',
email: '',
password: ''
};
errors = {login: undefined};
submitted = false;
Auth;
<%_ if(filters.ngroute) { -%>
$location;
<%_ } if(filters.uirouter) { -%>
$state;<% } %>

/*@ngInject*/
constructor(Auth<% if (filters.ngroute) { %>, $location<% } %><% if (filters.uirouter) { %>, $state<% } %>) {
this.user = {};
this.errors = {};
this.submitted = false;

this.Auth = Auth;<% if (filters.ngroute) { %>
this.$location = $location;<% } if (filters.uirouter) { %>
this.Auth = Auth;
<%_ if(filters.ngroute) { -%>
this.$location = $location;
<%_ } if(filters.uirouter) { -%>
this.$state = $state;<% } %>
}

Expand All @@ -25,7 +51,7 @@ export default class LoginController {
<% if (filters.ngroute) { %>this.$location.path('/');<% } %><% if (filters.uirouter) { %>this.$state.go('main');<% } %>
})
.catch(err => {
this.errors.other = err.message;
this.errors.login = err.message;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
'use strict';
// @flow
<%_ if(filters.flow) { -%>
type User = {
oldPassword: string;
newPassword: string;
confirmPassword: string;
};
<%_ } -%>
<%_ if(filters.ts) { -%>
interface User {
oldPassword: string;
newPassword: string;
confirmPassword: string;
}
<%_ } -%>

export default class SettingsController {
errors = {};
user: User = {
oldPassword: '',
newPassword: '',
confirmPassword: ''
};
errors = {other: undefined};
message = '';
submitted = false;
Auth;

/*@ngInject*/
constructor(Auth) {
Expand All @@ -12,7 +34,7 @@ export default class SettingsController {
changePassword(form) {
this.submitted = true;

if (form.$valid) {
if(form.$valid) {
this.Auth.changePassword(this.user.oldPassword, this.user.newPassword)
.then(() => {
this.message = 'Password successfully changed.';
Expand Down
49 changes: 37 additions & 12 deletions templates/app/client/app/account(auth)/signup/signup.controller.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,69 @@
'use strict';
// @flow
<%_ if(filters.flow) { -%>
type User = {
name: string;
email: string;
password: string;
};
<%_ } -%>
<%_ if(filters.ts) { -%>
interface User {
name: string;
email: string;
password: string;
}
<%_ } -%>

export default class SignupController {
//start-non-standard
user = {};
user: User = {
name: '',
email: '',
password: ''
};
errors = {};
submitted = false;
//end-non-standard
Auth;
<%_ if(filters.ngroute) { -%>
$location;
<%_ } if(filters.uirouter) { -%>
$state;<% } %>

/*@ngInject*/
constructor(Auth<% if (filters.ngroute) { %>, $location<% } %><% if (filters.uirouter) { %>, $state<% } %>) {
this.Auth = Auth;<% if (filters.ngroute) { %>
this.$location = $location;<% } if (filters.uirouter) { %>
this.Auth = Auth;
<%_ if(filters.ngroute) { -%>
this.$location = $location;
<%_ } if(filters.uirouter) { -%>
this.$state = $state;<% } %>
}

register(form) {
this.submitted = true;

if (form.$valid) {
this.Auth.createUser({
if(form.$valid) {
return this.Auth.createUser({
name: this.user.name,
email: this.user.email,
password: this.user.password
})
.then(() => {
// Account created, redirect to home
<% if (filters.ngroute) { %>this.$location.path('/');<% } %><% if (filters.uirouter) { %>this.$state.go('main');<% } %>
<% if(filters.ngroute) { %>this.$location.path('/');<% } -%>
<% if(filters.uirouter) { %>this.$state.go('main');<% } -%>
})
.catch(err => {
err = err.data;
this.errors = {};
<% if (filters.mongooseModels) { %>
<%_ if(filters.mongooseModels) { -%>
// Update validity of form fields that match the mongoose errors
angular.forEach(err.errors, (error, field) => {
form[field].$setValidity('mongoose', false);
this.errors[field] = error.message;
});<% }
if (filters.sequelizeModels) { %>
});<% } %>
<%_ if(filters.sequelizeModels) { -%>
// Update validity of form fields that match the sequelize errors
if (err.name) {
if(err.name) {
angular.forEach(err.fields, field => {
form[field].$setValidity('mongoose', false);
this.errors[field] = err.message;
Expand Down
14 changes: 10 additions & 4 deletions templates/app/client/app/main/main.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import uiRouter from 'angular-ui-router';<% } _%>
import routing from './main.routes';

export class MainController {
$http;
<%_ if(filters.socketio) { -%>
socket;<% } %>
awesomeThings = [];
<%_ if(filters.models) { -%>
newThing = '';<% } %>

/*@ngInject*/
constructor($http<% if(filters.socketio) { %>, $scope, socket<% } %>) {
this.$http = $http;<% if (filters.socketio) { %>
this.socket = socket;<% } %>
this.awesomeThings = [];
<%_ if (filters.socketio) { _%>
this.$http = $http;
<%_ if(filters.socketio) { -%>
this.socket = socket;

$scope.$on('$destroy', function() {
socket.unsyncUpdates('thing');
Expand Down
39 changes: 23 additions & 16 deletions templates/app/client/components/auth(auth)/auth.service.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
'use strict';
// @flow
class User {
_id: string = '';
name: string = '';
email: string = '';
role: string = '';
$promise = undefined;
}

export function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
'ngInject';
var safeCb = Util.safeCb;
var currentUser = {};
var currentUser: User = new User();
var userRoles = appConfig.userRoles || [];
/**
* Check if userRole is >= role
Expand All @@ -25,10 +32,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
* Authenticate user and save token
*
* @param {Object} user - login info
* @param {Function} callback - optional, function(error, user)
* @param {Function} callback - function(error, user)
* @return {Promise}
*/
login({email, password}, callback: Function) {
login({email, password}, callback?: Function) {
return $http.post('/auth/local', { email, password })
.then(res => {
$cookies.put('token', res.data.token);
Expand All @@ -51,17 +58,17 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
*/
logout() {
$cookies.remove('token');
currentUser = {};
currentUser = new User();
},

/**
* Create a new user
*
* @param {Object} user - user info
* @param {Function} callback - optional, function(error, user)
* @param {Function} callback - function(error, user)
* @return {Promise}
*/
createUser(user, callback) {
createUser(user, callback?: Function) {
return User.save(user,
function(data) {
$cookies.put('token', data.token);
Expand All @@ -79,10 +86,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
*
* @param {String} oldPassword
* @param {String} newPassword
* @param {Function} callback - optional, function(error, user)
* @param {Function} callback - function(error, user)
* @return {Promise}
*/
changePassword(oldPassword, newPassword, callback) {
changePassword(oldPassword, newPassword, callback?: Function) {
return User.changePassword({ id: currentUser._id }, { oldPassword, newPassword }, function() {
return safeCb(callback)(null);
}, function(err) {
Expand All @@ -93,10 +100,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
/**
* Gets all available info on a user
*
* @param {Function} [callback] - funciton(user)
* @param {Function} [callback] - function(user)
* @return {Promise}
*/
getCurrentUser(callback) {
getCurrentUser(callback?: Function) {
var value = currentUser.hasOwnProperty('$promise')
? currentUser.$promise
: currentUser;
Expand Down Expand Up @@ -124,10 +131,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
* Check if a user is logged in
*
* @param {Function} [callback] - function(is)
* @return {Bool|Promise}
* @return {Promise}
*/
isLoggedIn(callback) {
return Auth.getCurrentUser()
isLoggedIn(callback?: Function) {
return Auth.getCurrentUser(undefined)
.then(user => {
var is = user.hasOwnProperty('role');
safeCb(callback)(is);
Expand All @@ -149,10 +156,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
*
* @param {String} role - the role to check against
* @param {Function} [callback] - function(has)
* @return {Bool|Promise}
* @return {Promise}
*/
hasRole(role, callback) {
return Auth.getCurrentUser()
hasRole(role, callback?: Function) {
return Auth.getCurrentUser(undefined)
.then(user => {
var has = user.hasOwnProperty('role')
? hasRole(user.role, role)
Expand Down
11 changes: 8 additions & 3 deletions templates/app/client/components/navbar/navbar.component.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
'use strict';

export class NavbarComponent {
//start-non-standard
menu = [{
'title': 'Home',
<% if (filters.uirouter) { %>'state': 'main'<% } else { %>'link': '/'<% } %>
}];

<%_ if(!filters.uirouter) { -%>
$location;
<%_ } -%>
<%_ if (filters.auth) { -%>
isLoggedIn: Function;
isAdmin: Function;
getCurrentUser: Function;
<%_ } -%>
isCollapsed = true;
//end-non-standard
<%_ if(filters.ngroute || filters.auth) { _%>

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