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

34 lines
1.1 KiB
TypeScript
Raw Normal View History

'use client';
import { useCallback, useState } from 'react';
2023-08-23 22:57:25 +03:00
import { ErrorData } from '@/components/InfoError';
import { IResolutionData } from '@/models/language';
import { IRSForm } from '@/models/rsform';
import { DataCallback, postResolveText } from '@/utils/backendAPI';
2023-08-23 22:57:25 +03:00
function useResolveText({ schema }: { schema?: IRSForm }) {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<ErrorData>(undefined);
2023-09-25 14:17:52 +03:00
const [refsData, setRefsData] = useState<IResolutionData | undefined>(undefined);
2023-08-23 22:57:25 +03:00
2023-08-27 16:35:17 +03:00
const resetData = useCallback(() => setRefsData(undefined), []);
2023-08-23 22:57:25 +03:00
2023-09-25 14:17:52 +03:00
function resolveText(text: string, onSuccess?: DataCallback<IResolutionData>) {
2023-08-23 22:57:25 +03:00
setError(undefined);
postResolveText(String(schema!.id), {
data: { text: text },
showError: true,
setLoading,
2023-08-27 16:35:17 +03:00
onError: error => setError(error),
2023-08-23 22:57:25 +03:00
onSuccess: data => {
setRefsData(data);
if (onSuccess) onSuccess(data);
}
});
}
return { refsData, resolveText, resetData, error, setError, loading };
}
export default useResolveText;