2023-07-15 17:46:19 +03:00
|
|
|
import { useCallback, useEffect, useState } from 'react'
|
2023-07-20 17:11:03 +03:00
|
|
|
import { CalculateStats, IRSForm } from '../utils/models'
|
2023-07-15 17:46:19 +03:00
|
|
|
import { ErrorInfo } from '../components/BackendError';
|
2023-07-20 17:11:03 +03:00
|
|
|
import { getRSFormDetails } from '../utils/backendAPI';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
|
|
export function useRSFormDetails({target}: {target?: string}) {
|
|
|
|
const [schema, setSchema] = useState<IRSForm | undefined>();
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [error, setError] = useState<ErrorInfo>(undefined);
|
|
|
|
|
2023-07-23 21:38:04 +03:00
|
|
|
const fetchData = useCallback(
|
|
|
|
async () => {
|
2023-07-15 17:46:19 +03:00
|
|
|
setError(undefined);
|
|
|
|
setSchema(undefined);
|
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-15 17:57:25 +03:00
|
|
|
getRSFormDetails(target, {
|
|
|
|
showError: true,
|
|
|
|
setLoading: setLoading,
|
|
|
|
onError: error => setError(error),
|
2023-07-18 14:55:40 +03:00
|
|
|
onSucccess: (response) => {
|
|
|
|
CalculateStats(response.data)
|
2023-07-23 21:38:04 +03:00
|
|
|
console.log(response.data);
|
2023-07-18 14:55:40 +03:00
|
|
|
setSchema(response.data);
|
|
|
|
}
|
2023-07-15 17:46:19 +03:00
|
|
|
});
|
|
|
|
}, [target]);
|
|
|
|
|
|
|
|
async function reload() {
|
2023-07-23 21:38:04 +03:00
|
|
|
fetchData();
|
2023-07-15 17:46:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-07-23 21:38:04 +03:00
|
|
|
fetchData();
|
2023-07-15 17:46:19 +03:00
|
|
|
}, [fetchData])
|
|
|
|
|
|
|
|
return { schema, reload, error, setError, loading };
|
|
|
|
}
|