mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	More refactoring for notifications
- Adds default behaviour for successful stock item creation
This commit is contained in:
		@@ -1,7 +1,7 @@
 | 
				
			|||||||
/*
 | 
					/*
 | 
				
			||||||
 * Add a cached alert message to sesion storage
 | 
					 * Add a cached alert message to sesion storage
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function addCachedAlert(message, style) {
 | 
					function addCachedAlert(message, options={}) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var alerts = sessionStorage.getItem('inventree-alerts');
 | 
					    var alerts = sessionStorage.getItem('inventree-alerts');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -13,7 +13,8 @@ function addCachedAlert(message, style) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    alerts.push({
 | 
					    alerts.push({
 | 
				
			||||||
        message: message,
 | 
					        message: message,
 | 
				
			||||||
        style: style
 | 
					        style: options.style || 'success',
 | 
				
			||||||
 | 
					        icon: options.icon,
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sessionStorage.setItem('inventree-alerts', JSON.stringify(alerts));
 | 
					    sessionStorage.setItem('inventree-alerts', JSON.stringify(alerts));
 | 
				
			||||||
@@ -31,13 +32,13 @@ function clearCachedAlerts() {
 | 
				
			|||||||
/*
 | 
					/*
 | 
				
			||||||
 * Display an alert, or cache to display on reload
 | 
					 * Display an alert, or cache to display on reload
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function showAlertOrCache(message, style, cache=false) {
 | 
					function showAlertOrCache(message, cache, options={}) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (cache) {
 | 
					    if (cache) {
 | 
				
			||||||
        addCachedAlert(message, style);
 | 
					        addCachedAlert(message, options);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        showMessage(message, {style: style});
 | 
					        showMessage(message, options);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -50,7 +51,13 @@ function showCachedAlerts() {
 | 
				
			|||||||
    var alerts = JSON.parse(sessionStorage.getItem('inventree-alerts')) || [];
 | 
					    var alerts = JSON.parse(sessionStorage.getItem('inventree-alerts')) || [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    alerts.forEach(function(alert) {
 | 
					    alerts.forEach(function(alert) {
 | 
				
			||||||
        showMessage(alert.message, {style: alert.style});
 | 
					        showMessage(
 | 
				
			||||||
 | 
					            alert.message,
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                style: alert.style || 'success',
 | 
				
			||||||
 | 
					                icon: alert.icon,
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    clearCachedAlerts();
 | 
					    clearCachedAlerts();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -308,12 +308,12 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    $('#item-create').click(function () {
 | 
					    $('#item-create').click(function () {
 | 
				
			||||||
        createNewStockItem({
 | 
					        createNewStockItem({
 | 
				
			||||||
            follow: true,
 | 
					            table: '#stock-table',
 | 
				
			||||||
            data: {
 | 
					            data: {
 | 
				
			||||||
                {% if location %}
 | 
					                {% if location %}
 | 
				
			||||||
                location: {{ location.id }}
 | 
					                location: {{ location.id }}
 | 
				
			||||||
                {% endif %}
 | 
					                {% endif %}
 | 
				
			||||||
            }
 | 
					            },
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -111,7 +111,13 @@ $(document).ready(function () {
 | 
				
			|||||||
    // notifications
 | 
					    // notifications
 | 
				
			||||||
    {% if messages %}
 | 
					    {% if messages %}
 | 
				
			||||||
    {% for message in messages %}
 | 
					    {% for message in messages %}
 | 
				
			||||||
    showAlertOrCache('{{ message }}', 'info', true);
 | 
					    showAlertOrCache(
 | 
				
			||||||
 | 
					        '{{ message }}',
 | 
				
			||||||
 | 
					        true,
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            style: 'info',
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
    {% endfor %}
 | 
					    {% endfor %}
 | 
				
			||||||
    {% endif %}
 | 
					    {% endif %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -480,10 +480,13 @@ function barcodeCheckIn(location_id) {
 | 
				
			|||||||
                            $(modal).modal('hide');
 | 
					                            $(modal).modal('hide');
 | 
				
			||||||
                            if (status == 'success' && 'success' in response) {
 | 
					                            if (status == 'success' && 'success' in response) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                                showAlertOrCache(response.success, 'success', true);
 | 
					                                addCachedAlert(response.success);
 | 
				
			||||||
                                location.reload();
 | 
					                                location.reload();
 | 
				
			||||||
                            } else {
 | 
					                            } 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');
 | 
					                            $(modal).modal('hide');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            if (status == 'success' && 'success' in response) {
 | 
					                            if (status == 'success' && 'success' in response) {
 | 
				
			||||||
                                showAlertOrCache(response.success, 'success', true);
 | 
					                                addCachedAlert(response.success);
 | 
				
			||||||
                                location.reload();
 | 
					                                location.reload();
 | 
				
			||||||
                            } else {
 | 
					                            } else {
 | 
				
			||||||
                                showAlertOrCache('{% trans "Error transferring stock" %}', 'danger', false);
 | 
					                                showMessage('{% trans "Error transferring stock" %}', {
 | 
				
			||||||
 | 
					                                    style: 'danger',
 | 
				
			||||||
 | 
					                                });
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -904,19 +904,19 @@ function handleFormSuccess(response, options) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Display any messages
 | 
					    // Display any messages
 | 
				
			||||||
    if (response && response.success) {
 | 
					    if (response && response.success) {
 | 
				
			||||||
        showAlertOrCache(response.success, 'success', cache);
 | 
					        showAlertOrCache(response.success, cache, {style: 'success'});
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if (response && response.info) {
 | 
					    if (response && response.info) {
 | 
				
			||||||
        showAlertOrCache(response.info, 'info', cache);
 | 
					        showAlertOrCache(response.info, cache, {style: 'info'});
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (response && response.warning) {
 | 
					    if (response && response.warning) {
 | 
				
			||||||
        showAlertOrCache(response.warning, 'warning', cache);
 | 
					        showAlertOrCache(response.warning, cache, {style: 'warning'});
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (response && response.danger) {
 | 
					    if (response && response.danger) {
 | 
				
			||||||
        showAlertOrCache(response.danger, 'dagner', cache);
 | 
					        showAlertOrCache(response.danger, cache, {style: 'danger'});
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (options.onSuccess) {
 | 
					    if (options.onSuccess) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -399,19 +399,19 @@ function afterForm(response, options) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Display any messages
 | 
					    // Display any messages
 | 
				
			||||||
    if (response.success) {
 | 
					    if (response.success) {
 | 
				
			||||||
        showAlertOrCache(response.success, 'success', cache);
 | 
					        showAlertOrCache(response.success, cache, {style: 'success'});
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (response.info) {
 | 
					    if (response.info) {
 | 
				
			||||||
        showAlertOrCache(response.info, 'info', cache);
 | 
					        showAlertOrCache(response.info, cache, {style: 'info'});
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if (response.warning) {
 | 
					    if (response.warning) {
 | 
				
			||||||
        showAlertOrCache(response.warning, 'warning', cache);
 | 
					        showAlertOrCache(response.warning, cache, {style: 'warning'});
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if (response.danger) {
 | 
					    if (response.danger) {
 | 
				
			||||||
        showAlertOrCache(response.danger, 'danger', cache);
 | 
					        showAlertOrCache(response.danger, cache, {style: 'danger'});
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Was a callback provided?
 | 
					    // Was a callback provided?
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -317,6 +317,33 @@ function createNewStockItem(options={}) {
 | 
				
			|||||||
    options.fields = stockItemFields(options);
 | 
					    options.fields = stockItemFields(options);
 | 
				
			||||||
    options.groups = stockItemGroups(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);
 | 
					    constructForm(url, options);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user