Files
History-client/api/sections.ts
2026-04-20 23:27:38 +07:00

133 lines
4.3 KiB
TypeScript

import { API_ENDPOINTS } from "@/api/config";
import { jsonRequestInit, requestJson } from "@/api/http";
import type {
CreateCommitInput,
CreateSectionInput,
EditorLoadResponse,
RestoreCommitInput,
Section,
SectionCommit,
SectionState,
SectionSubmission,
} from "@/types/sections";
export type {
CreateCommitInput,
CreateSectionInput,
EditorLoadResponse,
RestoreCommitInput,
Section,
SectionCommit,
SectionState,
SectionSubmission,
} from "@/types/sections";
export async function fetchSections(): Promise<Section[]> {
return requestJson<Section[]>(API_ENDPOINTS.sections);
}
export async function createSection(input: CreateSectionInput): Promise<Section> {
return requestJson<Section>(API_ENDPOINTS.sections, jsonRequestInit("POST", input));
}
export async function openSectionEditor(sectionId: string, userId?: string): Promise<EditorLoadResponse> {
const params = new URLSearchParams();
if (userId) params.set("user_id", userId);
return requestJson<EditorLoadResponse>(sectionUrl(sectionId, "editor", params));
}
export async function lockSection(sectionId: string, userId: string): Promise<{ state: SectionState }> {
return requestJson<{ state: SectionState }>(
sectionUrl(sectionId, "lock"),
jsonRequestInit("POST", { user_id: userId })
);
}
export async function unlockSection(sectionId: string, userId: string): Promise<{ success: boolean }> {
return requestJson<{ success: boolean }>(
sectionUrl(sectionId, "unlock"),
jsonRequestInit("POST", { user_id: userId })
);
}
export async function createSectionCommit(
sectionId: string,
input: CreateCommitInput
): Promise<{ commit: SectionCommit; state: SectionState }> {
return requestJson<{ commit: SectionCommit; state: SectionState }>(
sectionUrl(sectionId, "commits"),
jsonRequestInit("POST", input)
);
}
export async function fetchSectionCommits(
sectionId: string,
options?: { includeSnapshot?: boolean }
): Promise<SectionCommit[]> {
const params = new URLSearchParams();
if (options?.includeSnapshot) params.set("include_snapshot", "1");
return requestJson<SectionCommit[]>(sectionUrl(sectionId, "commits", params));
}
export async function restoreSectionCommit(
sectionId: string,
input: RestoreCommitInput
): Promise<{ commit: SectionCommit; state: SectionState }> {
return requestJson<{ commit: SectionCommit; state: SectionState }>(
sectionUrl(sectionId, "restore"),
jsonRequestInit("POST", input)
);
}
export async function submitSection(
sectionId: string,
input: { submitted_by?: string; user_id?: string }
): Promise<SectionSubmission> {
return requestJson<SectionSubmission>(sectionUrl(sectionId, "submit"), jsonRequestInit("POST", input));
}
export async function fetchSectionSubmissions(
sectionId: string,
options?: { includeSnapshot?: boolean }
): Promise<SectionSubmission[]> {
const params = new URLSearchParams();
if (options?.includeSnapshot) params.set("include_snapshot", "1");
return requestJson<SectionSubmission[]>(sectionUrl(sectionId, "submissions", params));
}
export async function approveSubmission(
submissionId: string,
input: { reviewed_by?: string; user_id?: string; review_note?: string | null }
): Promise<SectionSubmission> {
return requestJson<SectionSubmission>(
submissionUrl(submissionId, "approve"),
jsonRequestInit("POST", input)
);
}
export async function rejectSubmission(
submissionId: string,
input: { reviewed_by?: string; user_id?: string; review_note?: string | null }
): Promise<SectionSubmission> {
return requestJson<SectionSubmission>(
submissionUrl(submissionId, "reject"),
jsonRequestInit("POST", input)
);
}
function sectionUrl(sectionId: string, path?: string, params?: URLSearchParams): string {
return appendQuery(
`${API_ENDPOINTS.sections}/${encodeURIComponent(sectionId)}${path ? `/${path}` : ""}`,
params
);
}
function submissionUrl(submissionId: string, path: "approve" | "reject"): string {
return `${API_ENDPOINTS.submissions}/${encodeURIComponent(submissionId)}/${path}`;
}
function appendQuery(url: string, params?: URLSearchParams): string {
const suffix = params?.toString();
return suffix ? `${url}?${suffix}` : url;
}