feat: update Daily management features and enhance date handling
- Marked the calendar/history view of dailies as completed in the TODO list. - Improved date formatting in `formatDateForAPI` to avoid timezone issues. - Added `getDailyDates` method in `DailyClient` and `DailyService` to retrieve all dates with dailies. - Enhanced `POST` route to robustly parse date input for better error handling. - Integrated daily dates loading in `DailyPageClient` for calendar display.
This commit is contained in:
20
src/app/api/daily/dates/route.ts
Normal file
20
src/app/api/daily/dates/route.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { dailyService } from '@/services/daily';
|
||||
|
||||
/**
|
||||
* API route pour récupérer toutes les dates avec des dailies
|
||||
* GET /api/daily/dates
|
||||
*/
|
||||
export async function GET() {
|
||||
try {
|
||||
const dates = await dailyService.getDailyDates();
|
||||
return NextResponse.json({ dates });
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération des dates:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur interne du serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,16 @@ export async function POST(request: Request) {
|
||||
);
|
||||
}
|
||||
|
||||
const date = new Date(body.date);
|
||||
// Parser la date de façon plus robuste
|
||||
let date: Date;
|
||||
if (typeof body.date === 'string') {
|
||||
// Si c'est une string YYYY-MM-DD, créer une date locale
|
||||
const [year, month, day] = body.date.split('-').map(Number);
|
||||
date = new Date(year, month - 1, day); // month est 0-indexé
|
||||
} else {
|
||||
date = new Date(body.date);
|
||||
}
|
||||
|
||||
if (isNaN(date.getTime())) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Format de date invalide. Utilisez YYYY-MM-DD' },
|
||||
@@ -93,4 +102,4 @@ export async function POST(request: Request) {
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useRef } from 'react';
|
||||
import { useState, useRef, useEffect } from 'react';
|
||||
import React from 'react';
|
||||
import { useDaily } from '@/hooks/useDaily';
|
||||
import { DailyCheckbox } from '@/lib/types';
|
||||
@@ -8,8 +8,7 @@ import { Button } from '@/components/ui/Button';
|
||||
import { Input } from '@/components/ui/Input';
|
||||
import { Card } from '@/components/ui/Card';
|
||||
import Link from 'next/link';
|
||||
import { formatDistanceToNow } from 'date-fns';
|
||||
import { fr } from 'date-fns/locale';
|
||||
import { DailyCalendar } from '@/components/daily/DailyCalendar';
|
||||
|
||||
interface DailySectionProps {
|
||||
title: string;
|
||||
@@ -212,22 +211,62 @@ export function DailyPageClient() {
|
||||
error,
|
||||
saving,
|
||||
currentDate,
|
||||
addTodayCheckbox,
|
||||
addYesterdayCheckbox,
|
||||
refreshDaily,
|
||||
toggleCheckbox,
|
||||
updateCheckbox,
|
||||
deleteCheckbox,
|
||||
goToPreviousDay,
|
||||
goToNextDay,
|
||||
goToToday
|
||||
goToToday,
|
||||
setDate
|
||||
} = useDaily();
|
||||
|
||||
const [dailyDates, setDailyDates] = useState<string[]>([]);
|
||||
|
||||
// Charger les dates avec des dailies pour le calendrier
|
||||
useEffect(() => {
|
||||
import('@/clients/daily-client').then(({ dailyClient }) => {
|
||||
return dailyClient.getDailyDates();
|
||||
}).then(setDailyDates).catch(console.error);
|
||||
}, []);
|
||||
|
||||
const handleAddTodayCheckbox = async (text: string) => {
|
||||
await addTodayCheckbox(text);
|
||||
try {
|
||||
const { dailyClient } = await import('@/clients/daily-client');
|
||||
await dailyClient.addCheckbox({
|
||||
date: currentDate,
|
||||
text,
|
||||
isChecked: false
|
||||
});
|
||||
// Recharger la vue daily après ajout
|
||||
await refreshDaily();
|
||||
// Recharger aussi les dates pour le calendrier
|
||||
const updatedDates = await dailyClient.getDailyDates();
|
||||
setDailyDates(updatedDates);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de l\'ajout de la tâche:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const handleAddYesterdayCheckbox = async (text: string) => {
|
||||
await addYesterdayCheckbox(text);
|
||||
try {
|
||||
const yesterday = new Date(currentDate);
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
|
||||
const { dailyClient } = await import('@/clients/daily-client');
|
||||
await dailyClient.addCheckbox({
|
||||
date: yesterday,
|
||||
text,
|
||||
isChecked: false
|
||||
});
|
||||
// Recharger la vue daily après ajout
|
||||
await refreshDaily();
|
||||
// Recharger aussi les dates pour le calendrier
|
||||
const updatedDates = await dailyClient.getDailyDates();
|
||||
setDailyDates(updatedDates);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de l\'ajout de la tâche:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const handleToggleCheckbox = async (checkboxId: string) => {
|
||||
@@ -252,6 +291,10 @@ export function DailyPageClient() {
|
||||
return currentDate;
|
||||
};
|
||||
|
||||
const handleDateSelect = (date: Date) => {
|
||||
setDate(date);
|
||||
};
|
||||
|
||||
const formatCurrentDate = () => {
|
||||
return currentDate.toLocaleDateString('fr-FR', {
|
||||
weekday: 'long',
|
||||
@@ -330,7 +373,7 @@ export function DailyPageClient() {
|
||||
onClick={goToToday}
|
||||
className="text-xs text-[var(--primary)] hover:text-[var(--primary)]/80 font-mono"
|
||||
>
|
||||
Aller à aujourd'hui
|
||||
Aller à aujourd'hui
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
@@ -350,8 +393,19 @@ export function DailyPageClient() {
|
||||
|
||||
{/* Contenu principal */}
|
||||
<main className="container mx-auto px-4 py-8">
|
||||
{dailyView && (
|
||||
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
||||
<div className="grid grid-cols-1 xl:grid-cols-3 gap-6">
|
||||
{/* Calendrier - toujours visible */}
|
||||
<div className="xl:col-span-1">
|
||||
<DailyCalendar
|
||||
currentDate={currentDate}
|
||||
onDateSelect={handleDateSelect}
|
||||
dailyDates={dailyDates}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Sections daily */}
|
||||
{dailyView && (
|
||||
<div className="xl:col-span-2 grid grid-cols-1 lg:grid-cols-2 gap-6">
|
||||
{/* Section Hier */}
|
||||
<DailySectionComponent
|
||||
title="📋 Hier"
|
||||
@@ -375,8 +429,9 @@ export function DailyPageClient() {
|
||||
onDeleteCheckbox={handleDeleteCheckbox}
|
||||
saving={saving}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Footer avec stats */}
|
||||
{dailyView && (
|
||||
|
||||
Reference in New Issue
Block a user