From 0288a1acbfffd1a87a9a496785a255353d1f7365 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 18 Jul 2021 22:59:34 +1000 Subject: [PATCH] Refactor edit and delete views for ManufacturerPart --- .../company/templates/company/detail.html | 20 +--- .../templates/company/manufacturer_part.html | 26 ++--- InvenTree/part/templates/part/detail.html | 19 +--- InvenTree/templates/js/company.js | 105 +++++++++++++++++- 4 files changed, 119 insertions(+), 51 deletions(-) diff --git a/InvenTree/company/templates/company/detail.html b/InvenTree/company/templates/company/detail.html index 18e85718cb..b45289eb8c 100644 --- a/InvenTree/company/templates/company/detail.html +++ b/InvenTree/company/templates/company/detail.html @@ -284,23 +284,9 @@ $("#manufacturer-part-create").click(function () { - constructForm('{% url "api-manufacturer-part-list" %}', { - fields: { - part: {}, - manufacturer: { - value: {{ company.pk }}, - }, - MPN: { - icon: 'fa-hashtag', - }, - description: {}, - link: { - icon: 'fa-link', - }, - }, - method: 'POST', - title: '{% trans "Add Manufacturer Part" %}', - onSuccess: function() { + createManufacturerPart({ + manufacturer: {{ company.pk }}, + onSuccess: function() { $("#part-table").bootstrapTable("refresh"); } }); diff --git a/InvenTree/company/templates/company/manufacturer_part.html b/InvenTree/company/templates/company/manufacturer_part.html index bc6d4d5960..94ff64440f 100644 --- a/InvenTree/company/templates/company/manufacturer_part.html +++ b/InvenTree/company/templates/company/manufacturer_part.html @@ -297,29 +297,19 @@ $('#order-part, #order-part2').click(function() { $('#edit-part').click(function () { - constructForm('{% url "api-manufacturer-part-detail" part.pk %}', { - fields: { - part: {}, - manufacturer: {}, - MPN: { - icon: 'fa-hashtag', - }, - description: {}, - link: { - icon: 'fa-link', - }, - }, - title: '{% trans "Edit Manufacturer Part" %}', - reload: true, + editManufacturerPart({{ part.pk }}, { + onSuccess: function() { + location.reload(); + } }); }); $('#delete-part').click(function() { - constructForm('{% url "api-manufacturer-part-detail" part.pk %}', { - method: 'DELETE', - title: '{% trans "Delete Manufacturer Part" %}', - redirect: "{% url 'company-detail' part.manufacturer.id %}", + deleteManufacturerPart({{ part.pk }}, { + onSuccess: function() { + window.location.href = "{% url 'company-detail' part.manufacturer.id %}"; + } }); }); diff --git a/InvenTree/part/templates/part/detail.html b/InvenTree/part/templates/part/detail.html index f5eee0cdfa..0666108e0b 100644 --- a/InvenTree/part/templates/part/detail.html +++ b/InvenTree/part/templates/part/detail.html @@ -879,21 +879,10 @@ }); $('#manufacturer-create').click(function () { - - constructForm('{% url "api-manufacturer-part-list" %}', { - fields: { - part: { - value: {{ part.pk }}, - hidden: true, - }, - manufacturer: {}, - MPN: {}, - description: {}, - link: {}, - }, - method: 'POST', - title: '{% trans "Add Manufacturer Part" %}', - onSuccess: function() { + + createManufacturerPart({ + part: {{ part.pk }}, + onSuccess: function() { $("#manufacturer-part-table").bootstrapTable("refresh"); } }); diff --git a/InvenTree/templates/js/company.js b/InvenTree/templates/js/company.js index 553cb9e0ba..b202fbcd52 100644 --- a/InvenTree/templates/js/company.js +++ b/InvenTree/templates/js/company.js @@ -1,6 +1,65 @@ {% load i18n %} +function manufacturerPartFields() { + + return { + part: {}, + manufacturer: {}, + MPN: { + icon: 'fa-hashtag', + }, + description: {}, + link: { + icon: 'fa-link', + } + }; +} + + +function createManufacturerPart(options={}) { + + var fields = manufacturerPartFields(); + + if (options.part) { + fields.part.value = options.part; + fields.part.hidden = true; + } + + if (options.manufacturer) { + fields.manufacturer.value = options.manufacturer; + } + + constructForm('{% url "api-manufacturer-part-list" %}', { + fields: fields, + method: 'POST', + title: '{% trans "Add Manufacturer Part" %}', + onSuccess: options.onSuccess + }); +} + + +function editManufacturerPart(part, options={}) { + + var url = `/api/company/part/manufacturer/${part}/`; + + constructForm(url, { + fields: manufacturerPartFields(), + title: '{% trans "Edit Manufacturer Part" %}', + onSuccess: options.onSuccess + }); +} + +function deleteManufacturerPart(part, options={}) { + + constructForm(`/api/company/part/manufacturer/${part}/`, { + method: 'DELETE', + title: '{% trans "Delete Manufacturer Part" %}', + onSuccess: options.onSuccess, + }); +} + + function supplierPartFields() { return { @@ -400,8 +459,52 @@ function loadManufacturerPartTable(table, url, options) { title: '{% trans "Description" %}', sortable: false, switchable: true, + }, + { + field: 'actions', + title: '', + sortable: false, + switchable: false, + formatter: function(value, row) { + var pk = row.pk; + + var html = `
`; + + html += makeIconButton('fa-edit icon-blue', 'button-manufacturer-part-edit', pk, '{% trans "Edit manufacturer part" %}'); + html += makeIconButton('fa-trash-alt icon-red', 'button-manufacturer-part-delete', pk, '{% trans "Delete manufacturer part" %}'); + + html += '
'; + + return html; + } } ], + onPostBody: function() { + // Callbacks + $(table).find('.button-manufacturer-part-edit').click(function() { + var pk = $(this).attr('pk'); + + editManufacturerPart( + pk, + { + onSuccess: function() { + $(table).bootstrapTable('refresh'); + } + }); + }); + + $(table).find('.button-manufacturer-part-delete').click(function() { + var pk = $(this).attr('pk'); + + deleteManufacturerPart( + pk, + { + onSuccess: function() { + $(table).bootstrapTable('refresh'); + } + }); + }) + } }); } @@ -659,7 +762,7 @@ function loadSupplierPartTable(table, url, options) { var html = `
`; html += makeIconButton('fa-edit icon-blue', 'button-supplier-part-edit', pk, '{% trans "Edit supplier part" %}'); - html += makeIconButton('fa-trash-alt icon-red', 'button-supplier-part-delete', pk, '{% trans "Delete manufacturer part" %}'); + html += makeIconButton('fa-trash-alt icon-red', 'button-supplier-part-delete', pk, '{% trans "Delete supplier part" %}'); html += '
';