Skip to content

Commit 39eafff

Browse files
authored
Merge pull request #793 from youbek/fix/naming-template-preview
fix: implement missing naming template preview logic
2 parents f9acc72 + 66fc55d commit 39eafff

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

static/js/tenant_settings.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,75 @@ function removeCurrencyLimit(currencyCode) {
17911791
}
17921792

17931793
// Naming Template Functions
1794+
function resolveTemplate(template, context) {
1795+
if (!template) return '';
1796+
1797+
return template.replace(/\{([^}]+)\}/g, (match, key) => {
1798+
// Handle fallbacks like {campaign_name|promoted_offering}
1799+
const options = key.split('|');
1800+
1801+
for (const option of options) {
1802+
const val = context[option.trim()];
1803+
if (val !== undefined && val !== null && val !== '') {
1804+
return val;
1805+
}
1806+
}
1807+
1808+
// If no value found, keep the placeholder
1809+
return match;
1810+
});
1811+
}
1812+
1813+
function updateNamingPreview() {
1814+
const orderTemplate = document.getElementById('order_name_template')?.value || '';
1815+
const lineItemTemplate = document.getElementById('line_item_name_template')?.value || '';
1816+
1817+
// Sample data matching the HTML description
1818+
const context = {
1819+
campaign_name: '', // null/empty
1820+
promoted_offering: 'Nike Shoes Q1',
1821+
brand_name: 'Nike', // Added for compatibility with existing preset
1822+
buyer_ref: 'PO-12345',
1823+
start_date: '2025-10-07',
1824+
end_date: '2025-10-14',
1825+
date_range: 'Oct 7-14, 2025',
1826+
month_year: 'Oct 2025',
1827+
package_count: 3,
1828+
auto_name: 'Nike Shoes Q1 Campaign'
1829+
};
1830+
1831+
// 1. Resolve Order Name
1832+
const orderName = resolveTemplate(orderTemplate, context);
1833+
1834+
const orderPreviewEl = document.getElementById('order-preview');
1835+
if (orderPreviewEl) {
1836+
orderPreviewEl.textContent = orderName;
1837+
}
1838+
1839+
// 2. Resolve Line Items
1840+
const products = [
1841+
{ name: 'Display 300x250', index: 1 },
1842+
{ name: 'Video Pre-roll', index: 2 },
1843+
{ name: 'Native Article', index: 3 }
1844+
];
1845+
1846+
const lineItemNames = products.map(p => {
1847+
const itemContext = {
1848+
...context,
1849+
order_name: orderName,
1850+
product_name: p.name,
1851+
package_index: p.index
1852+
};
1853+
const name = resolveTemplate(lineItemTemplate, itemContext);
1854+
return `${p.index}. ${name}`;
1855+
});
1856+
1857+
const lineItemPreviewEl = document.getElementById('lineitem-preview');
1858+
if (lineItemPreviewEl) {
1859+
lineItemPreviewEl.innerHTML = lineItemNames.join('<br>');
1860+
}
1861+
}
1862+
17941863
function useNamingPreset(presetName) {
17951864
const presets = {
17961865
'simple': {
@@ -1824,4 +1893,14 @@ function useNamingPreset(presetName) {
18241893
if (lineItemField) {
18251894
lineItemField.value = preset.lineItem;
18261895
}
1896+
1897+
// Update preview immediately
1898+
updateNamingPreview();
18271899
}
1900+
1901+
// Initialize naming preview on load
1902+
document.addEventListener('DOMContentLoaded', function() {
1903+
if (document.getElementById('order_name_template')) {
1904+
updateNamingPreview();
1905+
}
1906+
});

0 commit comments

Comments
 (0)