Added ability to select and create multiple label settings
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
import { Form, Switch } from "antd";
|
||||
import { Button, Flex, Form, Input, Modal, Popconfirm, Select, Space, Switch } from "antd";
|
||||
import { IFilament } from "../../pages/filaments/model";
|
||||
import { ISpool } from "../../pages/spools/model";
|
||||
import QRCodePrintingDialog from "./qrCodePrintingDialog";
|
||||
import { useSavedState } from "../../utils/saveload";
|
||||
import { useTranslate } from "@refinedev/core";
|
||||
import { useGetPrintSettings, useSetPrintSettings } from "./printing";
|
||||
import { QRCodePrintSettings, SpoolQRCodePrintSettings, useGetPrintSettings, useSetPrintSettings } from "./printing";
|
||||
import { useState } from "react";
|
||||
import { DeleteOutlined, EditOutlined, PlusOutlined } from "@ant-design/icons";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import _ from "lodash";
|
||||
|
||||
interface SpoolQRCodePrintingDialog {
|
||||
visible: boolean;
|
||||
@@ -14,16 +18,105 @@ interface SpoolQRCodePrintingDialog {
|
||||
|
||||
const SpoolQRCodePrintingDialog: React.FC<SpoolQRCodePrintingDialog> = ({ visible, items, onCancel }) => {
|
||||
const t = useTranslate();
|
||||
const printSettings = useGetPrintSettings()[0];
|
||||
|
||||
// Selected setting state
|
||||
const [selectedSetting, setSelectedSetting] = useState<string | undefined>();
|
||||
|
||||
const allPrintSettings = useGetPrintSettings();
|
||||
const setPrintSettings = useSetPrintSettings();
|
||||
|
||||
const showVendor = printSettings?.showVendor;
|
||||
const showLotNr = printSettings?.showLotNr;
|
||||
const showSpoolWeight = printSettings?.showSpoolWeight;
|
||||
const showTemperatures = printSettings?.showTemperatures;
|
||||
const showSpoolComment = printSettings?.showSpoolComment;
|
||||
const showFilamentComment = printSettings?.showFilamentComment;
|
||||
const showVendorComment = printSettings?.showVendorComment;
|
||||
// Functions to update settings
|
||||
const addNewPrintSettings = () => {
|
||||
if (!allPrintSettings) return;
|
||||
const newId = uuidv4();
|
||||
const newSetting = {
|
||||
labelSettings: {
|
||||
printSettings: {
|
||||
id: newId,
|
||||
name: t("printing.generic.newSetting"),
|
||||
},
|
||||
},
|
||||
};
|
||||
setPrintSettings([...allPrintSettings, newSetting]);
|
||||
setSelectedSetting(newId);
|
||||
return newSetting;
|
||||
};
|
||||
const updateCurrentPrintSettings = (newSettings: SpoolQRCodePrintSettings) => {
|
||||
if (!allPrintSettings) return;
|
||||
setPrintSettings(
|
||||
allPrintSettings.map((settings) =>
|
||||
settings.labelSettings.printSettings.id === newSettings.labelSettings.printSettings.id ? newSettings : settings
|
||||
)
|
||||
);
|
||||
};
|
||||
const deleteCurrentPrintSettings = () => {
|
||||
if (!allPrintSettings) return;
|
||||
setPrintSettings(
|
||||
allPrintSettings.filter((qSetting) => qSetting.labelSettings.printSettings.id !== selectedSetting)
|
||||
);
|
||||
setSelectedSetting(undefined);
|
||||
};
|
||||
|
||||
// Initialize settings
|
||||
let selectedPrintSetting: SpoolQRCodePrintSettings;
|
||||
if (allPrintSettings === undefined) {
|
||||
// DB not loaded yet, use a temporary one
|
||||
selectedPrintSetting = {
|
||||
labelSettings: {
|
||||
printSettings: {
|
||||
id: "TEMP",
|
||||
name: t("printing.generic.newSetting"),
|
||||
},
|
||||
},
|
||||
};
|
||||
} else {
|
||||
// DB is loaded, find the selected setting
|
||||
if (allPrintSettings.length === 0) {
|
||||
// DB loaded, but no settings found, add a new one and select it
|
||||
const newSetting = addNewPrintSettings();
|
||||
if (!newSetting) {
|
||||
console.error("Error adding new setting, this should never happen");
|
||||
return;
|
||||
}
|
||||
|
||||
// Mutate the allPrintSettings list so that the rest of the UI will work fine
|
||||
allPrintSettings.push(newSetting);
|
||||
selectedPrintSetting = newSetting;
|
||||
} else {
|
||||
// DB loaded and at least 1 setting exists
|
||||
if (!selectedSetting) {
|
||||
// No setting has been selected, select the first one
|
||||
selectedPrintSetting = allPrintSettings[0];
|
||||
setSelectedSetting(allPrintSettings[0].labelSettings.printSettings.id);
|
||||
} else {
|
||||
// A setting has been selected, find it
|
||||
const foundSetting = allPrintSettings.find(
|
||||
(settings) => settings.labelSettings.printSettings.id === selectedSetting
|
||||
);
|
||||
if (foundSetting) {
|
||||
selectedPrintSetting = foundSetting;
|
||||
} else {
|
||||
// Selected setting not found, select a temp one
|
||||
selectedPrintSetting = {
|
||||
labelSettings: {
|
||||
printSettings: {
|
||||
id: "TEMP",
|
||||
name: t("printing.generic.newSetting"),
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const showVendor = selectedPrintSetting?.showVendor;
|
||||
const showLotNr = selectedPrintSetting?.showLotNr;
|
||||
const showSpoolWeight = selectedPrintSetting?.showSpoolWeight;
|
||||
const showTemperatures = selectedPrintSetting?.showTemperatures;
|
||||
const showSpoolComment = selectedPrintSetting?.showSpoolComment;
|
||||
const showFilamentComment = selectedPrintSetting?.showFilamentComment;
|
||||
const showVendorComment = selectedPrintSetting?.showVendorComment;
|
||||
|
||||
const formatFilament = (filament: IFilament) => {
|
||||
let vendorPrefix = "";
|
||||
@@ -41,11 +134,63 @@ const SpoolQRCodePrintingDialog: React.FC<SpoolQRCodePrintingDialog> = ({ visibl
|
||||
<QRCodePrintingDialog
|
||||
visible={visible}
|
||||
onCancel={onCancel}
|
||||
printSettings={printSettings.labelSettings}
|
||||
printSettings={selectedPrintSetting.labelSettings}
|
||||
setPrintSettings={(newSettings) => {
|
||||
printSettings.labelSettings = newSettings;
|
||||
setPrintSettings([printSettings]);
|
||||
selectedPrintSetting.labelSettings = newSettings;
|
||||
updateCurrentPrintSettings(selectedPrintSetting);
|
||||
}}
|
||||
extraSettingsStart={
|
||||
<>
|
||||
<Form.Item label={t("printing.generic.settings")}>
|
||||
<Flex gap={8}>
|
||||
<Select
|
||||
value={selectedSetting}
|
||||
onChange={(value) => {
|
||||
setSelectedSetting(value);
|
||||
}}
|
||||
options={
|
||||
allPrintSettings &&
|
||||
allPrintSettings.map((settings) => ({
|
||||
label: settings.labelSettings.printSettings?.name || t("printing.generic.defaultSettings"),
|
||||
value: settings.labelSettings.printSettings.id,
|
||||
}))
|
||||
}
|
||||
></Select>
|
||||
<Button
|
||||
style={{ width: "3em" }}
|
||||
icon={<PlusOutlined />}
|
||||
title={t("printing.generic.addSettings")}
|
||||
onClick={addNewPrintSettings}
|
||||
/>
|
||||
{allPrintSettings && allPrintSettings.length > 1 && (
|
||||
<Popconfirm
|
||||
title={t("printing.generic.deleteSettings")}
|
||||
description={t("printing.generic.deleteSettingsConfirm")}
|
||||
onConfirm={deleteCurrentPrintSettings}
|
||||
okText={t("buttons.delete")}
|
||||
cancelText={t("buttons.cancel")}
|
||||
>
|
||||
<Button
|
||||
style={{ width: "3em" }}
|
||||
danger
|
||||
icon={<DeleteOutlined />}
|
||||
title={t("printing.generic.deleteSettings")}
|
||||
/>
|
||||
</Popconfirm>
|
||||
)}
|
||||
</Flex>
|
||||
</Form.Item>
|
||||
<Form.Item label={t("printing.generic.settingsName")}>
|
||||
<Input
|
||||
value={selectedPrintSetting.labelSettings.printSettings?.name}
|
||||
onChange={(e) => {
|
||||
selectedPrintSetting.labelSettings.printSettings.name = e.target.value;
|
||||
updateCurrentPrintSettings(selectedPrintSetting);
|
||||
}}
|
||||
/>
|
||||
</Form.Item>
|
||||
</>
|
||||
}
|
||||
items={items.map(function (spool) {
|
||||
const temps = [];
|
||||
if (spool.filament.settings_extruder_temp) {
|
||||
@@ -117,8 +262,8 @@ const SpoolQRCodePrintingDialog: React.FC<SpoolQRCodePrintingDialog> = ({ visibl
|
||||
<Switch
|
||||
checked={showVendor}
|
||||
onChange={(checked) => {
|
||||
printSettings.showVendor = checked;
|
||||
setPrintSettings([printSettings]);
|
||||
selectedPrintSetting.showVendor = checked;
|
||||
updateCurrentPrintSettings(selectedPrintSetting);
|
||||
}}
|
||||
/>
|
||||
</Form.Item>
|
||||
@@ -126,8 +271,8 @@ const SpoolQRCodePrintingDialog: React.FC<SpoolQRCodePrintingDialog> = ({ visibl
|
||||
<Switch
|
||||
checked={showSpoolWeight}
|
||||
onChange={(checked) => {
|
||||
printSettings.showSpoolWeight = checked;
|
||||
setPrintSettings([printSettings]);
|
||||
selectedPrintSetting.showSpoolWeight = checked;
|
||||
updateCurrentPrintSettings(selectedPrintSetting);
|
||||
}}
|
||||
/>
|
||||
</Form.Item>
|
||||
@@ -135,8 +280,8 @@ const SpoolQRCodePrintingDialog: React.FC<SpoolQRCodePrintingDialog> = ({ visibl
|
||||
<Switch
|
||||
checked={showTemperatures}
|
||||
onChange={(checked) => {
|
||||
printSettings.showTemperatures = checked;
|
||||
setPrintSettings([printSettings]);
|
||||
selectedPrintSetting.showTemperatures = checked;
|
||||
updateCurrentPrintSettings(selectedPrintSetting);
|
||||
}}
|
||||
/>
|
||||
</Form.Item>
|
||||
@@ -144,8 +289,8 @@ const SpoolQRCodePrintingDialog: React.FC<SpoolQRCodePrintingDialog> = ({ visibl
|
||||
<Switch
|
||||
checked={showLotNr}
|
||||
onChange={(checked) => {
|
||||
printSettings.showLotNr = checked;
|
||||
setPrintSettings([printSettings]);
|
||||
selectedPrintSetting.showLotNr = checked;
|
||||
updateCurrentPrintSettings(selectedPrintSetting);
|
||||
}}
|
||||
/>
|
||||
</Form.Item>
|
||||
@@ -153,8 +298,8 @@ const SpoolQRCodePrintingDialog: React.FC<SpoolQRCodePrintingDialog> = ({ visibl
|
||||
<Switch
|
||||
checked={showSpoolComment}
|
||||
onChange={(checked) => {
|
||||
printSettings.showSpoolComment = checked;
|
||||
setPrintSettings([printSettings]);
|
||||
selectedPrintSetting.showSpoolComment = checked;
|
||||
updateCurrentPrintSettings(selectedPrintSetting);
|
||||
}}
|
||||
/>
|
||||
</Form.Item>
|
||||
@@ -162,8 +307,8 @@ const SpoolQRCodePrintingDialog: React.FC<SpoolQRCodePrintingDialog> = ({ visibl
|
||||
<Switch
|
||||
checked={showFilamentComment}
|
||||
onChange={(checked) => {
|
||||
printSettings.showFilamentComment = checked;
|
||||
setPrintSettings([printSettings]);
|
||||
selectedPrintSetting.showFilamentComment = checked;
|
||||
updateCurrentPrintSettings(selectedPrintSetting);
|
||||
}}
|
||||
/>
|
||||
</Form.Item>
|
||||
@@ -171,8 +316,8 @@ const SpoolQRCodePrintingDialog: React.FC<SpoolQRCodePrintingDialog> = ({ visibl
|
||||
<Switch
|
||||
checked={showVendorComment}
|
||||
onChange={(checked) => {
|
||||
printSettings.showVendorComment = checked;
|
||||
setPrintSettings([printSettings]);
|
||||
selectedPrintSetting.showVendorComment = checked;
|
||||
updateCurrentPrintSettings(selectedPrintSetting);
|
||||
}}
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
Reference in New Issue
Block a user