import AddIcon from "@mui/icons-material/Add"; import { Fab, ListItem, ListItemButton, Paper, Tooltip, Typography, } from "@mui/material"; import React from "react"; import { Movement, MovementApi } from "../api/MovementsApi"; import { UpdateMovementDialog } from "../dialogs/UpdateMovementDialog"; import { AsyncWidget } from "./AsyncWidget"; import { AccountInput } from "./forms/AccountInput"; import { AmountInput } from "./forms/AmountInput"; import { DateInput } from "./forms/DateInput"; import { TextInput } from "./forms/TextInput"; import { MovementWidget } from "./MovementWidget"; export function SelectMovementWidget(p: { value?: number; onChange: (movement: Movement) => void; initialValues?: { amount?: number; accountId?: number; time?: number; label?: string; }; }): React.ReactElement { const loadKey = React.useRef(1); const [amount, setAmount] = React.useState( p.initialValues?.amount ); const [accountId, setAccountId] = React.useState( p.initialValues?.accountId ); const [time, setTime] = React.useState( p.initialValues?.time ); const [label, setLabel] = React.useState( p.initialValues?.label ); const filters = { label: label, amount_min: amount ? amount - 0.5 : undefined, amount_max: amount ? amount + 0.5 : undefined, time_min: time ? time - 3600 * 24 : undefined, time_max: time ? time + 3600 * 24 : undefined, limit: 10, }; const [list, setList] = React.useState(); const load = async () => { if (accountId) setList(await MovementApi.GetAccountMovements(accountId, filters)); else setList(undefined); }; const [creating, setCreating] = React.useState(false); const handleStartCreateNewMovement = () => { setCreating(true); }; const handleCloseCreateNewMovement = () => { setCreating(false); }; const handleCreatedNewMovement = (m: Movement) => { setAmount(m.amount); setAccountId(m.account_id); setTime(m.time); setLabel(m.label); p.onChange(m); loadKey.current += 1; setCreating(false); }; return ( <>
{ if (list === undefined) return ( Select an account to begin research. ); if (list.length === 0) return ( No result. ); return ( <> {list.map((entry) => ( { p.onChange(entry); }} > ))} ); }} />
{creating && accountId && ( )} ); }