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
|
|
|
|
2023-07-15 17:57:25 +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
|
2023-07-15 17:57:25 +03:00
|
|
|
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,
|
2023-07-15 17:57:25 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:57:25 +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;
|