feat: initialise template
This commit is contained in:
55
src/lib/utils/auth/hooks/useSendEmailCode.ts
Normal file
55
src/lib/utils/auth/hooks/useSendEmailCode.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
// useLogin.ts
|
||||
import { useState, useCallback } from 'react';
|
||||
import {
|
||||
ApiError,
|
||||
APIResponse,
|
||||
apiClient,
|
||||
isApiResponse,
|
||||
isApiError,
|
||||
OTPTypes,
|
||||
} from '..';
|
||||
|
||||
export interface SendEmailCodeRequest {
|
||||
email: string;
|
||||
kind: number;
|
||||
}
|
||||
|
||||
interface UseSendEmailCode {
|
||||
isLoading: boolean;
|
||||
error: ApiError | null;
|
||||
data: APIResponse | null;
|
||||
sendEmailCode: (email: string, kind: number) => Promise<void>;
|
||||
}
|
||||
|
||||
export const useSendEmailCode = (): UseSendEmailCode => {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [error, setError] = useState<ApiError | null>(null);
|
||||
const [data, setData] = useState<APIResponse | null>(null);
|
||||
|
||||
const sendEmailCode = useCallback(
|
||||
(email: string, kind: OTPTypes): Promise<void> => {
|
||||
const payload: SendEmailCodeRequest = { email, kind };
|
||||
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
setData(null);
|
||||
|
||||
return apiClient()
|
||||
.post('v1/auth/send-email-code', payload)
|
||||
.then(res => {
|
||||
if (isApiResponse(res)) {
|
||||
setData(res);
|
||||
} else if (isApiError(res)) {
|
||||
setError(res);
|
||||
} else {
|
||||
setError({ message: 'Received unknown response structure.' });
|
||||
}
|
||||
})
|
||||
.catch(() => setError({ message: 'Network or unexpected error' }))
|
||||
.finally(() => setIsLoading(false));
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
return { isLoading, error, data, sendEmailCode };
|
||||
};
|
||||
Reference in New Issue
Block a user