From 4100813f22206c250a99b5d407fb3051ec9f32e2 Mon Sep 17 00:00:00 2001 From: Donkie Date: Tue, 18 Jun 2024 19:37:17 +0200 Subject: [PATCH] Stop settings spam in label settings --- client/package-lock.json | 7 +++++++ client/package.json | 2 ++ .../printing/spoolQrCodePrintingDialog.tsx | 16 +++++++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/client/package-lock.json b/client/package-lock.json index 51b1ed3..755820b 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -18,6 +18,7 @@ "@tanstack/react-query": "^4.36.1", "@tanstack/react-query-devtools": "^4.36.1", "@types/loadable__component": "^5.13.9", + "@types/lodash": "^4.17.5", "@types/uuid": "^9.0.8", "@yudiel/react-qr-scanner": "^1.2.10", "antd": "^5.12.2", @@ -26,6 +27,7 @@ "i18next": "^23.11.4", "i18next-browser-languagedetector": "^7.2.1", "i18next-http-backend": "^2.5.1", + "lodash": "^4.17.21", "react": "^18.3.1", "react-dom": "^18.3.1", "react-i18next": "^14.1.1", @@ -2883,6 +2885,11 @@ "@types/react": "*" } }, + "node_modules/@types/lodash": { + "version": "4.17.5", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.5.tgz", + "integrity": "sha512-MBIOHVZqVqgfro1euRDWX7OO0fBVUUMrN6Pwm8LQsz8cWhEpihlvR70ENj3f40j58TNxZaWv2ndSkInykNBBJw==" + }, "node_modules/@types/mdast": { "version": "3.0.15", "resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-3.0.15.tgz", diff --git a/client/package.json b/client/package.json index cc4c73e..5792f0a 100644 --- a/client/package.json +++ b/client/package.json @@ -17,6 +17,7 @@ "@tanstack/react-query": "^4.36.1", "@tanstack/react-query-devtools": "^4.36.1", "@types/loadable__component": "^5.13.9", + "@types/lodash": "^4.17.5", "@types/uuid": "^9.0.8", "@yudiel/react-qr-scanner": "^1.2.10", "antd": "^5.12.2", @@ -25,6 +26,7 @@ "i18next": "^23.11.4", "i18next-browser-languagedetector": "^7.2.1", "i18next-http-backend": "^2.5.1", + "lodash": "^4.17.21", "react": "^18.3.1", "react-dom": "^18.3.1", "react-i18next": "^14.1.1", diff --git a/client/src/components/printing/spoolQrCodePrintingDialog.tsx b/client/src/components/printing/spoolQrCodePrintingDialog.tsx index 20cda7c..5b4af15 100644 --- a/client/src/components/printing/spoolQrCodePrintingDialog.tsx +++ b/client/src/components/printing/spoolQrCodePrintingDialog.tsx @@ -5,7 +5,7 @@ import QRCodePrintingDialog from "./qrCodePrintingDialog"; import { useSavedState } from "../../utils/saveload"; import { useTranslate } from "@refinedev/core"; import { QRCodePrintSettings, SpoolQRCodePrintSettings, useGetPrintSettings, useSetPrintSettings } from "./printing"; -import { useState } from "react"; +import { useMemo, useState } from "react"; import { DeleteOutlined, EditOutlined, PlusOutlined } from "@ant-design/icons"; import { v4 as uuidv4 } from "uuid"; import _ from "lodash"; @@ -22,8 +22,18 @@ const SpoolQRCodePrintingDialog: React.FC = ({ visibl // Selected setting state const [selectedSetting, setSelectedSetting] = useState(); - const allPrintSettings = useGetPrintSettings(); - const setPrintSettings = useSetPrintSettings(); + // Keep a local copy of the settings which is what's actually displayed. Use the remote state only for saving. + // This decouples the debounce stuff from the UI + const [localSettings, setLocalSettings] = useState(); + const remoteSettings = useGetPrintSettings(); + const setRemoteSettings = useSetPrintSettings(); + const debouncedSetRemoteSettings = useMemo(() => _.debounce(setRemoteSettings, 500), []); + + const allPrintSettings = localSettings ?? remoteSettings; + const setPrintSettings = (newSettings: SpoolQRCodePrintSettings[]) => { + setLocalSettings(newSettings); + debouncedSetRemoteSettings(newSettings); + }; // Functions to update settings const addNewPrintSettings = () => {