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

41 lines
1.1 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, setInnerSchema] = useState<IRSForm | undefined>(undefined);
2023-07-15 17:46:19 +03:00
const [loading, setLoading] = useState(false);
const [error, setError] = useState<ErrorInfo>(undefined);
function setSchema(schema?: IRSForm) {
if (schema) CalculateStats(schema);
setInnerSchema(schema);
console.log(schema);
}
const fetchData = useCallback(
async () => {
2023-07-15 17:46:19 +03:00
setError(undefined);
setInnerSchema(undefined);
2023-07-15 17:46:19 +03:00
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();
2023-07-15 17:46:19 +03:00
}
useEffect(() => {
fetchData();
2023-07-15 17:46:19 +03:00
}, [fetchData])
return { schema, setSchema, reload, error, setError, loading };
2023-07-15 17:46:19 +03:00
}