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

39 lines
1.0 KiB
TypeScript
Raw Normal View History

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);
const fetchData = useCallback(
async () => {
2023-07-15 17:46:19 +03:00
setError(undefined);
setSchema(undefined);
if (!target) {
return;
}
getRSFormDetails(target, {
showError: true,
setLoading: setLoading,
onError: error => setError(error),
2023-07-18 14:55:40 +03:00
onSucccess: (response) => {
CalculateStats(response.data)
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() {
fetchData();
2023-07-15 17:46:19 +03:00
}
useEffect(() => {
fetchData();
2023-07-15 17:46:19 +03:00
}, [fetchData])
return { schema, reload, error, setError, loading };
}