File size: 1,550 Bytes
65d68f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { apiGet, apiPost } from "@/lib/api/client";
import { hasLiveSession } from "@/lib/api/mode";
import type { ClassSessionProgressState } from "@/components/tuition/chapter-dashboard/classSessionProgress";

/**
 * Backend sync for tuition class session progress.
 *
 * localStorage (classSessionProgress.ts) gives instant refresh-restore on the
 * same device; this makes progress survive a device change / re-login. Both
 * helpers are best-effort and no-op when logged out.
 */

type ClassSessionProgressRemote = {
  class_session_id: string;
  data: ClassSessionProgressState;
  updated_at: string;
};

/** Returns true when the progress was persisted to the backend. */
export async function saveClassSessionProgressRemote(
  id: string,
  state: ClassSessionProgressState,
): Promise<boolean> {
  if (!hasLiveSession()) return false;
  try {
    await apiPost<ClassSessionProgressRemote, { class_session_id: string; data: ClassSessionProgressState }>(
      "/class-session-progress",
      { class_session_id: id, data: state },
    );
    return true;
  } catch {
    return false;
  }
}

/** Returns the saved progress for one class session, or null. */
export async function getClassSessionProgressRemote(id: string): Promise<ClassSessionProgressState | null> {
  if (!hasLiveSession()) return null;
  try {
    const rows = await apiGet<ClassSessionProgressRemote[]>("/class-session-progress");
    const row = (rows ?? []).find((item) => item.class_session_id === id);
    return row?.data ?? null;
  } catch {
    return null;
  }
}