fix(settings): ensure newly added variables are always preserved and saved

This commit is contained in:
2026-08-15 22:24:46 +03:30
parent 407a5124ac
commit 196eb77b31
+14 -9
View File
@@ -320,6 +320,9 @@ const getSlotDefaultName = (templateKey, slotKey) => {
return found?.defaultName || ''; return found?.defaultName || '';
}; };
let varIdCounter = 0;
const nextVarId = () => `var_${++varIdCounter}_${Date.now()}`;
const lockedAtLabel = computed(() => { const lockedAtLabel = computed(() => {
if (!lockedAt.value) return ''; if (!lockedAt.value) return '';
const date = new Date(lockedAt.value); const date = new Date(lockedAt.value);
@@ -332,9 +335,7 @@ const onSlotChange = (templateKey, vIdx) => {
if (!form || !form.variables[vIdx]) return; if (!form || !form.variables[vIdx]) return;
const currentVar = form.variables[vIdx]; const currentVar = form.variables[vIdx];
const defaultName = getSlotDefaultName(templateKey, currentVar.slot); const defaultName = getSlotDefaultName(templateKey, currentVar.slot);
if (!currentVar.name || currentVar.name === 'param') {
currentVar.name = defaultName; currentVar.name = defaultName;
}
}; };
const addVariable = (templateKey) => { const addVariable = (templateKey) => {
@@ -347,7 +348,7 @@ const addVariable = (templateKey) => {
const selectedSlot = unusedSlot || availableSlots[0] || { slot: 'amount', defaultName: 'PAYMENT_PRICE' }; const selectedSlot = unusedSlot || availableSlots[0] || { slot: 'amount', defaultName: 'PAYMENT_PRICE' };
form.variables.push({ form.variables.push({
id: `var_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`, id: nextVarId(),
slot: selectedSlot.slot, slot: selectedSlot.slot,
name: selectedSlot.defaultName || '' name: selectedSlot.defaultName || ''
}); });
@@ -449,13 +450,13 @@ const emptyFormEntry = (template) => {
let vars = []; let vars = [];
if (Array.isArray(template?.variables) && template.variables.length > 0) { if (Array.isArray(template?.variables) && template.variables.length > 0) {
vars = template.variables.map((variable) => ({ vars = template.variables.map((variable) => ({
id: `var_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`, id: nextVarId(),
slot: variable.slot || slots[0]?.slot || 'amount', slot: variable.slot || slots[0]?.slot || 'amount',
name: variable.name || getSlotDefaultName(template?.key, variable.slot) || '' name: variable.name || getSlotDefaultName(template?.key, variable.slot) || ''
})); }));
} else if (!template?.variables) { } else if (!template?.variables) {
vars = slots.map((s) => ({ vars = slots.map((s) => ({
id: `var_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`, id: nextVarId(),
slot: s.slot, slot: s.slot,
name: s.defaultName name: s.defaultName
})); }));
@@ -499,11 +500,15 @@ const saveSmsTemplates = async () => {
smsTemplatesPayload[template.key] = { smsTemplatesPayload[template.key] = {
templateId: String(entry.templateId || '').trim(), templateId: String(entry.templateId || '').trim(),
variables: (entry.variables || []) variables: (entry.variables || [])
.filter((v) => v.slot && String(v.name || '').trim()) .filter((v) => v && v.slot)
.map((v) => ({ .map((v) => {
const raw = String(v.name || '').trim().replace(/^#+|#+$/g, '');
const fallback = getSlotDefaultName(template.key, v.slot) || v.slot;
return {
slot: v.slot, slot: v.slot,
name: String(v.name).trim().replace(/^#+|#+$/g, '') name: raw || fallback
})) };
})
}; };
}); });
const response = await settingsApi.save({ smsTemplates: smsTemplatesPayload }); const response = await settingsApi.save({ smsTemplates: smsTemplatesPayload });