/* eslint-disable react-hooks/exhaustive-deps */ import { BaseBox, BoxButtonOnFooter, ButtonCustom, Divider, Grid, StackCustom, TextCustom, TextInputCustom, ViewWrapper, } from "@/components"; import { LOCAL_STORAGE_KEY } from "@/constants/local-storage-key"; import { apiInvestmentGetOne } from "@/service/api-client/api-investment"; import { formatCurrencyDisplay } from "@/utils/formatCurrencyDisplay"; import AsyncStorage from "@react-native-async-storage/async-storage"; import { router, useFocusEffect, useLocalSearchParams } from "expo-router"; import { useCallback, useState } from "react"; export default function InvestmentInvest() { const { id } = useLocalSearchParams(); // console.log("[ID]", id); const [data, setData] = useState(null); const [jumlah, setJumlah] = useState(0); const [total, setTotal] = useState(0); const [sisaLembar, setSisaLembar] = useState(0); const [isLoading, setIsLoading] = useState(false); useFocusEffect( useCallback(() => { onLoadData(); }, [id]) ); const onLoadData = async () => { try { const response = await apiInvestmentGetOne({ id: id as string, }); setData(response.data); setSisaLembar(response.data?.sisaLembar); } catch (error) { console.log("[ERROR]", error); } }; const handleTextChange = (text: string) => { // Izinkan input kosong → anggap sebagai 0 (atau abaikan, tergantung UX) if (text === "") { setJumlah(0); setTotal(0); return; } // Regex: hanya digit (angka bulat positif) const integerRegex = /^\d+$/; if (integerRegex.test(text)) { const numValue = Number(text); // Karena regex sudah pastikan hanya angka, isNaN biasanya false // Tapi tetap aman untuk cek if (!isNaN(numValue)) { setJumlah(numValue); setTotal(numValue * Number(data?.hargaLembar)); console.log("[VALUE]", numValue); } } // Jika input tidak valid (misal: "12a", "12."), abaikan → state tidak berubah }; const buttonSubmit = () => { return ( <> sisaLembar} onPress={async () => { try { setIsLoading(true); await AsyncStorage.setItem( LOCAL_STORAGE_KEY.transactionInvestment, JSON.stringify({ jumlah, total }) ); router.push(`/investment/${id}/select-bank`); } catch (error) { console.log("[ERROR]", error); } finally { setIsLoading(false); } }} > Beli ); }; return ( <> Sisa Lembar Saham {data && formatCurrencyDisplay(data?.sisaLembar) || "-"} Harga Per Lembar {data && formatCurrencyDisplay(data?.hargaLembar) || "-"} Jumlah Pembelian Minimum 10 lembar { handleTextChange(value); }} /> Total Harga Rp. {formatCurrencyDisplay(total)} ); }