MCPcopy Create free account
hub / github.com/PartPilotLab/PartPilot / handleAutocomplete

Function handleAutocomplete

app/add/page.tsx:72–148  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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",
89 body: JSON.stringify({
90 productCode: productCodeInternal,
91 }),
92 }).then((response) =>
93 response
94 .json()
95 .then((data) => ({ status: response.status, body: data }))
96 );
97 if (response.body.status == 200) {
98 notifications.show({
99 title: "Autocomplete Successful",
100 message: `The product code ${productCodeInternal} was found.`,
101 });
102 console.log(response.body.body);
103 if (response.body.body) {
104 Object.keys(response.body.body).forEach((key) => {
105 //check if exists, otherwise dont create
106 if (form.values.hasOwnProperty(key)) {
107 if (key == "quantity") {
108 //If quantity is received from the scanner
109 if (partInfoFromScanner.qty) {
110 form.setFieldValue(key, partInfoFromScanner.qty);
111 }
112 //If I receive a quantity, but not from the scanner --> do nothing
113 } else if (refMapping.hasOwnProperty(key)) {
114 // If the key corresponds to a ref, update the value of the corresponding UnitForm
115 if (key == "capacitance") {
116 refMapping[key].current.setValue(
117 response.body.body[key],
118 "pF"
119 );
120 } else if (key == "inductance") {
121 refMapping[key].current.setValue(
122 response.body.body[key],
123 "mH"
124 );
125 } else {
126 refMapping[key].current.setValue(response.body.body[key]);
127 }
128 } else {
129 form.setFieldValue(key, response.body.body[key]);

Callers 1

AddFunction · 0.85

Calls 1

scannerInputToTypeFunction · 0.90

Tested by

no test coverage detected