import { Button, Dialog, DialogActions, DialogContent, DialogTitle, } from "@mui/material"; import React from "react"; import { Movement, MovementApi, MovementUpdate } from "../api/MovementsApi"; import { ServerApi } from "../api/ServerApi"; import { useAccounts } from "../hooks/AccountsListProvider"; import { useAlert } from "../hooks/context_providers/AlertDialogProvider"; import { useLoadingMessage } from "../hooks/context_providers/LoadingMessageProvider"; import { useSnackbar } from "../hooks/context_providers/SnackbarProvider"; import { AccountInput } from "../widgets/forms/AccountInput"; import { AmountInput } from "../widgets/forms/AmountInput"; import { DateInput } from "../widgets/forms/DateInput"; import { TextInput } from "../widgets/forms/TextInput"; export function UpdateMovementDialog(p: { open: boolean; updateId?: number; initial: MovementUpdate; onClose: () => void; onFinished: (m: Movement) => void; }): React.ReactElement { const alert = useAlert(); const snackbar = useSnackbar(); const loadingMessage = useLoadingMessage(); const accounts = useAccounts(); const [movement, setMovement] = React.useState(p.initial); const style: React.CSSProperties = { width: "100%", marginBottom: "15px" }; const canSubmit = movement.amount !== 0 && movement.account_id && movement.time !== 0 && movement.label.length > 0; const handleSubmit = async () => { try { loadingMessage.show( `${p.updateId ? "Updating" : "Creating"} the movement...` ); let res: Movement; if (p.updateId) { res = await MovementApi.Update({ ...movement, id: p.updateId }); } else { res = await MovementApi.Create(movement); } accounts.reloadBalances(); snackbar("The movement was successfully saved!"); p.onFinished(res); } catch (e) { console.error("Failed to save movement!", e); alert(`Failed to ${p.updateId ? "update" : "create"} movement! ${e}`); } finally { loadingMessage.hide(); } }; return ( {p.updateId ? `Update a movement` : `Create a movement`} {/* Account */} setMovement((m) => { return { ...m, account_id: a ?? 0, }; }) } /> {/* Date input */} setMovement((m) => { return { ...m, time: t ?? 0, }; }) } /> {/* Label input */} setMovement((m) => { return { ...m, label: l ?? "", }; }) } size={ServerApi.Config.constraints.movement_label} /> {/* Amount input */} setMovement((m) => { return { ...m, amount: l, }; }) } /> ); }