/* eslint-disable react-hooks/exhaustive-deps */ import { BaseBox, BoxButtonOnFooter, ButtonCustom, Divider, Grid, StackCustom, TextCustom, TextInputCustom, ViewWrapper, } from "@/components"; import { apiInvestmentGetOne } from "@/service/api-client/api-investment"; import { formatCurrencyDisplay } from "@/utils/formatCurrencyDisplay"; 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 [value, setValue] = useState(0); const [total, setTotal] = useState(0); const [sisaLembar, setSisaLembar] = useState(0); 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 === "") { setValue(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)) { setValue(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={() => router.push(`/investment/${id}/select-bank`)} > Beli {value}, {sisaLembar} ); }; return ( <> Sisa Lembar Saham {data?.sisaLembar || "-"} Harga Per Lembar {data?.hargaLembar || "-"} Jumlah Pembelian Minimum 10 lembar { handleTextChange(value); }} /> Total Harga Rp. {formatCurrencyDisplay(total)} ); }