From 5fa8709989221c95d8834eb648658643f6c87646 Mon Sep 17 00:00:00 2001 From: zxhlyh Date: Tue, 18 Mar 2025 16:01:25 +0800 Subject: [PATCH] fix: education verify --- web/app/components/billing/plan/index.tsx | 11 ++++++----- .../components/education-apply-page.tsx | 11 +++++++---- web/app/education-apply/page.tsx | 17 +++++++++++------ web/service/use-education.ts | 7 +++---- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/web/app/components/billing/plan/index.tsx b/web/app/components/billing/plan/index.tsx index bdea0066bc..98e68541af 100644 --- a/web/app/components/billing/plan/index.tsx +++ b/web/app/components/billing/plan/index.tsx @@ -22,6 +22,7 @@ import Button from '@/app/components/base/button' import UsageInfo from '@/app/components/billing/usage-info' import VerifyStateModal from '@/app/education-apply/components/verify-state-modal' import { EDUCATION_VERIFYING_LOCALSTORAGE_ITEM } from '@/app/education-apply/components/constants' +import { useEducationVerify } from '@/service/use-education' type Props = { loc: string @@ -44,14 +45,14 @@ const PlanComp: FC = ({ } = plan const [showModal, setShowModal] = React.useState(false) + const { mutateAsync } = useEducationVerify() const handleVerify = () => { - if (userProfile.email.endsWith('.edu')) { + mutateAsync().then((res) => { localStorage.removeItem(EDUCATION_VERIFYING_LOCALSTORAGE_ITEM) - router.push('/education-apply') - } - else { + router.push(`/education-apply?token=${res.token}`) + }).catch(() => { setShowModal(true) - } + }) } return (
diff --git a/web/app/education-apply/components/education-apply-page.tsx b/web/app/education-apply/components/education-apply-page.tsx index 7c85e51edc..c83c3b8b35 100644 --- a/web/app/education-apply/components/education-apply-page.tsx +++ b/web/app/education-apply/components/education-apply-page.tsx @@ -4,7 +4,10 @@ import { useState, } from 'react' import { useTranslation } from 'react-i18next' -import { useRouter } from 'next/navigation' +import { + useRouter, + useSearchParams, +} from 'next/navigation' import UserInfo from './user-info' import SearchInput from './search-input' import RoleSelector from './role-selector' @@ -13,7 +16,6 @@ import Button from '@/app/components/base/button' import Checkbox from '@/app/components/base/checkbox' import { useEducationAdd, - useEducationVerify, useInvalidateEducationStatus, } from '@/service/use-education' import { useProviderContext } from '@/context/provider-context' @@ -31,7 +33,6 @@ const EducationApplyAge = () => { mutateAsync: educationAdd, } = useEducationAdd({ onSuccess: () => {} }) const [modalShow, setShowModal] = useState void }>(undefined) - const { data } = useEducationVerify() const { onPlanInfoChanged } = useProviderContext() const updateEducationStatus = useInvalidateEducationStatus() const { notify } = useToastContext() @@ -45,9 +46,11 @@ const EducationApplyAge = () => { router.replace('/') } + const searchParams = useSearchParams() + const token = searchParams.get('token') const handleSubmit = () => { educationAdd({ - token: data?.token || '', + token: token || '', role, institution: schoolName, }).then((res) => { diff --git a/web/app/education-apply/page.tsx b/web/app/education-apply/page.tsx index 7f55fd3f31..fb5f26f206 100644 --- a/web/app/education-apply/page.tsx +++ b/web/app/education-apply/page.tsx @@ -4,21 +4,26 @@ import { useEffect, useMemo, } from 'react' -import { useRouter } from 'next/navigation' +import { + useRouter, + useSearchParams, +} from 'next/navigation' import EducationApplyAge from './components/education-apply-page' import { useProviderContext } from '@/context/provider-context' export default function EducationApply() { const router = useRouter() const { enableEducationPlan, isEducationAccount } = useProviderContext() - const hiddenEducationApply = useMemo(() => { - return enableEducationPlan && isEducationAccount - }, [enableEducationPlan, isEducationAccount]) + const searchParams = useSearchParams() + const token = searchParams.get('token') + const showEducationApplyPage = useMemo(() => { + return enableEducationPlan && !isEducationAccount && token + }, [enableEducationPlan, isEducationAccount, token]) useEffect(() => { - if (hiddenEducationApply) + if (!showEducationApplyPage) router.replace('/') - }, [hiddenEducationApply, router]) + }, [showEducationApplyPage, router]) return } diff --git a/web/service/use-education.ts b/web/service/use-education.ts index 73e037c3f9..339f64a26c 100644 --- a/web/service/use-education.ts +++ b/web/service/use-education.ts @@ -9,12 +9,11 @@ import type { EducationAddParams } from '@/app/education-apply/components/types' const NAME_SPACE = 'education' export const useEducationVerify = () => { - return useQuery({ - queryKey: [NAME_SPACE, 'education-verify'], - queryFn: () => { + return useMutation({ + mutationKey: [NAME_SPACE, 'education-verify'], + mutationFn: () => { return get<{ token: string }>('/account/education/verify') }, - retry: false, }) }