All files / api client.ts

0% Statements 0/19
0% Branches 0/8
0% Functions 0/6
0% Lines 0/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                           
const API_BASE = "/api";
 
export interface Assessment {
  id: string;
  pl_assessment_id: string;
  name: string;
}
 
export interface Question {
  id: string;
  assessment_id: string;
  pl_question_uuid: string;
  title: string;
}
 
export interface QuestionConcept {
  id: string;
  question_id: string;
  concept_id: string;
  subconcept_label: string | null;
}
 
export async function fetchAssessments(): Promise<Assessment[]> {
  const res = await fetch(`${API_BASE}/assessments`);
  return res.json();
}
 
export async function fetchQuestions(
  assessmentId: string,
): Promise<Question[]> {
  const res = await fetch(`${API_BASE}/assessments/${assessmentId}/questions`);
  return res.json();
}
 
export async function fetchQuestionConcepts(
  questionId: string,
): Promise<QuestionConcept[]> {
  const res = await fetch(`${API_BASE}/questions/${questionId}/concepts`);
  return res.json();
}
 
export interface UserStateResponse {
  starred_ids: string[];
  detail_cards: unknown[];
  mastered_subconcepts: string[];
}
 
export async function fetchUserState(
  userid: number,
): Promise<UserStateResponse | null> {
  const res = await fetch(`${API_BASE}/user-state/${userid}`);
  if (res.status === 404) return null;
  if (!res.ok)
    throw new Error(`Failed to fetch user state for userid ${userid}`);
  return res.json();
}
 
export async function saveUserState(body: {
  userid: number;
  starred_ids: string[];
  detail_cards: unknown[];
  mastered_subconcepts: string[];
}): Promise<void> {
  const res = await fetch(`${API_BASE}/user-state`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(body),
  });
  if (!res.ok)
    throw new Error(`Failed to save user state for userid ${body.userid}`);
}
 
export async function logUserActivity(body: {
  userid: number;
  event_type: string;
  payload: object;
}): Promise<void> {
  const res = await fetch(`${API_BASE}/user-activity`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(body),
  });
  if (!res.ok)
    throw new Error(`Failed to log user activity for userid ${body.userid}`);
}