- 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.
164 lines
4.2 KiB
TypeScript
164 lines
4.2 KiB
TypeScript
import { httpClient } from './base/http-client';
|
|
import { DailyCheckbox, DailyView, CreateDailyCheckboxData, UpdateDailyCheckboxData } from '@/lib/types';
|
|
|
|
export interface DailyHistoryFilters {
|
|
limit?: number;
|
|
}
|
|
|
|
export interface DailySearchFilters {
|
|
query: string;
|
|
limit?: number;
|
|
}
|
|
|
|
export interface ReorderCheckboxesData {
|
|
date: Date;
|
|
checkboxIds: string[];
|
|
}
|
|
|
|
export class DailyClient {
|
|
/**
|
|
* Récupère la vue daily d'aujourd'hui (hier + aujourd'hui)
|
|
*/
|
|
async getTodaysDailyView(): Promise<DailyView> {
|
|
return httpClient.get('/daily');
|
|
}
|
|
|
|
/**
|
|
* Récupère la vue daily pour une date donnée
|
|
*/
|
|
async getDailyView(date: Date): Promise<DailyView> {
|
|
const dateStr = this.formatDateForAPI(date);
|
|
return httpClient.get(`/daily?date=${dateStr}`);
|
|
}
|
|
|
|
/**
|
|
* 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());
|
|
|
|
return httpClient.get(`/daily?${params}`);
|
|
}
|
|
|
|
/**
|
|
* 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());
|
|
|
|
return httpClient.get(`/daily?${params}`);
|
|
}
|
|
|
|
/**
|
|
* Ajoute une checkbox
|
|
*/
|
|
async addCheckbox(data: CreateDailyCheckboxData): Promise<DailyCheckbox> {
|
|
return httpClient.post('/daily', {
|
|
...data,
|
|
date: this.formatDateForAPI(data.date)
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Ajoute une checkbox pour aujourd'hui
|
|
*/
|
|
async addTodayCheckbox(text: string, taskId?: string): Promise<DailyCheckbox> {
|
|
return this.addCheckbox({
|
|
date: new Date(),
|
|
text,
|
|
taskId
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Ajoute une checkbox pour hier
|
|
*/
|
|
async addYesterdayCheckbox(text: string, taskId?: string): Promise<DailyCheckbox> {
|
|
const yesterday = new Date();
|
|
yesterday.setDate(yesterday.getDate() - 1);
|
|
|
|
return this.addCheckbox({
|
|
date: yesterday,
|
|
text,
|
|
taskId
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Met à jour une checkbox
|
|
*/
|
|
async updateCheckbox(checkboxId: string, data: UpdateDailyCheckboxData): Promise<DailyCheckbox> {
|
|
return httpClient.patch(`/daily/checkboxes/${checkboxId}`, data);
|
|
}
|
|
|
|
/**
|
|
* Supprime une checkbox
|
|
*/
|
|
async deleteCheckbox(checkboxId: string): Promise<void> {
|
|
return httpClient.delete(`/daily/checkboxes/${checkboxId}`);
|
|
}
|
|
|
|
/**
|
|
* Réordonne les checkboxes d'une date
|
|
*/
|
|
async reorderCheckboxes(data: ReorderCheckboxesData): Promise<void> {
|
|
return httpClient.post('/daily/checkboxes', {
|
|
date: this.formatDateForAPI(data.date),
|
|
checkboxIds: data.checkboxIds
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Coche/décoche une checkbox (raccourci)
|
|
*/
|
|
async toggleCheckbox(checkboxId: string, isChecked: boolean): Promise<DailyCheckbox> {
|
|
return this.updateCheckbox(checkboxId, { isChecked });
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
}
|
|
|
|
/**
|
|
* 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(); |