| 148 | } |
| 149 | |
| 150 | async function addPart() { |
| 151 | //If the user presses on add part: check if from is valid |
| 152 | form.validate(); |
| 153 | setLoading(true); |
| 154 | if (form.isValid()) { |
| 155 | //Making an object ob the form (to then update it later) |
| 156 | let currentPartInfo = form.values || ({} as any); |
| 157 | if (currentPartInfo) { |
| 158 | currentPartInfo.voltage = |
| 159 | voltageFormRef.current?.getSearchParameters().value; |
| 160 | currentPartInfo.resistance = |
| 161 | resistanceFormRef.current?.getSearchParameters().value; |
| 162 | currentPartInfo.power = |
| 163 | powerFormRef.current?.getSearchParameters().value; |
| 164 | currentPartInfo.current = |
| 165 | currentFormRef.current?.getSearchParameters().value; |
| 166 | currentPartInfo.frequency = |
| 167 | frequencyFormRef.current?.getSearchParameters().value; |
| 168 | currentPartInfo.capacitance = |
| 169 | capacitanceFormRef.current?.getSearchParameters().value; |
| 170 | currentPartInfo.inductance = |
| 171 | inductanceFormRef.current?.getSearchParameters().value; |
| 172 | } |
| 173 | const response = await fetch("/api/parts/create", { |
| 174 | method: "POST", |
| 175 | body: JSON.stringify(form.values), |
| 176 | }).then((response) => |
| 177 | response |
| 178 | .json() |
| 179 | .then((data) => ({ status: response.status, body: data })) |
| 180 | ); |
| 181 | console.log(response); |
| 182 | if (response.status == 200) { |
| 183 | notifications.show({ |
| 184 | title: "Part Add Successful", |
| 185 | message: `The part ${form.values.productCode} was added.`, |
| 186 | color: "green", |
| 187 | }); |
| 188 | form.reset(); |
| 189 | } else { |
| 190 | if (response.status == 500) { |
| 191 | notifications.show({ |
| 192 | title: "Part Add Failed", |
| 193 | message: `The part could not be added. Please try again.`, |
| 194 | color: "red", |
| 195 | }); |
| 196 | } else if (response.status == 409) { |
| 197 | notifications.show({ |
| 198 | title: "Part Add Failed", |
| 199 | message: `The part ${form.values.productCode} already exists.`, |
| 200 | color: "red", |
| 201 | }); |
| 202 | } else { |
| 203 | notifications.show({ |
| 204 | title: "Part Add Failed", |
| 205 | message: `The part could not be added. Please try again.`, |
| 206 | color: "red", |
| 207 | }); |