2023-07-25 20:27:29 +03:00
|
|
|
import { type AxiosResponse } from 'axios';
|
2023-07-20 17:11:03 +03:00
|
|
|
import { useCallback, useState } from 'react'
|
2023-07-25 20:27:29 +03:00
|
|
|
|
|
|
|
import { type ErrorInfo } from '../components/BackendError';
|
2023-07-20 17:11:03 +03:00
|
|
|
import { postCheckExpression } from '../utils/backendAPI';
|
2023-07-25 20:27:29 +03:00
|
|
|
import { type IRSForm } from '../utils/models';
|
2023-07-20 17:11:03 +03:00
|
|
|
|
2023-07-25 20:27:29 +03:00
|
|
|
function useCheckExpression({ schema }: { schema?: IRSForm }) {
|
2023-07-20 17:11:03 +03:00
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [error, setError] = useState<ErrorInfo>(undefined);
|
|
|
|
const [parseData, setParseData] = useState<any | undefined>(undefined);
|
|
|
|
|
2023-07-25 20:27:29 +03:00
|
|
|
const resetParse = useCallback(() => { setParseData(undefined); }, []);
|
|
|
|
|
2023-07-20 17:11:03 +03:00
|
|
|
async function checkExpression(expression: string, onSuccess?: (response: AxiosResponse) => void) {
|
|
|
|
setError(undefined);
|
|
|
|
setParseData(undefined);
|
2023-07-25 20:27:29 +03:00
|
|
|
await postCheckExpression(String(schema?.id), {
|
|
|
|
data: { expression },
|
2023-07-20 17:11:03 +03:00
|
|
|
showError: true,
|
2023-07-25 20:27:29 +03:00
|
|
|
setLoading,
|
|
|
|
onError: error => { setError(error); },
|
2023-07-26 10:59:55 +03:00
|
|
|
onSuccess: (response) => {
|
2023-07-20 17:11:03 +03:00
|
|
|
setParseData(response.data);
|
|
|
|
if (onSuccess) onSuccess(response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return { parseData, checkExpression, resetParse, error, setError, loading };
|
|
|
|
}
|
|
|
|
|
2023-07-25 20:27:29 +03:00
|
|
|
export default useCheckExpression;
|