Skip to content
This repository has been archived by the owner on May 17, 2023. It is now read-only.

Commit

Permalink
Fix settings restoration (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
deeebug authored Mar 8, 2023
1 parent 0689802 commit d35e0a8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/Pages/BackupPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand All @@ -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];
Expand Down Expand Up @@ -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];
Expand Down
26 changes: 18 additions & 8 deletions src/Services/WebApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,21 @@ 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);
newOptions.splashMode = parseInt(options.splashMode);
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) => {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d35e0a8

Please sign in to comment.