🐛 fix(user): Update notification and verify code settings
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
'use client';
|
||||
|
||||
import useGlobalStore from '@/config/use-global';
|
||||
import { sendEmailCode, sendSmsCode } from '@/services/common/common';
|
||||
import { Button } from '@workspace/ui/components/button';
|
||||
import { useCountDown } from 'ahooks';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { useState } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
interface SendCodeProps {
|
||||
type: 'email' | 'phone';
|
||||
@@ -17,22 +18,43 @@ interface SendCodeProps {
|
||||
}
|
||||
export default function SendCode({ type, params }: SendCodeProps) {
|
||||
const t = useTranslations('auth');
|
||||
const { common } = useGlobalStore();
|
||||
const { verify_code_interval } = common.verify_code;
|
||||
const [targetDate, setTargetDate] = useState<number>();
|
||||
|
||||
useEffect(() => {
|
||||
const storedEndTime = localStorage.getItem(`verify_code_${type}`);
|
||||
if (storedEndTime) {
|
||||
const endTime = parseInt(storedEndTime);
|
||||
if (endTime > Date.now()) {
|
||||
setTargetDate(endTime);
|
||||
} else {
|
||||
localStorage.removeItem(`verify_code_${type}`);
|
||||
}
|
||||
}
|
||||
}, [type]);
|
||||
|
||||
const [, { seconds }] = useCountDown({
|
||||
targetDate,
|
||||
onEnd: () => {
|
||||
setTargetDate(undefined);
|
||||
localStorage.removeItem(`verify_code_${type}`);
|
||||
},
|
||||
});
|
||||
|
||||
const setCodeTimer = () => {
|
||||
const endTime = Date.now() + verify_code_interval * 1000;
|
||||
setTargetDate(endTime);
|
||||
localStorage.setItem(`verify_code_${type}`, endTime.toString());
|
||||
};
|
||||
|
||||
const getEmailCode = async () => {
|
||||
if (params.email && params.type) {
|
||||
await sendEmailCode({
|
||||
email: params.email,
|
||||
type: params.type,
|
||||
});
|
||||
setTargetDate(Date.now() + 60000);
|
||||
setCodeTimer();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -43,7 +65,7 @@ export default function SendCode({ type, params }: SendCodeProps) {
|
||||
telephone_area_code: params.telephone_area_code,
|
||||
type: params.type,
|
||||
});
|
||||
setTargetDate(Date.now() + 60000);
|
||||
setCodeTimer();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user