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

56 lines
1.5 KiB
TypeScript
Raw Normal View History

'use client';
import { useCallback, useState } from 'react';
2023-09-25 14:17:52 +03:00
import { ErrorData } from '@/components/InfoError';
import { ILexemeData, ITextRequest, ITextResult, IWordFormPlain } from '@/models/language';
import { DataCallback, postGenerateLexeme, postInflectText, postParseText } from '@/utils/backendAPI';
2023-09-25 14:17:52 +03:00
function useConceptText() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<ErrorData>(undefined);
2023-09-25 14:17:52 +03:00
2023-12-28 14:04:44 +03:00
const inflect = useCallback((data: IWordFormPlain, onSuccess: DataCallback<ITextResult>) => {
2023-09-25 14:17:52 +03:00
setError(undefined);
postInflectText({
data: data,
showError: true,
setLoading,
2024-01-04 22:10:57 +03:00
onError: setError,
2023-09-25 14:17:52 +03:00
onSuccess: data => {
if (onSuccess) onSuccess(data);
}
});
}, []);
2023-12-28 14:04:44 +03:00
const parse = useCallback((data: ITextRequest, onSuccess: DataCallback<ITextResult>) => {
2023-09-25 14:17:52 +03:00
setError(undefined);
postParseText({
data: data,
showError: true,
setLoading,
2024-01-04 22:10:57 +03:00
onError: setError,
2023-09-25 14:17:52 +03:00
onSuccess: data => {
if (onSuccess) onSuccess(data);
}
});
}, []);
2023-12-28 14:04:44 +03:00
const generateLexeme = useCallback((data: ITextRequest, onSuccess: DataCallback<ILexemeData>) => {
2023-09-25 14:17:52 +03:00
setError(undefined);
postGenerateLexeme({
data: data,
showError: true,
setLoading,
2024-01-04 22:10:57 +03:00
onError: setError,
2023-09-25 14:17:52 +03:00
onSuccess: data => {
if (onSuccess) onSuccess(data);
}
});
}, []);
return { inflect, parse, generateLexeme, error, setError, loading };
}
2023-12-28 14:04:44 +03:00
export default useConceptText;