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

34 lines
938 B
TypeScript
Raw Normal View History

2023-07-15 17:46:19 +03:00
import { useCallback, useEffect, useState } from 'react'
import { IRSForm } from '../models'
import { ErrorInfo } from '../components/BackendError';
import { getRSFormDetails } from '../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 () => {
setError(undefined);
setSchema(undefined);
if (!target) {
return;
}
getRSFormDetails(target, {
showError: true,
setLoading: setLoading,
onError: error => setError(error),
onSucccess: response => setSchema(response.data)
2023-07-15 17:46:19 +03:00
});
}, [target]);
async function reload() {
fetchData()
}
useEffect(() => {
fetchData()
}, [fetchData])
return { schema, reload, error, setError, loading };
}