feat: enhance DailyClient and useDaily hook for improved checkbox handling

- Added new API response types (`ApiCheckbox`, `ApiDailyView`, `ApiHistoryItem`) for better type safety.
- Updated `getTodaysDailyView`, `getDailyView`, and `getHistory` methods to utilize new types and transform date strings into Date objects.
- Refactored `addCheckbox` and `updateCheckbox` methods to handle checkbox creation and updates with improved error handling.
- Optimized `DailyAddForm` for better UX by removing unnecessary loading states and implementing optimistic UI updates.
- Enhanced `useDaily` hook to support checkbox type management and rollback on errors during updates.
- Updated `DailyPageClient` to leverage new checkbox handling methods for adding tasks.
This commit is contained in:
Julien Froidefond
2025-09-15 22:30:56 +02:00
parent adfef551ab
commit 4b27047e63
4 changed files with 239 additions and 107 deletions

View File

@@ -1,5 +1,30 @@
import { httpClient } from './base/http-client';
import { DailyCheckbox, DailyView, CreateDailyCheckboxData, UpdateDailyCheckboxData } from '@/lib/types';
import { DailyCheckbox, DailyView, CreateDailyCheckboxData, UpdateDailyCheckboxData, Task } from '@/lib/types';
// Types pour les réponses API (avec dates en string)
interface ApiCheckbox {
id: string;
date: string;
text: string;
isChecked: boolean;
type: 'task' | 'meeting';
order: number;
taskId?: string;
task?: Task;
createdAt: string;
updatedAt: string;
}
interface ApiDailyView {
date: string;
yesterday: ApiCheckbox[];
today: ApiCheckbox[];
}
interface ApiHistoryItem {
date: string;
checkboxes: ApiCheckbox[];
}
export interface DailyHistoryFilters {
limit?: number;
@@ -20,7 +45,8 @@ export class DailyClient {
* Récupère la vue daily d'aujourd'hui (hier + aujourd'hui)
*/
async getTodaysDailyView(): Promise<DailyView> {
return httpClient.get('/daily');
const result = await httpClient.get<ApiDailyView>('/daily');
return this.transformDailyViewDates(result);
}
/**
@@ -28,7 +54,8 @@ export class DailyClient {
*/
async getDailyView(date: Date): Promise<DailyView> {
const dateStr = this.formatDateForAPI(date);
return httpClient.get(`/daily?date=${dateStr}`);
const result = await httpClient.get<ApiDailyView>(`/daily?date=${dateStr}`);
return this.transformDailyViewDates(result);
}
/**
@@ -39,7 +66,11 @@ export class DailyClient {
if (filters?.limit) params.append('limit', filters.limit.toString());
return httpClient.get(`/daily?${params}`);
const result = await httpClient.get<ApiHistoryItem[]>(`/daily?${params}`);
return result.map(item => ({
date: new Date(item.date),
checkboxes: item.checkboxes.map((cb: ApiCheckbox) => this.transformCheckboxDates(cb))
}));
}
/**
@@ -53,17 +84,26 @@ export class DailyClient {
if (filters.limit) params.append('limit', filters.limit.toString());
return httpClient.get(`/daily?${params}`);
const result = await httpClient.get<ApiCheckbox[]>(`/daily?${params}`);
return result.map((cb: ApiCheckbox) => this.transformCheckboxDates(cb));
}
/**
* Ajoute une checkbox
*/
async addCheckbox(data: CreateDailyCheckboxData): Promise<DailyCheckbox> {
return httpClient.post('/daily', {
const payload = {
...data,
date: this.formatDateForAPI(data.date)
});
};
try {
const result = await httpClient.post<ApiCheckbox>('/daily', payload);
// Transformer les dates string en objets Date
return this.transformCheckboxDates(result);
} catch (error) {
console.error('❌ DailyClient addCheckbox error:', error);
throw error;
}
}
/**
@@ -95,7 +135,8 @@ export class DailyClient {
* Met à jour une checkbox
*/
async updateCheckbox(checkboxId: string, data: UpdateDailyCheckboxData): Promise<DailyCheckbox> {
return httpClient.patch(`/daily/checkboxes/${checkboxId}`, data);
const result = await httpClient.patch<ApiCheckbox>(`/daily/checkboxes/${checkboxId}`, data);
return this.transformCheckboxDates(result);
}
/**
@@ -132,6 +173,29 @@ export class DailyClient {
return `${year}-${month}-${day}`; // YYYY-MM-DD
}
/**
* Transforme les dates string d'une checkbox en objets Date
*/
private transformCheckboxDates(checkbox: ApiCheckbox): DailyCheckbox {
return {
...checkbox,
date: new Date(checkbox.date),
createdAt: new Date(checkbox.createdAt),
updatedAt: new Date(checkbox.updatedAt)
};
}
/**
* Transforme les dates string d'une vue daily en objets Date
*/
private transformDailyViewDates(view: ApiDailyView): DailyView {
return {
date: new Date(view.date),
yesterday: view.yesterday.map((cb: ApiCheckbox) => this.transformCheckboxDates(cb)),
today: view.today.map((cb: ApiCheckbox) => this.transformCheckboxDates(cb))
};
}
/**
* Récupère la vue daily d'une date relative (hier, aujourd'hui, demain)
*/