fix(editor): preserve full site config object in save-draft and sync to draft file in config API
This commit is contained in:
@@ -22,10 +22,22 @@ export const POST: APIRoute = async ({ request }) => {
|
|||||||
hero: { ...(currentSite.hero || {}), ...(body.siteConfig.hero || {}) },
|
hero: { ...(currentSite.hero || {}), ...(body.siteConfig.hero || {}) },
|
||||||
site_info: { ...((currentSite as any).site_info || {}), ...(body.siteConfig.site_info || {}) }
|
site_info: { ...((currentSite as any).site_info || {}), ...(body.siteConfig.site_info || {}) }
|
||||||
};
|
};
|
||||||
fs.writeFileSync(path.join(dataDir, 'site.config.json'), JSON.stringify(newSite, null, 2), 'utf8');
|
const sitePath = path.join(dataDir, 'site.config.json');
|
||||||
|
const draftPath = path.join(dataDir, 'site.config.draft.json');
|
||||||
|
fs.writeFileSync(sitePath, JSON.stringify(newSite, null, 2), 'utf8');
|
||||||
|
|
||||||
|
// Falls bereits ein Draft existiert, site_info & is_demo_mode dort ebenfalls aktualisieren
|
||||||
|
if (fs.existsSync(draftPath)) {
|
||||||
|
try {
|
||||||
|
const draftContent = JSON.parse(fs.readFileSync(draftPath, 'utf8'));
|
||||||
|
const updatedDraft = { ...draftContent, ...newSite };
|
||||||
|
fs.writeFileSync(draftPath, JSON.stringify(updatedDraft, null, 2), 'utf8');
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (body.themeConfig) {
|
if (body.themeConfig) {
|
||||||
const newTheme = { ...currentTheme, ...body.themeConfig };
|
const newTheme = { ...currentTheme, ...body.themeConfig };
|
||||||
fs.writeFileSync(path.join(dataDir, 'theme.config.json'), JSON.stringify(newTheme, null, 2), 'utf8');
|
fs.writeFileSync(path.join(dataDir, 'theme.config.json'), JSON.stringify(newTheme, null, 2), 'utf8');
|
||||||
|
|||||||
@@ -8,18 +8,29 @@ const DATA_DIR = process.env.DATA_DIR || path.join(process.cwd(), 'app', 'data')
|
|||||||
|
|
||||||
export const POST: APIRoute = async ({ request }) => {
|
export const POST: APIRoute = async ({ request }) => {
|
||||||
try {
|
try {
|
||||||
const draftConfig = await request.json();
|
const draftBody = await request.json();
|
||||||
if (!draftConfig || typeof draftConfig !== 'object') {
|
const draftPath = path.join(DATA_DIR, 'site.config.draft.json');
|
||||||
return new Response(JSON.stringify({ error: 'Ungültige Konfigurationsdaten' }), { status: 400 });
|
const livePath = path.join(DATA_DIR, 'site.config.json');
|
||||||
|
|
||||||
|
let baseConfig: any = {};
|
||||||
|
try {
|
||||||
|
const raw = await fs.readFile(draftPath, 'utf-8');
|
||||||
|
baseConfig = JSON.parse(raw);
|
||||||
|
} catch {
|
||||||
|
try {
|
||||||
|
const rawLive = await fs.readFile(livePath, 'utf-8');
|
||||||
|
baseConfig = JSON.parse(rawLive);
|
||||||
|
} catch {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const draftPath = path.join(DATA_DIR, 'site.config.draft.json');
|
const updatedConfig = { ...baseConfig, ...draftBody, updated_at: new Date().toISOString() };
|
||||||
|
|
||||||
await fs.mkdir(DATA_DIR, { recursive: true });
|
await fs.mkdir(DATA_DIR, { recursive: true });
|
||||||
await fs.writeFile(draftPath, JSON.stringify(draftConfig, null, 2), 'utf-8');
|
await fs.writeFile(draftPath, JSON.stringify(updatedConfig, null, 2), 'utf-8');
|
||||||
|
|
||||||
return new Response(JSON.stringify({ success: true, message: 'Entwurf erfolgreich gespeichert' }), { status: 200 });
|
return new Response(JSON.stringify({ success: true, message: 'Entwurf erfolgreich gespeichert' }), { status: 200 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return new Response(JSON.stringify({ error: 'Fehler beim Speichern des Entwurfs' }), { status: 500 });
|
return new Response(JSON.stringify({ error: 'Fehler beim Speichern des Entwurfs' }), { status: 500 });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user