samar m
feat: frontend topics and upload API layer
decea84
import { apiFetch } from './client'
export interface Batch {
id: string
name: string
instructor_id: string
class_code: string
}
export interface Topic {
id: string
batch_id: string
name: string
is_unlocked: boolean
order_index: number
question_count: number
}
export async function getMyBatch(): Promise<Batch> {
const res = await apiFetch('/api/batches/mine')
if (!res.ok) throw new Error('No batch found')
return res.json()
}
export async function createBatch(name: string): Promise<Batch> {
const res = await apiFetch('/api/batches', {
method: 'POST',
body: JSON.stringify({ name }),
})
if (!res.ok) {
const err = await res.json()
throw new Error(err.detail ?? 'Failed to create batch')
}
return res.json()
}
export async function getTopics(batch_id: string): Promise<Topic[]> {
const res = await apiFetch(`/api/topics?batch_id=${batch_id}`)
if (!res.ok) throw new Error('Failed to load topics')
return res.json()
}
export async function createTopic(batch_id: string, name: string): Promise<Topic> {
const res = await apiFetch('/api/topics', {
method: 'POST',
body: JSON.stringify({ batch_id, name }),
})
if (!res.ok) {
const err = await res.json()
throw new Error(err.detail ?? 'Failed to create topic')
}
return res.json()
}
export async function setTopicUnlock(topic_id: string, is_unlocked: boolean): Promise<Topic> {
const res = await apiFetch(`/api/topics/${topic_id}`, {
method: 'PATCH',
body: JSON.stringify({ is_unlocked }),
})
if (!res.ok) throw new Error('Failed to update topic')
return res.json()
}