dify/web/app/education-apply/components/hooks.ts

41 lines
1.1 KiB
TypeScript
Raw Normal View History

2025-03-13 15:52:43 +08:00
import {
useCallback,
useState,
} from 'react'
import { useDebounceFn } from 'ahooks'
import type { SearchParams } from './types'
import { useEducationAutocomplete } from '@/service/use-education'
export const useEducation = () => {
const {
mutateAsync,
isPending,
} = useEducationAutocomplete()
const [prevSchools, setPrevSchools] = useState<string[]>([])
const handleUpdateSchools = useCallback((searchParams: SearchParams) => {
if (searchParams.keywords) {
mutateAsync(searchParams).then((res) => {
const currentPage = searchParams.page || 1
const resSchools = res.data
if (currentPage > 1)
setPrevSchools(prevSchools => [...(prevSchools || []), ...resSchools])
else
setPrevSchools(resSchools)
})
}
}, [mutateAsync])
const { run: querySchoolsWithDebounced } = useDebounceFn((searchParams: SearchParams) => {
handleUpdateSchools(searchParams)
}, {
wait: 1000,
})
return {
schools: prevSchools,
querySchoolsWithDebounced,
isLoading: isPending,
}
}