File size: 6,379 Bytes
f871fed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
 * Study Plan API client.
 */

import { apiClient } from './client';
import type {
  StudyPlan,
  StudyPlanCreate,
  StudyPlanUpdate,
  StudyPlanFull,
  StudyPlanStats,
  StudyTopic,
  StudyTopicCreate,
  StudyTopicUpdate,
  StudySession,
  StudySessionCreate,
  StudySessionUpdate,
  PlanAdjustment,
  WeeklySchedule,
  PlanGenerationRequest,
  PlanGenerationResult,
} from '../types/study-plan';

// ============ Study Plan API ============

export async function createStudyPlan(data: StudyPlanCreate): Promise<StudyPlan> {
  const response = await apiClient.post<StudyPlan>('/study-plans', data);
  return response.data;
}

export async function generateStudyPlan(request: PlanGenerationRequest): Promise<PlanGenerationResult> {
  const response = await apiClient.post<PlanGenerationResult>('/study-plans/generate', request);
  return response.data;
}

export async function getStudyPlans(notebookId?: string, activeOnly = false): Promise<StudyPlan[]> {
  const params = new URLSearchParams();
  if (notebookId) params.append('notebook_id', notebookId);
  if (activeOnly) params.append('active_only', 'true');

  const response = await apiClient.get<StudyPlan[]>(`/study-plans?${params.toString()}`);
  return response.data;
}

export async function getTodaySessions(planId?: string): Promise<StudySession[]> {
  const params = planId ? `?plan_id=${encodeURIComponent(planId)}` : '';
  const response = await apiClient.get<StudySession[]>(`/study-plans/today${params}`);
  return response.data;
}

export async function getStudyPlan(planId: string): Promise<StudyPlanFull> {
  const response = await apiClient.get<StudyPlanFull>(`/study-plans/${encodeURIComponent(planId)}`);
  return response.data;
}

export async function updateStudyPlan(planId: string, data: StudyPlanUpdate): Promise<StudyPlan> {
  const response = await apiClient.patch<StudyPlan>(`/study-plans/${encodeURIComponent(planId)}`, data);
  return response.data;
}

export async function deleteStudyPlan(planId: string): Promise<void> {
  await apiClient.delete(`/study-plans/${encodeURIComponent(planId)}`);
}

export async function getStudyPlanStats(planId: string): Promise<StudyPlanStats> {
  const response = await apiClient.get<StudyPlanStats>(`/study-plans/${encodeURIComponent(planId)}/stats`);
  return response.data;
}

export async function getWeeklySchedule(planId: string, weekStart?: string): Promise<WeeklySchedule> {
  const params = weekStart ? `?week_start=${weekStart}` : '';
  const response = await apiClient.get<WeeklySchedule>(`/study-plans/${encodeURIComponent(planId)}/schedule${params}`);
  return response.data;
}

// ============ Topic API ============

export async function createTopic(planId: string, data: StudyTopicCreate): Promise<StudyTopic> {
  const response = await apiClient.post<StudyTopic>(`/study-plans/${encodeURIComponent(planId)}/topics`, data);
  return response.data;
}

export async function getTopics(planId: string): Promise<StudyTopic[]> {
  const response = await apiClient.get<StudyTopic[]>(`/study-plans/${encodeURIComponent(planId)}/topics`);
  return response.data;
}

export async function getTopic(topicId: string): Promise<StudyTopic> {
  const response = await apiClient.get<StudyTopic>(`/study-plans/topics/${encodeURIComponent(topicId)}`);
  return response.data;
}

export async function updateTopic(topicId: string, data: StudyTopicUpdate): Promise<StudyTopic> {
  const response = await apiClient.patch<StudyTopic>(`/study-plans/topics/${encodeURIComponent(topicId)}`, data);
  return response.data;
}

export async function deleteTopic(topicId: string): Promise<void> {
  await apiClient.delete(`/study-plans/topics/${encodeURIComponent(topicId)}`);
}

// ============ Session API ============

export async function createSession(planId: string, data: StudySessionCreate): Promise<StudySession> {
  const response = await apiClient.post<StudySession>(`/study-plans/${encodeURIComponent(planId)}/sessions`, data);
  return response.data;
}

export async function getSessions(planId: string): Promise<StudySession[]> {
  const response = await apiClient.get<StudySession[]>(`/study-plans/${encodeURIComponent(planId)}/sessions`);
  return response.data;
}

export async function getSession(sessionId: string): Promise<StudySession> {
  const response = await apiClient.get<StudySession>(`/study-plans/sessions/${encodeURIComponent(sessionId)}`);
  return response.data;
}

export async function updateSession(sessionId: string, data: StudySessionUpdate): Promise<StudySession> {
  const response = await apiClient.patch<StudySession>(`/study-plans/sessions/${encodeURIComponent(sessionId)}`, data);
  return response.data;
}

export async function startSession(sessionId: string): Promise<StudySession> {
  const response = await apiClient.post<StudySession>(`/study-plans/sessions/${encodeURIComponent(sessionId)}/start`);
  return response.data;
}

export async function completeSession(
  sessionId: string,
  rating?: number,
  notes?: string
): Promise<StudySession> {
  const params = new URLSearchParams();
  if (rating !== undefined && rating !== null) params.append('rating', rating.toString());
  if (notes) params.append('notes', notes);

  const response = await apiClient.post<StudySession>(
    `/study-plans/sessions/${encodeURIComponent(sessionId)}/complete?${params.toString()}`
  );
  return response.data;
}

export async function skipSession(sessionId: string, reason?: string): Promise<StudySession> {
  const params = reason ? `?reason=${encodeURIComponent(reason)}` : '';
  const response = await apiClient.post<StudySession>(
    `/study-plans/sessions/${encodeURIComponent(sessionId)}/skip${params}`
  );
  return response.data;
}

export async function deleteSession(sessionId: string): Promise<void> {
  await apiClient.delete(`/study-plans/sessions/${encodeURIComponent(sessionId)}`);
}

// ============ Adjustment API ============

export async function getAdjustments(planId: string): Promise<PlanAdjustment[]> {
  const response = await apiClient.get<PlanAdjustment[]>(`/study-plans/${encodeURIComponent(planId)}/adjustments`);
  return response.data;
}

export async function respondToAdjustment(adjustmentId: string, accepted: boolean): Promise<void> {
  await apiClient.post(`/study-plans/adjustments/${encodeURIComponent(adjustmentId)}/respond`, {
    adjustment_id: adjustmentId,
    accepted,
  });
}