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,
|
2025-03-18 15:10:33 +08:00
|
|
|
data,
|
2025-03-13 15:52:43 +08:00
|
|
|
} = useEducationAutocomplete()
|
|
|
|
|
|
|
|
const [prevSchools, setPrevSchools] = useState<string[]>([])
|
|
|
|
const handleUpdateSchools = useCallback((searchParams: SearchParams) => {
|
|
|
|
if (searchParams.keywords) {
|
|
|
|
mutateAsync(searchParams).then((res) => {
|
2025-03-18 15:10:33 +08:00
|
|
|
const currentPage = searchParams.page || 0
|
2025-03-13 15:52:43 +08:00
|
|
|
const resSchools = res.data
|
2025-03-18 15:10:33 +08:00
|
|
|
if (currentPage > 0)
|
2025-03-13 15:52:43 +08:00
|
|
|
setPrevSchools(prevSchools => [...(prevSchools || []), ...resSchools])
|
|
|
|
else
|
|
|
|
setPrevSchools(resSchools)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [mutateAsync])
|
|
|
|
|
|
|
|
const { run: querySchoolsWithDebounced } = useDebounceFn((searchParams: SearchParams) => {
|
|
|
|
handleUpdateSchools(searchParams)
|
|
|
|
}, {
|
2025-03-18 15:10:33 +08:00
|
|
|
wait: 300,
|
2025-03-13 15:52:43 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
schools: prevSchools,
|
2025-03-18 15:10:33 +08:00
|
|
|
setSchools: setPrevSchools,
|
2025-03-13 15:52:43 +08:00
|
|
|
querySchoolsWithDebounced,
|
2025-03-18 15:10:33 +08:00
|
|
|
handleUpdateSchools,
|
2025-03-13 15:52:43 +08:00
|
|
|
isLoading: isPending,
|
2025-03-18 15:10:33 +08:00
|
|
|
hasNext: data?.has_next,
|
2025-03-13 15:52:43 +08:00
|
|
|
}
|
|
|
|
}
|