()
| 29 | import UnitForm, { UnitFormRef } from "@/lib/components/unit/UnitForm"; |
| 30 | |
| 31 | export default function Add() { |
| 32 | const [isLoading, setLoading] = useState(false); |
| 33 | //TODO: integrate the UnitForm more efficiently by directly changing the form value from UnitForm |
| 34 | const form = useForm({ |
| 35 | initialValues: { |
| 36 | title: undefined, |
| 37 | quantity: 1, |
| 38 | productId: undefined, |
| 39 | productCode: undefined, |
| 40 | productModel: undefined, |
| 41 | productDescription: undefined, |
| 42 | parentCatalogName: undefined, |
| 43 | catalogName: undefined, |
| 44 | brandName: undefined, |
| 45 | encapStandard: undefined, |
| 46 | productImages: [], |
| 47 | pdfLink: undefined, |
| 48 | productLink: undefined, |
| 49 | tolerance: undefined, |
| 50 | voltage: undefined, |
| 51 | resistance: undefined, |
| 52 | power: undefined, |
| 53 | current: undefined, |
| 54 | frequency: undefined, |
| 55 | capacitance: undefined, |
| 56 | inductance: undefined, |
| 57 | prices: [] as { |
| 58 | ladder: string; |
| 59 | price: number; |
| 60 | }[], |
| 61 | }, |
| 62 | validate: { |
| 63 | productCode: (value) => |
| 64 | value && value.length > 0 ? null : "Product Code is required", |
| 65 | }, |
| 66 | }); |
| 67 | |
| 68 | const [scannerInput, setScannerInput] = useState(""); |
| 69 | const [productCode, setProductCode] = useState(""); |
| 70 | |
| 71 | //When the user presses the Autocomplete Button |
| 72 | async function handleAutocomplete() { |
| 73 | //Firstly checking of there is a valid input by the scanner form |
| 74 | const partInfoFromScanner = scannerInputToType( |
| 75 | JSON.parse(JSON.stringify(scannerInput)) ?? "" |
| 76 | ); |
| 77 | const validScannerInput = |
| 78 | partInfoFromScanner.pc != "" && partInfoFromScanner.pc; |
| 79 | //Checking of there is a valid input |
| 80 | if (productCode != "" || validScannerInput) { |
| 81 | //Using the scanner product code if both are given |
| 82 | let productCodeInternal = partInfoFromScanner.pc |
| 83 | ? partInfoFromScanner.pc |
| 84 | : productCode; |
| 85 | setLoading(true); |
| 86 | |
| 87 | const response = await fetch("/api/parts/autocomplete", { |
| 88 | method: "POST", |
nothing calls this directly
no test coverage detected