diff --git a/src/Pages/BackupPage.js b/src/Pages/BackupPage.js index 02b0b6a..d833e57 100644 --- a/src/Pages/BackupPage.js +++ b/src/Pages/BackupPage.js @@ -56,12 +56,12 @@ export default function BackupPage() { let validated = {}; for (const [key, value] of Object.entries(data)) { - if (nextData[key] && typeof value == typeof nextData[key]) { - if (typeof nextData[key] == "object") { - validated[key] = validateValues(value, nextData[key]); + const nextDataValue = nextData[key]; + if ((nextDataValue != null || nextDataValue != undefined) && typeof value == typeof nextDataValue) { + if (typeof nextDataValue == "object") { + validated[key] = validateValues(value, nextDataValue); } else { - validated[key] = nextData[key]; - + validated[key] = nextDataValue; } } } @@ -88,7 +88,7 @@ export default function BackupPage() { const handleSave = async (values) => { let exportData = {}; for (const [key, value] of Object.entries(checkValues)) { - if (key.match("export_") && value) { + if (key.match("export_") && (value != null || value != undefined)) { let skey = key.slice(7, key.length); if (optionState[skey] != undefined || optionState[skey] != null) { exportData[skey] = optionState[skey]; @@ -158,7 +158,7 @@ export default function BackupPage() { // filter by known values let filteredData = {}; for (const [key, value] of Object.entries(checkValues)) { - if (key.match("import_") && value) { + if (key.match("import_") && (value != null || value != undefined)) { let skey = key.slice(7, key.length); if (newData[skey] != undefined || newData[skey] != null) { filteredData[skey] = newData[skey]; diff --git a/src/Services/WebApi.js b/src/Services/WebApi.js index 41ede45..10af362 100644 --- a/src/Services/WebApi.js +++ b/src/Services/WebApi.js @@ -44,7 +44,7 @@ async function getDisplayOptions() { } async function setDisplayOptions(options, isPreview) { - let newOptions = { ...options }; + let newOptions = sanitizeRequest(options); newOptions.i2cAddress = parseInt(options.i2cAddress); newOptions.buttonLayout = parseInt(options.buttonLayout); newOptions.buttonLayoutRight = parseInt(options.buttonLayoutRight); @@ -52,9 +52,13 @@ async function setDisplayOptions(options, isPreview) { newOptions.splashDuration = parseInt(options.splashDuration) * 1000; // seconds to milliseconds newOptions.displaySaverTimeout = parseInt(options.displaySaverTimeout) * 60000; // minutes to milliseconds newOptions.splashChoice = parseInt(options.splashChoice); - newOptions.buttonLayoutCustomOptions.params.layout = parseInt(options.buttonLayoutCustomOptions.params.layout); - newOptions.buttonLayoutCustomOptions.paramsRight.layout = parseInt(options.buttonLayoutCustomOptions.paramsRight.layout); - newOptions.splashImage = ''; + + if (newOptions.buttonLayoutCustomOptions) { + newOptions.buttonLayoutCustomOptions.params.layout = parseInt(options.buttonLayoutCustomOptions?.params?.layout); + newOptions.buttonLayoutCustomOptions.paramsRight.layout = parseInt(options.buttonLayoutCustomOptions?.paramsRight?.layout); + } + + delete newOptions.splashImage; const url = !isPreview ? `${baseUrl}/api/setDisplayOptions` : `${baseUrl}/api/setPreviewDisplayOptions`; return axios.post(url, newOptions) .then((response) => { @@ -89,7 +93,7 @@ async function getGamepadOptions() { } async function setGamepadOptions(options) { - return axios.post(`${baseUrl}/api/setGamepadOptions`, options) + return axios.post(`${baseUrl}/api/setGamepadOptions`, sanitizeRequest(options)) .then((response) => { console.log(response.data); return true; @@ -107,7 +111,7 @@ async function getLedOptions() { } async function setLedOptions(options) { - return axios.post(`${baseUrl}/api/setLedOptions`, options) + return axios.post(`${baseUrl}/api/setLedOptions`, sanitizeRequest(options)) .then((response) => { console.log(response.data); return true; @@ -134,7 +138,7 @@ async function setPinMappings(mappings) { let data = {}; Object.keys(mappings).map((button, i) => data[button] = mappings[button].pin); - return axios.post(`${baseUrl}/api/setPinMappings`, data) + return axios.post(`${baseUrl}/api/setPinMappings`, sanitizeRequest(data)) .then((response) => { console.log(response.data); return true; @@ -152,7 +156,7 @@ async function getAddonsOptions() { } async function setAddonsOptions(options) { - return axios.post(`${baseUrl}/api/setAddonsOptions`, options) + return axios.post(`${baseUrl}/api/setAddonsOptions`, sanitizeRequest(options)) .then((response) => { console.log(response.data); return true; @@ -181,6 +185,12 @@ async function reboot() { .catch(console.error); } +function sanitizeRequest(request) { + const newRequest = {...request}; + delete newRequest.usedPins; + return newRequest; +} + const WebApi = { resetSettings, getDisplayOptions,