From 5e7c155f6e6be996a4a4c9aba99f18500032726a Mon Sep 17 00:00:00 2001 From: megumin Date: Tue, 25 Oct 2022 18:49:50 +0100 Subject: feat(settings): add beforeSave check (#161) --- src/components/PluginSettings/PluginModal.tsx | 59 ++++++++++++++++----------- 1 file changed, 36 insertions(+), 23 deletions(-) (limited to 'src/components') diff --git a/src/components/PluginSettings/PluginModal.tsx b/src/components/PluginSettings/PluginModal.tsx index 908876f..970eb52 100644 --- a/src/components/PluginSettings/PluginModal.tsx +++ b/src/components/PluginSettings/PluginModal.tsx @@ -67,6 +67,7 @@ export default function PluginModal({ plugin, onRestartNeeded, onClose, transiti const [tempSettings, setTempSettings] = React.useState>({}); const [errors, setErrors] = React.useState>({}); + const [saveError, setSaveError] = React.useState(null); const canSubmit = () => Object.values(errors).every(e => !e); @@ -79,11 +80,20 @@ export default function PluginModal({ plugin, onRestartNeeded, onClose, transiti })(); }, []); - function saveAndClose() { + async function saveAndClose() { if (!plugin.options) { onClose(); return; } + + if (plugin.beforeSave) { + const result = await Promise.resolve(plugin.beforeSave(tempSettings)); + if (result !== true) { + setSaveError(result); + return; + } + } + let restartNeeded = false; for (const [key, value] of Object.entries(tempSettings)) { const option = plugin.options[key]; @@ -195,28 +205,31 @@ export default function PluginModal({ plugin, onRestartNeeded, onClose, transiti - - - - {({ onMouseEnter, onMouseLeave }) => ( - - )} - + + + + + {({ onMouseEnter, onMouseLeave }) => ( + + )} + + + {saveError && Error while saving: {saveError}} -- cgit