fetch practice integration fix

This commit is contained in:
Yared Yemane 2026-04-07 03:38:18 -07:00
parent e8c601985b
commit dbfb4307fb

View File

@ -42,18 +42,34 @@ export function PracticeDetailsPage() {
const fetchPractices = useCallback(async () => { const fetchPractices = useCallback(async () => {
setLoadingList(true) setLoadingList(true)
try { try {
const res = await getQuestionSets({ set_type: "PRACTICE" }) const batchSize = 100
const payload = res.data?.data as unknown let offset = 0
let sets: QuestionSet[] = [] let total = Number.POSITIVE_INFINITY
if (Array.isArray(payload)) { const sets: QuestionSet[] = []
sets = payload as QuestionSet[]
} else if ( while (sets.length < total) {
payload && const res = await getQuestionSets({ set_type: "PRACTICE", limit: batchSize, offset })
typeof payload === "object" && const payload = res.data?.data as unknown
Array.isArray((payload as { question_sets?: unknown[] }).question_sets) let chunk: QuestionSet[] = []
) { let chunkTotal = 0
sets = (payload as { question_sets: QuestionSet[] }).question_sets if (Array.isArray(payload)) {
chunk = payload as QuestionSet[]
chunkTotal = chunk.length
} else if (
payload &&
typeof payload === "object" &&
Array.isArray((payload as { question_sets?: unknown[] }).question_sets)
) {
const mapped = payload as { question_sets: QuestionSet[]; total_count?: number }
chunk = mapped.question_sets
chunkTotal = mapped.total_count ?? chunk.length
}
sets.push(...chunk)
total = chunkTotal
if (chunk.length < batchSize) break
offset += chunk.length
} }
setPractices(sets) setPractices(sets)
if (sets.length > 0) { if (sets.length > 0) {
setSelectedPracticeId((prev) => prev ?? sets[0].id) setSelectedPracticeId((prev) => prev ?? sets[0].id)