From 5a8d71fe70957a667fca661c51d92258808e8ab1 Mon Sep 17 00:00:00 2001 From: Donkie Date: Mon, 13 May 2024 21:55:18 +0200 Subject: [PATCH] Added import external filament in filament create form --- client/public/locales/en/common.json | 4 +- client/src/components/filamentImportModal.tsx | 66 +++++++++++++++++++ client/src/pages/filaments/create.tsx | 51 ++++++++++++-- 3 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 client/src/components/filamentImportModal.tsx diff --git a/client/public/locales/en/common.json b/client/public/locales/en/common.json index e30c352..d0ff6d4 100644 --- a/client/public/locales/en/common.json +++ b/client/public/locales/en/common.json @@ -224,7 +224,9 @@ "show_title": "[Filament #{{id}}] {{name}}" }, "form": { - "filament_updated": "This filament has been updated by someone/something else since you opened this page. Saving will overwrite those changes!" + "filament_updated": "This filament has been updated by someone/something else since you opened this page. Saving will overwrite those changes!", + "import_external": "Import from External", + "import_external_description": "Select a filament in the list to automatically populate its details in the filament creation form. This will overwrite any data you have entered in the form.

A Manufacturer object will be created automatically when you click Ok, if necessary." }, "buttons": { "add_spool": "Add Spool" diff --git a/client/src/components/filamentImportModal.tsx b/client/src/components/filamentImportModal.tsx new file mode 100644 index 0000000..97ac725 --- /dev/null +++ b/client/src/components/filamentImportModal.tsx @@ -0,0 +1,66 @@ +import { Form, InputNumber, Modal, Select } from "antd"; +import { useState } from "react"; +import { ExternalFilament, useGetExternalDBFilaments } from "../utils/queryExternalDB"; +import { formatFilamentLabel } from "../pages/spools/functions"; +import { searchMatches } from "../utils/filtering"; +import { useTranslate } from "@refinedev/core"; +import { Trans } from "react-i18next"; + +export function FilamentImportModal(props: { + isOpen: boolean; + onImport: (filament: ExternalFilament) => void; + onClose: () => void; +}) { + const [form] = Form.useForm(); + const t = useTranslate(); + + const externalFilaments = useGetExternalDBFilaments(); + const filamentOptions = + externalFilaments.data?.map((item) => { + return { + label: formatFilamentLabel(item.name, item.diameter, item.manufacturer, item.material, item.weight), + value: item.id, + item: item, + }; + }) ?? []; + filamentOptions.sort((a, b) => a.label.localeCompare(b.label, undefined, { sensitivity: "base" })); + + return ( + form.submit()} + onCancel={() => props.onClose()} + > +
{ + const filament = filamentOptions.find((item) => item.value === values.filament)?.item; + if (!filament) { + throw new Error("Filament not found"); + } + props.onImport(filament); + props.onClose(); + form.resetFields(); + }} + > +

+ , + }} + /> +

+ + { return a?.label && b?.label