From 089a9f98409df35a5368b407d308572ac80ac0aa Mon Sep 17 00:00:00 2001
From: Dorian Pula <dorian.pula@amber-penguin-software.ca>
Date: Mon, 8 Aug 2016 09:03:28 -0400
Subject: [PATCH] Start on fixing up the login manager.

---
 client/actions.js                             | 27 +++++++++----
 client/containers/app.js                      |  6 +--
 client/stores/app_site_info_store.js          | 34 -----------------
 client/stores/navigation_menu_store.js        | 34 -----------------
 ...r_login_store.js => user_login_manager.js} | 38 ++++++++-----------
 client/views/user_login_modal.js              | 12 +++---
 6 files changed, 46 insertions(+), 105 deletions(-)
 delete mode 100644 client/stores/app_site_info_store.js
 delete mode 100644 client/stores/navigation_menu_store.js
 rename client/{stores/user_login_store.js => user_login_manager.js} (65%)

diff --git a/client/actions.js b/client/actions.js
index 30d3d19..a440dee 100644
--- a/client/actions.js
+++ b/client/actions.js
@@ -33,6 +33,12 @@ export function fetchArticle(articleId='landing') {
         console.info(`Loading... ${articleId}`);
         return fetch(`/app/pages/${articleId}`)
             .then((response) => {
+                // TODO: Add better error handling.
+                // if (response.ok) {
+                //     return response.json();
+                // } else {
+                //     console.error(`Error getting document JSON - ${res.status} - ${res.text} - Actual ${err}`);
+                // }
                 return response.json();
             })
             .then((json) => {
@@ -54,6 +60,12 @@ export function fetchNavigationMenu() {
     return (dispatch) => {
         return fetch("/app/menu")
             .then((response) => {
+                // TODO: Add better error handling.
+                // if (response.ok) {
+                //     return response.json();
+                // } else {
+                //     console.error(`Error getting document JSON - ${res.status} - ${res.text} - Actual ${err}`);
+                // }
                 return response.json();
             })
             .then((json) => {
@@ -66,6 +78,14 @@ export function fetchAppSiteInfo() {
     return (dispatch) => {
         return fetch("/app/config")
             .then((response) => {
+
+                // TODO: Add better error handling.
+                // if (response.ok) {
+                //     return response.json();
+                // } else {
+                //     console.error(`Error getting document JSON - ${res.status} - ${res.text} - Actual ${err}`);
+                // }
+
                 return response.json();
             })
             .then((json) => {
@@ -125,13 +145,6 @@ export function loadNavigation(menu=[]) {
     };
 }
 
-export function notifyUserLogin(user) {
-    return {
-        type: LOGIN_USER_ACTION,
-        user: user
-    };
-}
-
 export function switchThemes() {
     let switchedThemes = ThemeManager.switchTheme();
     return {
diff --git a/client/containers/app.js b/client/containers/app.js
index 0b1213e..1b95e54 100644
--- a/client/containers/app.js
+++ b/client/containers/app.js
@@ -9,8 +9,8 @@ Rookeries client app
 import React from "react";
 import {connect} from "react-redux";
 
-// import {UserLoginView} from "../views/user_login_modal";
-import {ThemeSwitchView, ConnectedThemeSwitchView} from "../views/theme_switcher_button";
+import {UserLoginView} from "../views/user_login_modal";
+import {ConnectedThemeSwitchView} from "../views/theme_switcher_button";
 
 import {SiteHeader} from "../views/site_header";
 import {SiteFooter} from "../views/site_footer";
@@ -33,7 +33,7 @@ class App extends React.Component {
                 {/*TODO Extract as header container.*/}
                 <div className="row">
                     <div className="col-lg-offset-1 col-lg-3">
-                        {/*<UserLoginView />*/}
+                        <UserLoginView />
                     </div>
                     <div className="col-lg-offset-5 col-lg-2">
                         <ConnectedThemeSwitchView />
diff --git a/client/stores/app_site_info_store.js b/client/stores/app_site_info_store.js
deleted file mode 100644
index e6661a1..0000000
--- a/client/stores/app_site_info_store.js
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
-Store handling app site information.
-
-@copyright (c) Copyright 2013-2016 Dorian Pula
-@license AGPL-1.0
-@author Dorian Pula [dorian.pula@amber-penguin-software.ca]
-*/
-
-import Reflux from "reflux";
-import request from "superagent";
-
-import {Actions} from "../actions.js";
-
-
-const AppSiteInfoStore = Reflux.createStore({
-    init: function() {
-        this.listenTo(Actions.fetchAppSiteInfo, this.fetchAppSiteInfo);
-    },
-
-    fetchAppSiteInfo: function() {
-        request.get("/app/config")
-          .accept('json')
-          .end(
-            (err, res) => {
-                if (res.ok) {
-                    Actions.renderAppSiteInfo(res.body);
-                } else {
-                    console.error(`Error getting document JSON - ${res.status} - ${res.text} - Actual ${err}`);
-                }
-            });
-    }
-});
-
-export { AppSiteInfoStore };
diff --git a/client/stores/navigation_menu_store.js b/client/stores/navigation_menu_store.js
deleted file mode 100644
index 0be608a..0000000
--- a/client/stores/navigation_menu_store.js
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
-Store handling navigation menu.
-
-@copyright (c) Copyright 2013-2016 Dorian Pula
-@license AGPL-1.0
-@author Dorian Pula [dorian.pula@amber-penguin-software.ca]
-*/
-
-import Reflux from "reflux";
-import request from "superagent";
-
-import {Actions} from "../actions.js";
-
-
-const NavigationMenuStore = Reflux.createStore({
-    init: function() {
-        this.listenTo(Actions.fetchNavigationMenu, this.fetchNavigationDetails);
-    },
-
-    fetchNavigationDetails: function() {
-        request.get("/app/menu")
-          .accept('json')
-          .end(
-            (err, res) => {
-                if (res.ok) {
-                    Actions.renderNavigationMenu({menu: res.body.menu});
-                } else {
-                    console.error(`Error getting document JSON - ${res.status} - ${res.text} - Actual ${err}`);
-                }
-            });
-    }
-});
-
-export { NavigationMenuStore };
diff --git a/client/stores/user_login_store.js b/client/user_login_manager.js
similarity index 65%
rename from client/stores/user_login_store.js
rename to client/user_login_manager.js
index cb5db08..94e3d4d 100644
--- a/client/stores/user_login_store.js
+++ b/client/user_login_manager.js
@@ -1,60 +1,54 @@
 /*
-User login store.
+Managing the user login store.
 
 @copyright (c) Copyright 2013-2016 Dorian Pula
 @license AGPL v3
 @author Dorian Pula [dorian.pula@amber-penguin-software.ca]
 */
 
-import Reflux from "reflux";
 import request from "superagent";
 
-import { Actions } from "../actions.js";
-import { UserInfo } from "../user_info.js";
+import { UserInfo } from "./user_info";
 
 
-export const UserLoginStore = Reflux.createStore({
-    init: function() {
-        this.listenTo(Actions.loginUserAttempt, this.attemptUserLogin);
-        this.listenTo(Actions.logoutUser, this.logoutUser);
-    },
+export class UserLoginManager {
 
-    isUserLoggedIn: function() {
+    static isUserLoggedIn() {
         return UserInfo.getUserInfo("auth_token") != "";
-    },
+    }
 
-    getUserAuthToken: function() {
+    static getUserAuthToken() {
         return UserInfo.getUserInfo("auth_token");
-    },
+    }
 
-    getCurrentUserFullName: function() {
+    static getCurrentUserFullName() {
         return UserInfo.getUserInfo("full_name");
-    },
+    }
 
-    attemptUserLogin: function(username, password) {
+    static attemptUserLogin(username, password) {
         request.post("/auth")
           .send({username: username, password: password})
           .accept('json')
           .end((err, res) => {
               if (res.ok) {
                   // TODO Include the user full name in response as well.
-                  UserLoginStore.updateUserLogin(res.body.token, "admin");
+                  UserLoginManager.updateUserLogin(res.body.token, "admin");
                   Actions.loginUserSuccess();
               } else {
                   console.error(`Unable to login - ${res.status} - ${res.text} - Actual ${err}`);
                   Actions.loginUserFailure("Login failed.  Try again!");
               }
           });
-    },
+    }
 
-    logoutUser: function() {
+    static logoutUser() {
         UserInfo.removeUserInfo("auth_token");
         UserInfo.removeUserInfo("full_name");
         Actions.logoutUserFinalize();
-    },
+    }
 
 
-    updateUserLogin: function(token, fullName) {
+    static updateUserLogin(token, fullName) {
         if (token !== undefined && token !== "") {
             UserInfo.setUserInfo("auth_token", token);
             UserInfo.setUserInfo("full_name", fullName);
@@ -63,4 +57,4 @@ export const UserLoginStore = Reflux.createStore({
             Actions.loginUserFailure(`Received invalid token: ${token}`);
         }
     }
-});
+}
diff --git a/client/views/user_login_modal.js b/client/views/user_login_modal.js
index 94c5b58..adb0fa2 100644
--- a/client/views/user_login_modal.js
+++ b/client/views/user_login_modal.js
@@ -12,14 +12,16 @@ import LinkedStateMixin from "react-addons-linked-state-mixin";
 import {Modal, Input, Button, Alert} from "react-bootstrap";
 import FontAwesome from "react-fontawesome";
 
+import {UserLoginManager} from "../user_login_manager";
+
 
 const UserLoginView = React.createClass({
     mixins: [LinkedStateMixin],
 
     getInitialState: function() {
         return {
-            isUserLoggedIn: UserLoginStore.isUserLoggedIn(),
-            fullName: UserLoginStore.getCurrentUserFullName(),
+            isUserLoggedIn: UserLoginManager.isUserLoggedIn(),
+            fullName: UserLoginManager.getCurrentUserFullName(),
             username: "",
             password: "",
             errorMessage: "",
@@ -34,7 +36,7 @@ const UserLoginView = React.createClass({
         }
 
         console.log("Attempting login...");
-        Actions.loginUserAttempt(this.state.username, this.state.password);
+        UserLoginManager.attemptUserLogin(this.state.username, this.state.password);
     },
 
     loginFailure: function(errorMessage = "") {
@@ -43,7 +45,7 @@ const UserLoginView = React.createClass({
 
     loginSuccess: function() {
         this.hideLoginModal();
-        this.setState({fullName: UserLoginStore.getCurrentUserFullName(), isUserLoggedIn: true});
+        this.setState({fullName: UserLoginManager.getCurrentUserFullName(), isUserLoggedIn: true});
     },
 
     showLoginModal: function() {
@@ -60,7 +62,7 @@ const UserLoginView = React.createClass({
     },
 
     logoutUserRefresh: function() {
-        this.setState({errorMessage: "", isUserLoggedIn: false, fullName: UserLoginStore.getCurrentUserFullName()});
+        this.setState({errorMessage: "", isUserLoggedIn: false, fullName: UserLoginManager.getCurrentUserFullName()});
     },
 
     render: function() {