97 lines
3.0 KiB
JavaScript
97 lines
3.0 KiB
JavaScript
/**
|
|
* Gemeinsame Utility-Funktionen für Skrift Konfigurator
|
|
*/
|
|
|
|
/**
|
|
* Bereitet Platzhalter für einen bestimmten Index vor
|
|
* Wird von PreviewManager und Backend-Integration verwendet
|
|
*/
|
|
export function preparePlaceholdersForIndex(state, index) {
|
|
const placeholders = {};
|
|
|
|
// Prüfen ob strukturierte Empfängerdaten (klassische Adresse) im aktuellen Flow verwendet werden:
|
|
// - Adressmodus muss 'classic' sein (bei 'free' gibt es keine Felder wie vorname, name etc.)
|
|
// - UND: Direktversand ODER Kuvert mit Empfängeradresse
|
|
const isClassicAddress = (state.addressMode || 'classic') === 'classic';
|
|
const needsRecipientData = isClassicAddress && (
|
|
state.answers?.shippingMode === 'direct' ||
|
|
(state.answers?.envelope === true && state.answers?.envelopeMode === 'recipientData')
|
|
);
|
|
|
|
// Empfänger-Feldnamen, die aus recipientRows kommen können
|
|
const recipientFields = ['vorname', 'name', 'ort', 'strasse', 'hausnummer', 'plz', 'land'];
|
|
|
|
// Platzhalter aus placeholderValues extrahieren
|
|
if (state.placeholderValues) {
|
|
for (const [name, values] of Object.entries(state.placeholderValues)) {
|
|
// Empfängerfelder nur überspringen wenn sie aus recipientRows kommen
|
|
if (needsRecipientData && recipientFields.includes(name)) {
|
|
continue;
|
|
}
|
|
if (Array.isArray(values) && values.length > index) {
|
|
placeholders[name] = values[index];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Empfängerdaten aus recipientRows hinzufügen (nur wenn benötigt)
|
|
if (needsRecipientData && Array.isArray(state.recipientRows) && state.recipientRows.length > index) {
|
|
const recipient = state.recipientRows[index];
|
|
placeholders['vorname'] = recipient.firstName || '';
|
|
placeholders['name'] = recipient.lastName || '';
|
|
placeholders['ort'] = recipient.city || '';
|
|
placeholders['strasse'] = recipient.street || '';
|
|
placeholders['hausnummer'] = recipient.houseNumber || '';
|
|
placeholders['plz'] = recipient.zip || '';
|
|
placeholders['land'] = recipient.country || '';
|
|
}
|
|
|
|
return placeholders;
|
|
}
|
|
|
|
/**
|
|
* Validiert Empfängerzeilen (gemeinsame Logik für klassische und freie Adressen)
|
|
*/
|
|
export function validateRecipientRows(state, requiredCount) {
|
|
const addressMode = state.addressMode || 'classic';
|
|
|
|
if (addressMode === 'free') {
|
|
// Freie Adresse: mindestens Zeile 1 muss ausgefüllt sein
|
|
const rows = state.freeAddressRows || [];
|
|
if (rows.length !== requiredCount) return false;
|
|
|
|
for (const r of rows) {
|
|
if (!r) return false;
|
|
if (!String(r.line1 || "").trim()) return false;
|
|
}
|
|
} else {
|
|
// Klassische Adresse
|
|
const rows = state.recipientRows || [];
|
|
if (rows.length !== requiredCount) return false;
|
|
|
|
const required = [
|
|
"firstName",
|
|
"lastName",
|
|
"street",
|
|
"houseNumber",
|
|
"zip",
|
|
"city",
|
|
"country",
|
|
];
|
|
|
|
for (const r of rows) {
|
|
if (!r) return false;
|
|
for (const k of required) {
|
|
if (!String(r[k] || "").trim()) return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
export default {
|
|
preparePlaceholdersForIndex,
|
|
validateRecipientRows,
|
|
};
|