import Styles from "@/constants/Styles" import { apiCheckPhoneLogin, apiSendOtp } from "@/lib/api" import AsyncStorage from "@react-native-async-storage/async-storage" import { useState } from "react" import { Image, SafeAreaView, View } from "react-native" import Toast from "react-native-toast-message" import { ButtonForm } from "../buttonForm" import { InputForm } from "../inputForm" import ModalLoading from "../modalLoading" import Text from "../Text" import ToastCustom from "../toastCustom" type Props = { onValidate: ({ phone, otp }: { phone: string, otp: number }) => void } export default function ViewLogin({ onValidate }: Props) { const [loadingLogin, setLoadingLogin] = useState(false) const [disableLogin, setDisableLogin] = useState(true) const [phone, setPhone] = useState('') const handleCheckPhone = async () => { try { setLoadingLogin(true) const response = await apiCheckPhoneLogin({ phone: `62${phone}` }); if (response.success) { const otp = Math.floor(1000 + Math.random() * 9000) const responseOtp = await apiSendOtp({ phone: `62${phone}`, otp }) if (responseOtp == 200) { await AsyncStorage.setItem('user', response.id); return onValidate({ phone: `62${phone}`, otp }) } } return Toast.show({ type: 'small', text1: response.message, position: 'top' }) } catch (error) { return Toast.show({ type: 'small', text1: 'Terjadi kesalahan', position: 'top' }) } finally { setLoadingLogin(false) } }; return ( PERBEKEL DARMASABA { val == "" ? setDisableLogin(true) : setDisableLogin(false) setPhone(val) }} type="numeric" placeholder="XXX-XXX-XXXX" round itemLeft={+62} info="Kami akan mengirim kode verifikasi melalui WhatsApp, guna mengonfirmasikan nomor Anda." /> { handleCheckPhone() }} disabled={disableLogin} /> { loadingLogin && } ) }