ConceptPortal-public/rsconcept/frontend/src/hooks/useNewRSForm.ts

33 lines
810 B
TypeScript
Raw Normal View History

2023-07-15 17:46:19 +03:00
import { useState } from 'react'
2023-07-25 20:27:29 +03:00
import { type ErrorInfo } from '../components/BackendError';
2023-07-20 17:11:03 +03:00
import { postNewRSForm } from '../utils/backendAPI';
2023-07-15 17:46:19 +03:00
function useNewRSForm() {
2023-07-15 17:46:19 +03:00
const [loading, setLoading] = useState(false);
const [error, setError] = useState<ErrorInfo>(undefined);
2023-07-25 20:27:29 +03:00
async function createSchema({ data, file, onSuccess }: {
data: any
file?: File
onSuccess: (newID: string) => void
}) {
2023-07-15 17:46:19 +03:00
setError(undefined);
if (file) {
2023-07-25 20:27:29 +03:00
data.file = file;
data.fileName = file.name;
2023-07-15 17:46:19 +03:00
}
2023-07-25 20:27:29 +03:00
await postNewRSForm({
data,
showError: true,
2023-07-25 20:27:29 +03:00
setLoading,
onError: error => { setError(error); },
onSucccess: response => { onSuccess(response.data.id); }
2023-07-15 17:46:19 +03:00
});
}
return { createSchema, error, setError, loading };
2023-07-15 17:46:19 +03:00
}
2023-07-25 20:27:29 +03:00
export default useNewRSForm;