From 3be4acf3ef9d8290ab8472ab50a743b17968ab29 Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Thu, 4 Nov 2021 23:09:49 +1100
Subject: [PATCH] More refactoring for notifications

- Adds default behaviour for successful stock item creation
---
 .../static/script/inventree/notification.js   | 19 ++++++++-----
 InvenTree/stock/templates/stock/location.html |  4 +--
 InvenTree/templates/account/base.html         |  8 +++++-
 InvenTree/templates/js/translated/barcode.js  | 13 ++++++---
 InvenTree/templates/js/translated/forms.js    |  8 +++---
 InvenTree/templates/js/translated/modals.js   |  8 +++---
 InvenTree/templates/js/translated/stock.js    | 27 +++++++++++++++++++
 7 files changed, 66 insertions(+), 21 deletions(-)

diff --git a/InvenTree/InvenTree/static/script/inventree/notification.js b/InvenTree/InvenTree/static/script/inventree/notification.js
index 399ba1d359..f6bdf3bc57 100644
--- a/InvenTree/InvenTree/static/script/inventree/notification.js
+++ b/InvenTree/InvenTree/static/script/inventree/notification.js
@@ -1,7 +1,7 @@
 /*
  * Add a cached alert message to sesion storage
  */
-function addCachedAlert(message, style) {
+function addCachedAlert(message, options={}) {
 
     var alerts = sessionStorage.getItem('inventree-alerts');
 
@@ -13,7 +13,8 @@ function addCachedAlert(message, style) {
 
     alerts.push({
         message: message,
-        style: style
+        style: options.style || 'success',
+        icon: options.icon,
     });
 
     sessionStorage.setItem('inventree-alerts', JSON.stringify(alerts));
@@ -31,13 +32,13 @@ function clearCachedAlerts() {
 /*
  * Display an alert, or cache to display on reload
  */
-function showAlertOrCache(message, style, cache=false) {
+function showAlertOrCache(message, cache, options={}) {
 
     if (cache) {
-        addCachedAlert(message, style);
+        addCachedAlert(message, options);
     } else {
 
-        showMessage(message, {style: style});
+        showMessage(message, options);
     }
 }
 
@@ -50,7 +51,13 @@ function showCachedAlerts() {
     var alerts = JSON.parse(sessionStorage.getItem('inventree-alerts')) || [];
 
     alerts.forEach(function(alert) {
-        showMessage(alert.message, {style: alert.style});
+        showMessage(
+            alert.message,
+            {
+                style: alert.style || 'success',
+                icon: alert.icon,
+            }
+        );
     });
 
     clearCachedAlerts();
diff --git a/InvenTree/stock/templates/stock/location.html b/InvenTree/stock/templates/stock/location.html
index 8270db22f5..24a6da69a9 100644
--- a/InvenTree/stock/templates/stock/location.html
+++ b/InvenTree/stock/templates/stock/location.html
@@ -308,12 +308,12 @@
 
     $('#item-create').click(function () {
         createNewStockItem({
-            follow: true,
+            table: '#stock-table',
             data: {
                 {% if location %}
                 location: {{ location.id }}
                 {% endif %}
-            }
+            },
         });
     });
 
diff --git a/InvenTree/templates/account/base.html b/InvenTree/templates/account/base.html
index fa2a34f79d..7f2486bfcc 100644
--- a/InvenTree/templates/account/base.html
+++ b/InvenTree/templates/account/base.html
@@ -111,7 +111,13 @@ $(document).ready(function () {
     // notifications
     {% if messages %}
     {% for message in messages %}
-    showAlertOrCache('{{ message }}', 'info', true);
+    showAlertOrCache(
+        '{{ message }}',
+        true,
+        {
+            style: 'info',
+        }
+    );
     {% endfor %}
     {% endif %}
 
diff --git a/InvenTree/templates/js/translated/barcode.js b/InvenTree/templates/js/translated/barcode.js
index 4b61249d0b..fcc4df5f50 100644
--- a/InvenTree/templates/js/translated/barcode.js
+++ b/InvenTree/templates/js/translated/barcode.js
@@ -480,10 +480,13 @@ function barcodeCheckIn(location_id) {
                             $(modal).modal('hide');
                             if (status == 'success' && 'success' in response) {
 
-                                showAlertOrCache(response.success, 'success', true);
+                                addCachedAlert(response.success);
                                 location.reload();
                             } else {
-                                showAlertOrCache('{% trans "Error transferring stock" %}', 'danger', false);
+                                showMessage('{% trans "Error transferring stock" %}', {
+                                    style: 'danger',
+                                    icon: 'fas fa-times-circle',
+                                });
                             }
                         }
                     }
@@ -604,10 +607,12 @@ function scanItemsIntoLocation(item_id_list, options={}) {
                             $(modal).modal('hide');
 
                             if (status == 'success' && 'success' in response) {
-                                showAlertOrCache(response.success, 'success', true);
+                                addCachedAlert(response.success);
                                 location.reload();
                             } else {
-                                showAlertOrCache('{% trans "Error transferring stock" %}', 'danger', false);
+                                showMessage('{% trans "Error transferring stock" %}', {
+                                    style: 'danger',
+                                });
                             }
                         }
                     }
diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js
index 2308c1e247..0da32c58ff 100644
--- a/InvenTree/templates/js/translated/forms.js
+++ b/InvenTree/templates/js/translated/forms.js
@@ -904,19 +904,19 @@ function handleFormSuccess(response, options) {
 
     // Display any messages
     if (response && response.success) {
-        showAlertOrCache(response.success, 'success', cache);
+        showAlertOrCache(response.success, cache, {style: 'success'});
     }
     
     if (response && response.info) {
-        showAlertOrCache(response.info, 'info', cache);
+        showAlertOrCache(response.info, cache, {style: 'info'});
     }
 
     if (response && response.warning) {
-        showAlertOrCache(response.warning, 'warning', cache);
+        showAlertOrCache(response.warning, cache, {style: 'warning'});
     }
 
     if (response && response.danger) {
-        showAlertOrCache(response.danger, 'dagner', cache);
+        showAlertOrCache(response.danger, cache, {style: 'danger'});
     }
 
     if (options.onSuccess) {
diff --git a/InvenTree/templates/js/translated/modals.js b/InvenTree/templates/js/translated/modals.js
index aefdba604f..4cd0be8cec 100644
--- a/InvenTree/templates/js/translated/modals.js
+++ b/InvenTree/templates/js/translated/modals.js
@@ -399,19 +399,19 @@ function afterForm(response, options) {
 
     // Display any messages
     if (response.success) {
-        showAlertOrCache(response.success, 'success', cache);
+        showAlertOrCache(response.success, cache, {style: 'success'});
     }
 
     if (response.info) {
-        showAlertOrCache(response.info, 'info', cache);
+        showAlertOrCache(response.info, cache, {style: 'info'});
     }
     
     if (response.warning) {
-        showAlertOrCache(response.warning, 'warning', cache);
+        showAlertOrCache(response.warning, cache, {style: 'warning'});
     }
     
     if (response.danger) {
-        showAlertOrCache(response.danger, 'danger', cache);
+        showAlertOrCache(response.danger, cache, {style: 'danger'});
     }
 
     // Was a callback provided?
diff --git a/InvenTree/templates/js/translated/stock.js b/InvenTree/templates/js/translated/stock.js
index 7ba5a52b97..3e6b35ca83 100644
--- a/InvenTree/templates/js/translated/stock.js
+++ b/InvenTree/templates/js/translated/stock.js
@@ -317,6 +317,33 @@ function createNewStockItem(options={}) {
     options.fields = stockItemFields(options);
     options.groups = stockItemGroups(options);
 
+    if (!options.onSuccess) {
+        options.onSuccess = function(response) {
+            // If a single stock item has been created, follow it!
+            if (response.pk) {
+                var url = `/stock/item/${pk}/`;
+
+                addCachedAlert('{% trans "Created stock item" %}', {
+                    icon: 'fas fa-boxes',
+                });
+
+                location.href = url;
+            } else {
+
+                var q = response.quantity;
+
+                showMessage('{% trans "Created stock items" %}', {
+                    icon: 'fas fa-boxes',
+                });
+
+                if (options.table) {
+                    // Reload the table
+                    $(options.table).bootstrapTable('refresh');
+                }
+            }
+        }
+    }
+
     constructForm(url, options);
 }