refac appsettings page

This commit is contained in:
Simon
2025-01-09 22:16:39 +07:00
parent 1a6f51a122
commit f6b5de00df
5 changed files with 492 additions and 801 deletions

View File

@@ -1,37 +1,12 @@
import APIClient from '../../functions/APIClient';
import { AppSettingsConfigType } from '../loader/loadAppsettingsConfig';
type ObjectType = Record<
string,
string | number | boolean | Record<string, string | number | boolean>
>;
function flattenObject(ob: ObjectType) {
// source: https://stackoverflow.com/a/53739792
const toReturn: ObjectType = {};
for (const i in ob) {
if (!Object.prototype.hasOwnProperty.call(ob, i)) continue;
if (typeof ob[i] == 'object' && ob[i] !== null) {
const flatObject = flattenObject(ob[i]);
for (const x in flatObject) {
if (!Object.prototype.hasOwnProperty.call(flatObject, x)) continue;
toReturn[i + '.' + x] = flatObject[x];
}
} else {
toReturn[i] = ob[i];
}
}
return toReturn;
}
const updateAppsettingsConfig = async (config: AppSettingsConfigType) => {
const updateAppsettingsConfig = async (
configKey: string,
configValue: string | boolean | number | null,
) => {
return APIClient('/api/appsettings/config/', {
method: 'POST',
body: flattenObject(config),
body: { [configKey]: configValue },
});
};