This commit is contained in:
@@ -670,17 +670,30 @@ export default function AdminPage() {
|
||||
|
||||
// Save to JSON file in background
|
||||
try {
|
||||
console.log('Fetching current pinned data...');
|
||||
const pinnedRes = await fetch('/api/admin/posts', { method: 'GET' });
|
||||
if (!pinnedRes.ok) {
|
||||
throw new Error(`Failed to fetch pinned data: ${pinnedRes.status}`);
|
||||
}
|
||||
const pinnedData = await pinnedRes.json();
|
||||
console.log('Current pinned data:', pinnedData);
|
||||
|
||||
const folderEmojis = pinnedData.folderEmojis || {};
|
||||
folderEmojis[folderPath] = emoji;
|
||||
|
||||
await fetch('/api/admin/posts', {
|
||||
console.log('Updated folderEmojis:', folderEmojis);
|
||||
console.log('Saving to pinned.json...');
|
||||
|
||||
const saveRes = await fetch('/api/admin/posts', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ folderEmojis, pinned: pinnedData.pinned || [] }),
|
||||
});
|
||||
|
||||
if (!saveRes.ok) {
|
||||
throw new Error(`Failed to save emoji: ${saveRes.status}`);
|
||||
}
|
||||
|
||||
console.log('Emoji saved to JSON successfully');
|
||||
} catch (saveError) {
|
||||
console.error('Failed to save emoji to JSON:', saveError);
|
||||
|
||||
@@ -51,12 +51,17 @@ export async function GET(request: Request) {
|
||||
// Return the current pinned.json object
|
||||
try {
|
||||
const pinnedPath = path.join(postsDirectory, 'pinned.json');
|
||||
console.log('Reading pinned.json from:', pinnedPath);
|
||||
let pinnedData = { pinned: [], folderEmojis: {} };
|
||||
if (fs.existsSync(pinnedPath)) {
|
||||
pinnedData = JSON.parse(fs.readFileSync(pinnedPath, 'utf8'));
|
||||
console.log('Successfully read pinned.json with data:', pinnedData);
|
||||
} else {
|
||||
console.log('pinned.json does not exist, using default data');
|
||||
}
|
||||
return NextResponse.json(pinnedData);
|
||||
} catch (error) {
|
||||
console.error('Error reading pinned.json:', error);
|
||||
return NextResponse.json({ error: 'Error reading pinned.json' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -69,7 +74,10 @@ export async function PATCH(request: Request) {
|
||||
return NextResponse.json({ error: 'Invalid pinned or folderEmojis data' }, { status: 400 });
|
||||
}
|
||||
const pinnedPath = path.join(postsDirectory, 'pinned.json');
|
||||
console.log('Saving pinned.json to:', pinnedPath);
|
||||
console.log('Saving data:', { pinned, folderEmojis });
|
||||
fs.writeFileSync(pinnedPath, JSON.stringify({ pinned, folderEmojis }, null, 2), 'utf8');
|
||||
console.log('Successfully saved pinned.json');
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error updating pinned.json:', error);
|
||||
|
||||
Reference in New Issue
Block a user