Invesment

Fix: Integrasi API ke UI
- investment/[id]/(transaction-flow)/index.tsx
- investment/[id]/index.tsx

### Issue: input data untuk total lembar dan sisa lembar berisi . dianatra angkanya
This commit is contained in:
2025-10-01 17:29:59 +08:00
parent c2acb97a37
commit aa85e05f79
4 changed files with 69 additions and 8 deletions

View File

@@ -1,3 +1,4 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { import {
BaseBox, BaseBox,
BoxButtonOnFooter, BoxButtonOnFooter,
@@ -9,16 +10,72 @@ import {
TextInputCustom, TextInputCustom,
ViewWrapper, ViewWrapper,
} from "@/components"; } from "@/components";
import { router, useLocalSearchParams } from "expo-router"; 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() { export default function InvestmentInvest() {
const { id } = useLocalSearchParams(); const { id } = useLocalSearchParams();
// console.log("[ID]", id);
const [data, setData] = useState<any>(null);
const [value, setValue] = useState<number>(0);
const [total, setTotal] = useState<number>(0);
const [sisaLembar, setSisaLembar] = useState<number>(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 = () => { const buttonSubmit = () => {
return ( return (
<> <>
<BoxButtonOnFooter> <BoxButtonOnFooter>
<ButtonCustom onPress={() => router.push(`/investment/${id}/select-bank`)}>Beli</ButtonCustom> <ButtonCustom
disabled={value < 10 || value >= sisaLembar}
onPress={() => router.push(`/investment/${id}/select-bank`)}
>
Beli {value}, {sisaLembar}
</ButtonCustom>
</BoxButtonOnFooter> </BoxButtonOnFooter>
</> </>
); );
@@ -34,7 +91,7 @@ export default function InvestmentInvest() {
<TextCustom>Sisa Lembar Saham</TextCustom> <TextCustom>Sisa Lembar Saham</TextCustom>
</Grid.Col> </Grid.Col>
<Grid.Col span={6} style={{ alignItems: "flex-end" }}> <Grid.Col span={6} style={{ alignItems: "flex-end" }}>
<TextCustom>3.000</TextCustom> <TextCustom>{data?.sisaLembar || "-"}</TextCustom>
</Grid.Col> </Grid.Col>
</Grid> </Grid>
<Grid> <Grid>
@@ -42,7 +99,7 @@ export default function InvestmentInvest() {
<TextCustom>Harga Per Lembar</TextCustom> <TextCustom>Harga Per Lembar</TextCustom>
</Grid.Col> </Grid.Col>
<Grid.Col span={6} style={{ alignItems: "flex-end" }}> <Grid.Col span={6} style={{ alignItems: "flex-end" }}>
<TextCustom>Rp. 1.000</TextCustom> <TextCustom>{data?.hargaLembar || "-"}</TextCustom>
</Grid.Col> </Grid.Col>
</Grid> </Grid>
<Grid> <Grid>
@@ -69,6 +126,10 @@ export default function InvestmentInvest() {
}} }}
placeholder="0" placeholder="0"
keyboardType="numeric" keyboardType="numeric"
value={value.toString()}
onChangeText={(value) => {
handleTextChange(value);
}}
/> />
</Grid.Col> </Grid.Col>
</Grid> </Grid>
@@ -78,7 +139,7 @@ export default function InvestmentInvest() {
<TextCustom>Total Harga</TextCustom> <TextCustom>Total Harga</TextCustom>
</Grid.Col> </Grid.Col>
<Grid.Col span={6} style={{ alignItems: "flex-end" }}> <Grid.Col span={6} style={{ alignItems: "flex-end" }}>
<TextCustom>Rp. 1.000</TextCustom> <TextCustom> Rp. {formatCurrencyDisplay(total)}</TextCustom>
</Grid.Col> </Grid.Col>
</Grid> </Grid>
</StackCustom> </StackCustom>

View File

@@ -1,3 +1,4 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { import {
BackButton, BackButton,
DotButton, DotButton,

View File

@@ -40,7 +40,7 @@ const listDataPublishInvesment = ({ data }: { data: any }) => [
}, },
{ {
label: "Target Dana", label: "Target Dana",
value: data?.targetDana, value: `Rp. ${formatCurrencyDisplay(data?.targetDana) || "-"}`,
}, },
{ {
label: "Harga Per Lembar", label: "Harga Per Lembar",

View File

@@ -1,12 +1,11 @@
import { import {
AvatarUsernameAndOtherComponent, AvatarUsernameAndOtherComponent,
BaseBox,
BoxWithHeaderSection, BoxWithHeaderSection,
DummyLandscapeImage, DummyLandscapeImage,
Grid, Grid,
Spacing, Spacing,
StackCustom, StackCustom,
TextCustom, TextCustom
} from "@/components"; } from "@/components";
import { View } from "react-native"; import { View } from "react-native";