mirror of
https://github.com/IRBorisov/ConceptPortal.git
synced 2025-06-26 21:10:38 +03:00
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react'
|
|
|
|
import { type ErrorInfo } from '../components/BackendError';
|
|
import { getRSFormDetails } from '../utils/backendAPI';
|
|
import { IRSForm, IRSFormData,LoadRSFormData } from '../utils/models'
|
|
|
|
export function useRSFormDetails({ target }: { target?: string }) {
|
|
const [schema, setInnerSchema] = useState<IRSForm | undefined>(undefined);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<ErrorInfo>(undefined);
|
|
|
|
function setSchema(data?: IRSFormData) {
|
|
if (!data) {
|
|
setInnerSchema(undefined);
|
|
return;
|
|
}
|
|
const schema = LoadRSFormData(data);
|
|
setInnerSchema(schema);
|
|
}
|
|
|
|
const reload = useCallback(
|
|
(setCustomLoading?: typeof setLoading, callback?: () => void) => {
|
|
setError(undefined);
|
|
if (!target) {
|
|
return;
|
|
}
|
|
getRSFormDetails(target, {
|
|
showError: true,
|
|
setLoading: setCustomLoading ?? setLoading,
|
|
onError: error => {
|
|
setInnerSchema(undefined);
|
|
setError(error);
|
|
},
|
|
onSuccess: schema => {
|
|
setSchema(schema);
|
|
if (callback) callback();
|
|
}
|
|
});
|
|
}, [target]);
|
|
|
|
useEffect(() => {
|
|
reload();
|
|
}, [reload])
|
|
|
|
return { schema, setSchema, reload, error, setError, loading };
|
|
}
|