More refactoring for notifications

- Adds default behaviour for successful stock item creation
This commit is contained in:
Oliver 2021-11-04 23:09:49 +11:00
parent 97326d9fb2
commit 3be4acf3ef
7 changed files with 66 additions and 21 deletions

View File

@ -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();

View File

@ -308,12 +308,12 @@
$('#item-create').click(function () {
createNewStockItem({
follow: true,
table: '#stock-table',
data: {
{% if location %}
location: {{ location.id }}
{% endif %}
}
},
});
});

View File

@ -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 %}

View File

@ -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',
});
}
}
}

View File

@ -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) {

View File

@ -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?

View File

@ -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);
}