fix board admin validateSettings function

This commit is contained in:
plebeius.eth
2023-08-19 16:57:58 +02:00
parent ca4e4d2a7f
commit 0a62130ece
+45 -22
View File
@@ -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,
@@ -81,23 +103,24 @@ const BoardSettings = ({ subplebbit }) => {
}, [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,25 +213,25 @@ 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 = () => {
setBoardSettingsJson(JSON.stringify(initialSettings, null, 2)); setBoardSettingsJson(JSON.stringify(initialSettings, null, 2));
}; };