import AddIcon from "@mui/icons-material/Add"; import ClearIcon from "@mui/icons-material/Clear"; import { IconButton, Tooltip, Typography } from "@mui/material"; import React, { useRef } from "react"; import { Account } from "../api/AccountApi"; import { UploadedFile } from "../api/FileApi"; import { InboxApi } from "../api/InboxApi"; import { MovementApi } from "../api/MovementsApi"; import { ServerApi } from "../api/ServerApi"; import { useAlert } from "../hooks/context_providers/AlertDialogProvider"; import { useSnackbar } from "../hooks/context_providers/SnackbarProvider"; import { time } from "../utils/DateUtils"; import { firstLetterUppercase } from "../utils/StringUtils"; import { AmountInput } from "./forms/AmountInput"; import { DateInput } from "./forms/DateInput"; import { TextInput } from "./forms/TextInput"; import { UploadFileButton } from "./forms/UploadFileButton"; import { UploadedFileWidget } from "./UploadedFileWidget"; export function NewMovementWidget( p: { onCreated: () => void; } & ({ isInbox: true } | { isInbox?: undefined; account: Account }) ): React.ReactElement { const snackbar = useSnackbar(); const alert = useAlert(); const dateInputRef = useRef(null); const [file, setFile] = React.useState(); const [movTime, setMovTime] = React.useState(time()); const [label, setLabel] = React.useState(""); const [amount, setAmount] = React.useState(0); const entity = p.isInbox ? "inbox entry" : "movement"; const submit = async (e: React.SyntheticEvent) => { e.preventDefault(); if (!p.isInbox && (label?.length ?? 0) === 0) { alert(`Please specify ${entity} label!`); return; } if (p.isInbox && !file) { alert(`Please specify ${entity} file!`); return; } if (!movTime) { alert(`Please specify ${entity} date!`); return; } if (!amount && !p.isInbox) { alert(`Please specify ${entity} amount!`); return; } try { if (!p.isInbox) { await MovementApi.Create({ account_id: p.account.id, checked: false, amount: amount!, label: label!, time: movTime, }); } else { await InboxApi.Create({ file_id: file!.id, amount: amount, label: label, time: movTime, }); } snackbar(`The ${entity} was successfully created!`); p.onCreated(); setFile(undefined); setLabel(""); setAmount(0); // Give back focus to date input dateInputRef.current?.querySelector("input")?.focus(); } catch (e) { console.error(`Failed to create ${entity}!`, e); alert(`Failed to create ${entity}! ${e}`); } }; return (
{/* Label */} {/* Add label only when creating movement */} {!p.isInbox ? ( New {entity} ) : ( <> )} {/* File input */} {/* Add file only when creating inbox entries */} {p.isInbox ? ( file ? ( <> { setFile(undefined); }} > ) : ( ) ) : ( <> )}   {/* Date input */}   {/* Label input */}   {/* Amount input */} {/* Submit button */} ); }