mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-02 20:08:17 +03:00
41 lines
917 B
TypeScript
41 lines
917 B
TypeScript
import { FileUploadImportTypes, ImportDocument } from "tachi-common";
|
|
import { ImportStates } from "types/import";
|
|
import { SetState } from "types/react";
|
|
import { APIFetchV1 } from "./api";
|
|
|
|
export default async function SubmitFile(
|
|
type: FileUploadImportTypes,
|
|
file: File,
|
|
setState: SetState<ImportStates>,
|
|
moreData: Record<string, string> = {}
|
|
) {
|
|
const formData = new FormData();
|
|
formData.append("importType", type);
|
|
formData.append("scoreData", file);
|
|
|
|
for (const [key, value] of Object.entries(moreData)) {
|
|
formData.append(key, value);
|
|
}
|
|
|
|
setState({ state: "waiting_init" });
|
|
|
|
const res = await APIFetchV1<ImportDocument>(
|
|
"/import/file",
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"X-User-Intent": "true",
|
|
},
|
|
body: formData,
|
|
},
|
|
true,
|
|
true
|
|
);
|
|
|
|
if (res.success) {
|
|
setState({ state: "done", import: res.body });
|
|
} else {
|
|
setState({ state: "failed", error: res.description });
|
|
}
|
|
}
|