mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix board admin validateSettings function
This commit is contained in:
@@ -16,6 +16,28 @@ const BoardSettings = ({ subplebbit }) => {
|
|||||||
selectedStyle,
|
selectedStyle,
|
||||||
} = useGeneralStore(state => state);
|
} = useGeneralStore(state => state);
|
||||||
|
|
||||||
|
const allowedSettings = {
|
||||||
|
address: subplebbit.address,
|
||||||
|
apiUrl: subplebbit.apiUrl,
|
||||||
|
description: subplebbit.description,
|
||||||
|
pubsubTopic: subplebbit.pubsubTopic,
|
||||||
|
settings: {
|
||||||
|
fetchThumbnailUrls: subplebbit.settings?.fetchThumbnailUrls,
|
||||||
|
fetchThumbnailUrlsProxyUrl: subplebbit.settings?.fetchThumbnailUrlsProxyUrl,
|
||||||
|
},
|
||||||
|
roles: subplebbit.roles,
|
||||||
|
rules: subplebbit.rules,
|
||||||
|
suggested: {
|
||||||
|
avatarUrl: subplebbit.suggested?.avatarUrl,
|
||||||
|
backgroundUrl: subplebbit.suggested?.backgroundUrl,
|
||||||
|
bannerUrl: subplebbit.suggested?.bannerUrl,
|
||||||
|
language: subplebbit.suggested?.language,
|
||||||
|
primaryColor: subplebbit.suggested?.primaryColor,
|
||||||
|
secondaryColor: subplebbit.suggested?.secondaryColor,
|
||||||
|
},
|
||||||
|
title: subplebbit.title,
|
||||||
|
};
|
||||||
|
|
||||||
const generateSettingsFromSubplebbit = (subplebbitData) => ({
|
const generateSettingsFromSubplebbit = (subplebbitData) => ({
|
||||||
address: subplebbitData.address,
|
address: subplebbitData.address,
|
||||||
apiUrl: subplebbitData.apiUrl,
|
apiUrl: subplebbitData.apiUrl,
|
||||||
@@ -79,25 +101,26 @@ const BoardSettings = ({ subplebbit }) => {
|
|||||||
isInitialMount.current = false;
|
isInitialMount.current = false;
|
||||||
}
|
}
|
||||||
}, [subplebbit]);
|
}, [subplebbit]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function validateSettings(updatedSettings, allowedSettings) {
|
function validateSettings(updatedSettings, allowedSettings) {
|
||||||
if (!allowedSettings) throw new Error(`Allowed settings structure does not match updated settings.`);
|
|
||||||
|
|
||||||
for (let key in updatedSettings) {
|
for (let key in updatedSettings) {
|
||||||
if (!allowedSettings.hasOwnProperty(key)) {
|
if (!allowedSettings.hasOwnProperty(key) && !initialSettings.hasOwnProperty(key)) {
|
||||||
throw new Error(`Unexpected setting: ${key}`);
|
throw new Error(`Unexpected setting: ${key}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof updatedSettings[key] === 'object' && updatedSettings[key] !== null) {
|
if (typeof updatedSettings[key] === 'object' && updatedSettings[key] !== null
|
||||||
validateSettings(updatedSettings[key], allowedSettings[key]);
|
&& !Array.isArray(updatedSettings[key])) {
|
||||||
|
if (typeof allowedSettings[key] !== 'object' || allowedSettings[key] === null
|
||||||
|
|| Array.isArray(allowedSettings[key])) {
|
||||||
|
throw new Error(`Expected ${key} to be an object in allowedSettings`);
|
||||||
|
}
|
||||||
|
validateSettings(updatedSettings[key], allowedSettings[key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const onChallenge = async (challenges, subplebbitEdit) => {
|
const onChallenge = async (challenges, subplebbitEdit) => {
|
||||||
let challengeAnswers = [];
|
let challengeAnswers = [];
|
||||||
|
|
||||||
@@ -190,23 +213,23 @@ const BoardSettings = ({ subplebbit }) => {
|
|||||||
|
|
||||||
const handleSaveChanges = async () => {
|
const handleSaveChanges = async () => {
|
||||||
try {
|
try {
|
||||||
const updatedSettings = JSON.parse(boardSettingsJson);
|
const updatedSettings = JSON.parse(boardSettingsJson);
|
||||||
validateSettings(updatedSettings, initialSettings);
|
validateSettings(updatedSettings, allowedSettings);
|
||||||
const changes = getDifferences(initialSettings, updatedSettings);
|
const changes = getDifferences(initialSettings, updatedSettings);
|
||||||
if (Object.keys(changes).length > 0) {
|
if (Object.keys(changes).length > 0) {
|
||||||
setEditSubplebbitOptions(prevOptions => ({
|
setEditSubplebbitOptions(prevOptions => ({
|
||||||
...prevOptions,
|
...prevOptions,
|
||||||
...changes
|
...changes
|
||||||
}));
|
}));
|
||||||
setTriggerPublishCommentEdit(true);
|
setTriggerPublishCommentEdit(true);
|
||||||
} else {
|
} else {
|
||||||
setNewErrorMessage("No changes detected");
|
setNewErrorMessage("No changes detected");
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setNewErrorMessage(`Error saving changes: ${error}`);
|
setNewErrorMessage(`Error saving changes: ${error}`);
|
||||||
|
console.log(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const handleResetChanges = () => {
|
const handleResetChanges = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user