Pages corrections pages missions
This commit is contained in:
parent
4e26b958fe
commit
808e1d93cd
@ -498,37 +498,40 @@ export default function CarnetPage() {
|
|||||||
|
|
||||||
// For Diary and Health, ensure title is formatted with date
|
// For Diary and Health, ensure title is formatted with date
|
||||||
let noteTitle = note.title || 'Untitled';
|
let noteTitle = note.title || 'Untitled';
|
||||||
if ((selectedFolder === 'Diary' || selectedFolder === 'Health') && !note.id) {
|
let fileKey: string | undefined;
|
||||||
// For new notes in Diary/Health, use today's date as title (formatted for display)
|
|
||||||
|
if (selectedFolder === 'Diary' || selectedFolder === 'Health') {
|
||||||
|
// For Diary/Health, always use today's date as title (formatted for display)
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
const dateStr = format(today, 'yyyy-MM-dd'); // For filename matching
|
const dateStr = format(today, 'yyyy-MM-dd'); // For filename matching
|
||||||
const dateTitle = format(today, 'd MMMM yyyy', { locale: fr }); // For display: "16 janvier 2026"
|
const dateTitle = format(today, 'd MMMM yyyy', { locale: fr }); // For display: "16 janvier 2026"
|
||||||
noteTitle = dateTitle;
|
noteTitle = dateTitle;
|
||||||
|
|
||||||
// Check if a note with this date already exists
|
// ALWAYS check if a note with this date already exists (even if note.id is set)
|
||||||
|
// This ensures we update the existing note instead of creating a duplicate
|
||||||
const existingNote = notes.find(n => {
|
const existingNote = notes.find(n => {
|
||||||
const title = n.title || '';
|
const title = n.title || '';
|
||||||
return title.startsWith(dateStr) ||
|
const noteId = n.id || '';
|
||||||
|
// Check multiple patterns to find the existing note
|
||||||
|
return title === dateTitle ||
|
||||||
|
title === dateStr ||
|
||||||
|
title.startsWith(dateStr) ||
|
||||||
title.startsWith(dateTitle) ||
|
title.startsWith(dateTitle) ||
|
||||||
n.id?.includes(dateStr);
|
noteId.includes(dateStr) ||
|
||||||
|
noteId.includes(dateTitle.replace(/\s/g, '_'));
|
||||||
});
|
});
|
||||||
|
|
||||||
if (existingNote) {
|
if (existingNote) {
|
||||||
// Update the existing note instead of creating a new one
|
// Update the existing note instead of creating a new one
|
||||||
console.log(`[handleSaveNote] Found existing note for today, updating: ${existingNote.id}`);
|
console.log(`[handleSaveNote] Found existing note for today, updating: ${existingNote.id}`);
|
||||||
note.id = existingNote.id;
|
note.id = existingNote.id;
|
||||||
noteTitle = existingNote.title; // Keep the existing title format
|
noteTitle = existingNote.title || dateTitle; // Keep the existing title format
|
||||||
}
|
// Use the existing note's id as the fileKey
|
||||||
}
|
fileKey = existingNote.id;
|
||||||
|
} else {
|
||||||
// For Diary/Health, always reconstruct filename from formatted title to ensure new format
|
// No existing note found, create new one with formatted date title
|
||||||
// For other folders, use existing id if available, otherwise construct from title
|
// The fileKey will be constructed from the formatted title
|
||||||
let fileKey: string | undefined;
|
fileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${dateTitle}${dateTitle.endsWith('.md') ? '' : '.md'}`;
|
||||||
if (selectedFolder === 'Diary' || selectedFolder === 'Health') {
|
|
||||||
// Always use the formatted date title for the filename (will be sanitized by API)
|
|
||||||
// This ensures we use "16_janvier_2026.md" instead of "2026-01-16.md"
|
|
||||||
if (noteTitle) {
|
|
||||||
fileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${noteTitle}${noteTitle.endsWith('.md') ? '' : '.md'}`;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// For other folders, use existing id or construct from title
|
// For other folders, use existing id or construct from title
|
||||||
@ -552,9 +555,13 @@ export default function CarnetPage() {
|
|||||||
|
|
||||||
// Use direct storage API endpoint
|
// Use direct storage API endpoint
|
||||||
const endpoint = '/api/storage/files';
|
const endpoint = '/api/storage/files';
|
||||||
const method = note.id ? 'PUT' : 'POST';
|
// For Diary/Health, always use PUT (we always have a fileKey, either from existing note or constructed)
|
||||||
|
// For other folders, use PUT if note.id exists, otherwise POST
|
||||||
|
const method = (selectedFolder === 'Diary' || selectedFolder === 'Health')
|
||||||
|
? 'PUT' // Always PUT for Diary/Health since we always have a known fileKey
|
||||||
|
: (note.id ? 'PUT' : 'POST');
|
||||||
|
|
||||||
console.log(`Saving note to ${selectedFolder.toLowerCase()} using ${method}, title: ${noteTitle}`);
|
console.log(`Saving note to ${selectedFolder.toLowerCase()} using ${method}, title: ${noteTitle}, fileKey: ${fileKey}`);
|
||||||
|
|
||||||
const response = await fetch(endpoint, {
|
const response = await fetch(endpoint, {
|
||||||
method,
|
method,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user