Commit 571ba9fd authored by Evren Kutar's avatar Evren Kutar

form service kullanımı ve url değişikliği

interceptor'den sayfayı ayıklama
parent d1e16543
...@@ -39,11 +39,10 @@ var app = angular.module( ...@@ -39,11 +39,10 @@ var app = angular.module(
* Based on the environment it changes from dev to prod * Based on the environment it changes from dev to prod
*/ */
constant("RESTURL", (function () { constant("RESTURL", (function () {
var dev = "http://127.0.0.1:3000/api/"; var dev = "http://127.0.0.1:8000/";
var prod = ""; var prod = "";
var ENV = "dev"; // change to prod in production var ENV = "dev"; // change to prod in production
return ENV == "dev" ? {url: dev} : {url: prod}; return ENV == "dev" ? {url: dev} : {url: prod};
//return "http://127.0.0.1:3000/api/";
})()). })()).
/** /**
* USER_ROLES and AUTH_EVENTS are constant for auth functions * USER_ROLES and AUTH_EVENTS are constant for auth functions
......
...@@ -9,6 +9,12 @@ app.config(['$routeProvider', function ($routeProvider) { ...@@ -9,6 +9,12 @@ app.config(['$routeProvider', function ($routeProvider) {
}], }],
loadMyService: ['$ocLazyLoad', function ($ocLazyLoad) { loadMyService: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load('components/auth/auth_service.js'); return $ocLazyLoad.load('components/auth/auth_service.js');
}],
loadMyService2: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load('zetalib/forms/form_service.js');
}],
loadMyService3: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load('zetalib/general.js');
}] }]
} }
}) })
......
...@@ -12,73 +12,92 @@ ...@@ -12,73 +12,92 @@
// TODO: who field can be removed?? // TODO: who field can be removed??
// todo: use zetalib/forms/form_service functions for email validation // todo: use zetalib/forms/form_service functions for email validation
var auth = angular.module('zaerp.auth', ['ngRoute', 'schemaForm', 'ngCookies']); var auth = angular.module('zaerp.auth', ['ngRoute', 'schemaForm', 'ngCookies', 'general']);
auth.controller('LoginCtrl', function ($scope, $q, $timeout, LoginService) { auth.controller('LoginCtrl', function ($scope, $q, $timeout, $routeParams, Generator, LoginService) {
$scope.schema = $scope.url = 'simple_login';
{
title: "Login", // todo: change simple login when api ready
type: "object", Generator.get_form($scope.url, $routeParams).then(function(d){
properties: { $scope.schema = d.schema;
email: { $scope.form = d.form;
type: "email", // model is the init data of the form or in edit templates
title: "Email" $scope.model = d.model ? d.model : {};
}, $scope.initialModel = angular.copy(d.model);
password: { // for email validation add asyncvalidator
type: "string", //$scope.form[0].$asyncValidators = Generator.asyncValidators;
title: "Password" // add submit button to the form todo: move this to form service
}, $scope.form.push(
remember: { {
type: "boolean", type: "submit",
title: "Remember me?" title: "Save"
},
who: {
title: "Who are you?",
type: "string",
enum: ["student", "stuff", "dean"]
}
},
required: ["email", "password", "who"]
};
$scope.model = {
email: "user@example.com",
remember: false
};
$scope.form = [
{
key: "email",
type: "email",
validationMessages: {
'emailNotValid': 'Email is not valid!'
},
$asyncValidators: {
emailNotValid: function (value) {
var deferred = $q.defer();
$timeout(function () {
if (LoginService.isValidEmail(value)) {
deferred.resolve();
} else {
deferred.reject();
}
}, 500);
return deferred.promise;
}
} }
}, );
{ });
key: "password", //$scope.schema =
type: "password" //{
}, // title: "Login",
"remember", // type: "object",
"who", // properties: {
{ // email: {
type: "submit", // type: "email",
title: "Save" // title: "Email"
} // },
]; // password: {
// type: "string",
// title: "Password"
// },
// remember: {
// type: "boolean",
// title: "Remember me?"
// },
// who: {
// title: "Who are you?",
// type: "string",
// enum: ["student", "stuff", "dean"]
// }
// },
// required: ["email", "password", "who"]
//};
//$scope.model = {
// email: "user@example.com",
// remember: false
//};
//$scope.form = [
// {
// key: "email",
// type: "email",
// validationMessages: {
// 'emailNotValid': 'Email is not valid!'
// },
// $asyncValidators: {
// emailNotValid: function (value) {
// var deferred = $q.defer();
// $timeout(function () {
// if (LoginService.isValidEmail(value)) {
// deferred.resolve();
// } else {
// deferred.reject();
// }
// }, 500);
// return deferred.promise;
// }
// }
// },
// {
// key: "password",
// type: "password"
// },
// "remember",
// "who",
// {
// type: "submit",
// title: "Save"
// }
//];
$scope.onSubmit = function (form) { $scope.onSubmit = function (form) {
$scope.$broadcast('schemaFormValidate'); $scope.$broadcast('schemaFormValidate');
if (form.$valid) { if (form.$valid) {
LoginService.login($scope.model); LoginService.login($scope.url, $scope.model);
} }
else { else {
console.log("not valid"); console.log("not valid");
......
...@@ -12,14 +12,14 @@ ...@@ -12,14 +12,14 @@
auth.factory('LoginService', function ($http, $rootScope, $location, $log, $cookies, Session, RESTURL) { auth.factory('LoginService', function ($http, $rootScope, $location, $log, $cookies, Session, RESTURL) {
var loginService = {}; var loginService = {};
loginService.login = function (credentials) { loginService.login = function (url, credentials) {
// TODO: change this getParams var to service to use app-wide // TODO: change this getParams var to service to use app-wide
var getParams = "?"; var getParams = "?";
for (var k in credentials) { for (var k in credentials) {
getParams += k + "=" + credentials[k] + "&"; getParams += k + "=" + credentials[k] + "&";
} }
return $http return $http
.get(RESTURL.url + 'login' + getParams) .get(RESTURL.url + url + getParams)
.then(function (res) { .then(function (res) {
$log.info(res.data[0]); $log.info(res.data[0]);
res.data = res.data[0]; res.data = res.data[0];
......
...@@ -60,5 +60,20 @@ describe('form service module', function () { ...@@ -60,5 +60,20 @@ describe('form service module', function () {
}) })
); );
it('should post form',
inject(function (Generator, $httpBackend, RESTURL) {
$httpBackend.expectGET(RESTURL.url + 'student/add')
.respond(200, [{data: 'OK'}]);
var cred = {email: 'test@test.com'};
Generator.submit('student/add', cred)
.then(function (data) {
expect(data).toEqual({data: 'OK'});
});
$httpBackend.flush();
})
);
}); });
}); });
\ No newline at end of file
...@@ -22,6 +22,9 @@ app.config(['$httpProvider', function ($httpProvider) { ...@@ -22,6 +22,9 @@ app.config(['$httpProvider', function ($httpProvider) {
}, },
'response': function (response) { 'response': function (response) {
//Will only be called for HTTP up to 300 //Will only be called for HTTP up to 300
if(response.screen) {
location.path(response.screen);
}
return response; return response;
}, },
'responseError': function (rejection) { 'responseError': function (rejection) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment