- Marked all daily checkbox actions as complete in TODO.md. - Removed outdated mutation methods from `daily-client.ts`, now managed by server actions in `actions/daily.ts`. - Deleted unused API routes for checkbox management, streamlining the codebase. - Updated `useDaily.ts` to utilize server actions with `useTransition`, enhancing performance and user experience.
158 lines
4.3 KiB
TypeScript
158 lines
4.3 KiB
TypeScript
import { httpClient } from './base/http-client';
|
|
import { DailyCheckbox, DailyView, 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;
|
|
}
|
|
|
|
export interface DailySearchFilters {
|
|
query: string;
|
|
limit?: number;
|
|
}
|
|
|
|
// Types conservés pour la compatibilité des hooks d'historique et de recherche
|
|
export interface ReorderCheckboxesData {
|
|
date: Date;
|
|
checkboxIds: string[];
|
|
}
|
|
|
|
/**
|
|
* Client HTTP pour les données Daily (lecture seule)
|
|
* Les mutations sont gérées par les server actions dans actions/daily.ts
|
|
*/
|
|
export class DailyClient {
|
|
/**
|
|
* Récupère la vue daily d'aujourd'hui (hier + aujourd'hui)
|
|
*/
|
|
async getTodaysDailyView(): Promise<DailyView> {
|
|
const result = await httpClient.get<ApiDailyView>('/daily');
|
|
return this.transformDailyViewDates(result);
|
|
}
|
|
|
|
/**
|
|
* Récupère la vue daily pour une date donnée
|
|
*/
|
|
async getDailyView(date: Date): Promise<DailyView> {
|
|
const dateStr = this.formatDateForAPI(date);
|
|
const result = await httpClient.get<ApiDailyView>(`/daily?date=${dateStr}`);
|
|
return this.transformDailyViewDates(result);
|
|
}
|
|
|
|
/**
|
|
* Récupère l'historique des checkboxes
|
|
*/
|
|
async getCheckboxHistory(filters?: DailyHistoryFilters): Promise<{ date: Date; checkboxes: DailyCheckbox[] }[]> {
|
|
const params = new URLSearchParams({ action: 'history' });
|
|
|
|
if (filters?.limit) params.append('limit', filters.limit.toString());
|
|
|
|
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))
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* Recherche dans les checkboxes
|
|
*/
|
|
async searchCheckboxes(filters: DailySearchFilters): Promise<DailyCheckbox[]> {
|
|
const params = new URLSearchParams({
|
|
action: 'search',
|
|
q: filters.query
|
|
});
|
|
|
|
if (filters.limit) params.append('limit', filters.limit.toString());
|
|
|
|
const result = await httpClient.get<ApiCheckbox[]>(`/daily?${params}`);
|
|
return result.map((cb: ApiCheckbox) => this.transformCheckboxDates(cb));
|
|
}
|
|
|
|
/**
|
|
* Formate une date pour l'API (évite les décalages timezone)
|
|
*/
|
|
formatDateForAPI(date: Date): string {
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
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)
|
|
*/
|
|
async getDailyViewByRelativeDate(relative: 'yesterday' | 'today' | 'tomorrow'): Promise<DailyView> {
|
|
const date = new Date();
|
|
|
|
switch (relative) {
|
|
case 'yesterday':
|
|
date.setDate(date.getDate() - 1);
|
|
break;
|
|
case 'tomorrow':
|
|
date.setDate(date.getDate() + 1);
|
|
break;
|
|
// 'today' ne change rien
|
|
}
|
|
|
|
return this.getDailyView(date);
|
|
}
|
|
|
|
/**
|
|
* Récupère toutes les dates qui ont des dailies
|
|
*/
|
|
async getDailyDates(): Promise<string[]> {
|
|
const response = await httpClient.get<{ dates: string[] }>('/daily/dates');
|
|
return response.dates;
|
|
}
|
|
}
|
|
|
|
// Instance singleton du client
|
|
export const dailyClient = new DailyClient(); |