|
| 1 | +/** |
| 2 | + * For the full copyright and license information, please view the |
| 3 | + * docs/licenses/LICENSE.txt file that was distributed with this source code. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Merchandise return edit page — deferred deletion of returned products (Issue #27628). |
| 8 | + * |
| 9 | + * When the merchant clicks the per-row "Delete" button we open a confirmation modal but |
| 10 | + * do NOT call the back end. Instead the row is visually marked as staged-for-deletion and |
| 11 | + * the row's (id_order_detail, id_customization) pair is appended to a hidden JSON field. |
| 12 | + * The actual removal happens server-side when the form is submitted via the main "Save" |
| 13 | + * button, processed by OrderReturnFormDataHandler. |
| 14 | + */ |
| 15 | + |
| 16 | +type StagedRow = { |
| 17 | + order_detail_id: number; |
| 18 | + customization_id: number; |
| 19 | +}; |
| 20 | + |
| 21 | +const SELECTORS = { |
| 22 | + deleteButton: '[data-role="order-return-delete-product"]', |
| 23 | + productRow: '[data-role="order-return-product-row"]', |
| 24 | + stagedField: '[data-role="order-return-staged-deletions"]', |
| 25 | + modal: '#orderReturnDeleteProductModal', |
| 26 | + modalConfirmButton: '[data-role="order-return-confirm-delete-product"]', |
| 27 | +} as const; |
| 28 | + |
| 29 | +const STAGED_ROW_CSS_CLASS = 'order-return-row-staged-for-deletion'; |
| 30 | + |
| 31 | +class OrderReturnDeletionStager { |
| 32 | + private readonly $form: JQuery; |
| 33 | + |
| 34 | + private readonly $modal: JQuery; |
| 35 | + |
| 36 | + private $pendingRow: JQuery | null = null; |
| 37 | + |
| 38 | + constructor($form: JQuery) { |
| 39 | + this.$form = $form; |
| 40 | + this.$modal = $(SELECTORS.modal); |
| 41 | + this.bind(); |
| 42 | + } |
| 43 | + |
| 44 | + private bind(): void { |
| 45 | + this.$form.on('click', SELECTORS.deleteButton, (event) => { |
| 46 | + event.preventDefault(); |
| 47 | + this.$pendingRow = $(event.currentTarget).closest(SELECTORS.productRow); |
| 48 | + this.$modal.modal('show'); |
| 49 | + }); |
| 50 | + |
| 51 | + this.$modal.on('click', SELECTORS.modalConfirmButton, () => { |
| 52 | + if (this.$pendingRow !== null) { |
| 53 | + this.stageRow(this.$pendingRow); |
| 54 | + this.$pendingRow = null; |
| 55 | + } |
| 56 | + this.$modal.modal('hide'); |
| 57 | + }); |
| 58 | + |
| 59 | + this.$modal.on('hidden.bs.modal', () => { |
| 60 | + this.$pendingRow = null; |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + private stageRow($row: JQuery): void { |
| 65 | + const orderDetailId = parseInt($row.data('order-detail-id'), 10); |
| 66 | + const customizationId = parseInt($row.data('customization-id'), 10); |
| 67 | + |
| 68 | + if (Number.isNaN(orderDetailId) || orderDetailId <= 0) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // Visual feedback — strike-through + dim, plus disable any further interaction in the row. |
| 73 | + $row.addClass(STAGED_ROW_CSS_CLASS); |
| 74 | + $row.find(SELECTORS.deleteButton).attr('disabled', 'disabled'); |
| 75 | + |
| 76 | + // Update the hidden JSON payload that OrderReturnFormDataHandler reads on submit. |
| 77 | + const $stagedField = this.$form.find(SELECTORS.stagedField); |
| 78 | + const existing: StagedRow[] = OrderReturnDeletionStager.parseField($stagedField.val()); |
| 79 | + |
| 80 | + if (!existing.some((row) => row.order_detail_id === orderDetailId |
| 81 | + && row.customization_id === (Number.isNaN(customizationId) ? 0 : customizationId))) { |
| 82 | + existing.push({ |
| 83 | + order_detail_id: orderDetailId, |
| 84 | + customization_id: Number.isNaN(customizationId) ? 0 : customizationId, |
| 85 | + }); |
| 86 | + } |
| 87 | + $stagedField.val(JSON.stringify(existing)); |
| 88 | + } |
| 89 | + |
| 90 | + private static parseField(raw: unknown): StagedRow[] { |
| 91 | + if (typeof raw !== 'string' || raw === '') { |
| 92 | + return []; |
| 93 | + } |
| 94 | + try { |
| 95 | + const decoded = JSON.parse(raw); |
| 96 | + |
| 97 | + return Array.isArray(decoded) ? decoded as StagedRow[] : []; |
| 98 | + } catch (_error) { |
| 99 | + return []; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +$(() => { |
| 105 | + const $form = $(SELECTORS.stagedField).closest('form'); |
| 106 | + |
| 107 | + if ($form.length === 0) { |
| 108 | + return; |
| 109 | + } |
| 110 | + // eslint-disable-next-line no-new |
| 111 | + new OrderReturnDeletionStager($form); |
| 112 | +}); |
0 commit comments