dify/web/service/use-education.ts

69 lines
2.0 KiB
TypeScript

import type { EducationAddParams } from '@/app/education-apply/types'
import { useMutation, useQuery } from '@tanstack/react-query'
import { get, post } from './base'
import { consoleClient } from './client'
import { useInvalid } from './use-base'
const NAME_SPACE = 'education'
export const useEducationVerify = () => {
return useMutation({
mutationKey: [NAME_SPACE, 'education-verify'],
mutationFn: async () => {
const response = await consoleClient.account.education.verify.get(
{},
{ context: { silent: true } },
)
if (!response.token) throw new Error('Education verification token is missing')
return { token: response.token }
},
})
}
export const useEducationAdd = ({ onSuccess }: { onSuccess?: () => void }) => {
return useMutation({
mutationKey: [NAME_SPACE, 'education-add'],
mutationFn: (params: EducationAddParams) => {
return post<{ message: string }>('/account/education', {
body: params,
})
},
onSuccess,
})
}
type SearchParams = {
keywords?: string
page?: number
limit?: number
}
export const useEducationAutocomplete = () => {
return useMutation({
mutationFn: (searchParams: SearchParams) => {
const { keywords = '', page = 0, limit = 40 } = searchParams
return get<{ data: string[]; has_next: boolean; curr_page: number }>(
`/account/education/autocomplete?keywords=${keywords}&page=${page}&limit=${limit}`,
)
},
})
}
export const useEducationStatus = (disable?: boolean) => {
return useQuery({
enabled: !disable,
queryKey: [NAME_SPACE, 'education-status'],
queryFn: () => {
return get<{ is_student: boolean; allow_refresh: boolean; expire_at: number | null }>(
'/account/education',
)
},
retry: false,
staleTime: 0, // Data expires immediately, ensuring fresh data on refetch
})
}
export const useInvalidateEducationStatus = () => {
return useInvalid([NAME_SPACE, 'education-status'])
}