From 9e9dd40af4427b1001d4a64b827c4f65db03c45d Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Mon, 25 May 2015 16:54:24 -0700 Subject: [PATCH] save a user's last state to show them when they log in next --- config.js | 23 ++++++++++++++--------- scripts/FeedController.js | 13 ------------- scripts/FlashcardDirective.js | 1 - scripts/LoginController.js | 21 +++++++++++---------- scripts/RootController.js | 17 +++++++++++++---- scripts/UserService.js | 25 ++++++++++++++++--------- 6 files changed, 54 insertions(+), 46 deletions(-) diff --git a/config.js b/config.js index 5ee3b4e..bf84695 100644 --- a/config.js +++ b/config.js @@ -13,7 +13,7 @@ angular.module('flashy', [ 'flashy.VerifyEmailController', 'flashy.CardListController', 'ngCookies']). - config(function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider) { + config(function ($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider) { 'use strict'; $httpProvider.defaults.withCredentials = true; $httpProvider.defaults.xsrfCookieName = 'csrftoken'; @@ -27,9 +27,9 @@ angular.module('flashy', [ } }; - $httpProvider.interceptors.push(function($q) { + $httpProvider.interceptors.push(function ($q) { return { - 'responseError': function(rejection) { + 'responseError': function (rejection) { if (rejection.status >= 500) { console.log('got error'); console.log(rejection); @@ -42,9 +42,9 @@ angular.module('flashy', [ $locationProvider.html5Mode(true); $urlRouterProvider.otherwise('/404'); var auth_resolve = { - authorize: function($q, $state, UserService) { + authorize: function ($q, $state, UserService) { console.log('resolving user before continuing'); - var redirectAsNeeded = function() { + var redirectAsNeeded = function () { if (!UserService.isLoggedIn()) { console.log(UserService.getUserData()); console.log('making the user log in'); @@ -69,8 +69,7 @@ angular.module('flashy', [ }). state('root', { resolve: auth_resolve, - url: '', - templateUrl: 'templates/root.html', + url: '/', controller: 'RootController' }). state('feed', { @@ -130,9 +129,15 @@ angular.module('flashy', [ template: "

This page doesn't exist!

" }); }). - run(function($rootScope, $state, $stateParams, $location, UserService) { - $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) { + run(function ($rootScope, $state, $stateParams, $location, UserService) { + $rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) { console.log('failed to change state: ' + error); $state.go('login'); }); + $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { + if (['feed', 'deck', 'cardlist'].indexOf(toState.name) >= 0) { + localStorage.setItem('last_state', toState.name); + localStorage.setItem('last_state_params', JSON.stringify(toParams)); + } + }); }); diff --git a/scripts/FeedController.js b/scripts/FeedController.js index 9481f26..453c8fc 100644 --- a/scripts/FeedController.js +++ b/scripts/FeedController.js @@ -1,6 +1,5 @@ angular.module('flashy.FeedController', ['ui.router']). controller('FeedController', function($scope, $rootScope, $stateParams, $state, $http, UserService) { - console.log('Hello from feed'); sectionId = $stateParams.sectionId; $rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId}); $scope.cards = false; @@ -35,7 +34,6 @@ angular.module('flashy.FeedController', ['ui.router']). success(function(data) { console.log(data); $scope.cards = data; - }). error(function(err) { console.log('pulling feed failed'); @@ -59,7 +57,6 @@ angular.module('flashy.FeedController', ['ui.router']). i += node.data.length; } }); - console.log(blanks); text = $('#new-card-input').text(); var myCard = { 'text': text, @@ -79,16 +76,6 @@ angular.module('flashy.FeedController', ['ui.router']). listenForC = true; $('#new-card-input').html(''); }; - - $http.get('/api/sections/' + sectionId + '/feed/'). - success(function(data) { - console.log(data); - $scope.cards = data; - }). - error(function(err) { - console.log("couldn't get cards!"); - }); - /* Key bindings for the whole feed window. Hotkey it up! */ var listenForC = true; diff --git a/scripts/FlashcardDirective.js b/scripts/FlashcardDirective.js index 50c75d8..32e78ae 100644 --- a/scripts/FlashcardDirective.js +++ b/scripts/FlashcardDirective.js @@ -23,7 +23,6 @@ angular.module('flashy.FlashcardDirective', []). angular.element($window).bind('resize', refresh_width); refresh_width(); scope.textPieces = []; - console.log(scope.flashcard); scope.flashcard.mask.sort(function(a, b) { return a[0] - b[0]; }); diff --git a/scripts/LoginController.js b/scripts/LoginController.js index f1a3f12..c5c6c78 100644 --- a/scripts/LoginController.js +++ b/scripts/LoginController.js @@ -1,43 +1,44 @@ angular.module('flashy.LoginController', ['ui.router']). controller('LoginController', ['$rootScope', '$scope', '$state', '$http', 'UserService', - function($rootScope, $scope, $state, $http, UserService) { + function ($rootScope, $scope, $state, $http, UserService) { 'use strict'; // If we're logged in, there's nothing to do here if (UserService.isLoggedIn()) $state.go('addclass'); $scope.uniqueError = false; $scope.loginError = false; - $scope.login = function(email, password) { + $scope.login = function (email, password) { $http.post('/api/login/', JSON.stringify({ 'email': email, 'password': password })). - success(function(data) { + success(function (data) { UserService.login(data); if (angular.isDefined($scope.returnToState)) $state.go($scope.returnToState.name, $scope.returnToStateParams); - else $state.go('addclass'); + else + UserService.redirectToDefaultState($state); }). - error(function(data, status, header, config) { + error(function (data, status, header, config) { if (data.detail) { // assume 'invalid email or pass' $scope.loginError = true; } console.log(data); }); }; - $scope.signUp = function(email, password) { + $scope.signUp = function (email, password) { $http.post('/api/register/', JSON.stringify({ 'email': email, 'password': password })). - success(function(data) { + success(function (data) { UserService.login(data); if (angular.isDefined($scope.returnToState)) $state.go($scope.returnToState.name, $scope.returnToStateParams); else $state.go('addclass'); }). - error(function(data, status, headers, config) { + error(function (data, status, headers, config) { console.log(data.email); if (data.email == 'This field is required.') { $scope.invalid = true; @@ -51,10 +52,10 @@ angular.module('flashy.LoginController', ['ui.router']). }); }; - $scope.triggerPasswordReset = function() { + $scope.triggerPasswordReset = function () { $state.go('requestpasswordreset'); }; - $(document).ready(function() { + $(document).ready(function () { $('ul.tabs').tabs(); }); } diff --git a/scripts/RootController.js b/scripts/RootController.js index 98b1ab4..77b22eb 100644 --- a/scripts/RootController.js +++ b/scripts/RootController.js @@ -1,14 +1,13 @@ angular.module('flashy.RootController', ['ui.router', 'ngResource']). - controller('RootController', function($rootScope, $resource, $scope, $state, UserService) { + controller('RootController', function ($rootScope, $resource, $scope, $state, UserService) { $rootScope.SectionResource = $resource('/api/sections/:sectionId/'); window.rootscope = $rootScope; window.userservice = UserService; $rootScope.currentSection = {}; $scope.UserService = UserService; - UserService.getUserData().then(function(data) { - $scope.user = data; - }); + + //UserService.getUserData().then(function(data) { // console.log(data); // $rootScope.user = data; @@ -19,4 +18,14 @@ angular.module('flashy.RootController', ['ui.router', 'ngResource']). closeOnClick: true // Closes side-nav on clicks, useful for Angular/Meteor } ); + var postlogin = function (data) { + $scope.user = data; + UserService.redirectToDefaultState($state); + }; + if (UserService.isLoggedIn()) { + postlogin(UserService.getUserData()); + } else { + UserService.getUserData().then(postlogin); + } + }); diff --git a/scripts/UserService.js b/scripts/UserService.js index 979a92e..179ef1c 100644 --- a/scripts/UserService.js +++ b/scripts/UserService.js @@ -1,41 +1,48 @@ angular.module('flashy.UserService', ['ui.router']). - service('UserService', function($rootScope, $http, $q) { + service('UserService', function ($rootScope, $http, $q) { var deferred = $q.defer(); var _user = false; - $http.get('/api/me/').success(function(data) { + $http.get('/api/me/').success(function (data) { console.log('user is logged in!'); _user = data; deferred.resolve(data); - }).error(function(data) { + }).error(function (data) { console.log(data); console.log('not logged in yet: ' + data.detail); _user = {email: false}; deferred.resolve(_user); }); - this.isResolved = function() { + this.isResolved = function () { return !!_user; }; - this.getUserData = function() { + this.getUserData = function () { if (this.isResolved()) return _user; else return deferred.promise; }; - this.login = function(data) { + this.login = function (data) { _user = data; if (!data.is_confirmed) { Materialize.toast('Please verify your email address!', 4000); } deferred.resolve(data); }; - this.hasVerifiedEmail = function() { + this.hasVerifiedEmail = function () { return this.isResolved() && _user.is_confirmed; }; - this.logout = function() { + this.logout = function () { _user = false; deferred.resolve({}); }; - this.isLoggedIn = function() { + this.isLoggedIn = function () { rv = this.isResolved() && _user.email; return rv; }; + this.redirectToDefaultState = function ($state) { + if (!this.isLoggedIn()) return $state.go('login'); + if (!_user.sections.length) return $state.go('addclass'); + last_state = localStorage.getItem('last_state'); + if (last_state) return $state.go(last_state, JSON.parse(localStorage.getItem('last_state_params'))); + $state.go('feed', {sectionId: _user.sections[0].id}); + }; }); -- 1.9.1