From 8cb572f8fa39df160793764b58538be0e112188e Mon Sep 17 00:00:00 2001 From: f0x Date: Fri, 16 Sep 2022 19:06:11 +0200 Subject: [PATCH] refactor submit functions --- web/source/settings-panel/admin/actions.js | 19 ++++------ web/source/settings-panel/admin/federation.js | 35 ++++++------------- web/source/settings-panel/admin/settings.js | 21 ++++------- web/source/settings-panel/lib/submit.js | 1 - web/source/settings-panel/user/profile.js | 19 ++++------ web/source/settings-panel/user/settings.js | 23 +++++------- 6 files changed, 37 insertions(+), 81 deletions(-) diff --git a/web/source/settings-panel/admin/actions.js b/web/source/settings-panel/admin/actions.js index a9d779312..d4980d021 100644 --- a/web/source/settings-panel/admin/actions.js +++ b/web/source/settings-panel/admin/actions.js @@ -25,6 +25,7 @@ const Redux = require("react-redux"); const Submit = require("../components/submit"); const api = require("../lib/api"); +const submit = require("../lib/submit"); module.exports = function AdminActionPanel() { const dispatch = Redux.useDispatch(); @@ -34,18 +35,10 @@ module.exports = function AdminActionPanel() { const [errorMsg, setError] = React.useState(""); const [statusMsg, setStatus] = React.useState(""); - function submit() { - setStatus("PATCHing"); - setError(""); - return Promise.try(() => { - return dispatch(api.admin.mediaCleanup(days)); - }).then(() => { - setStatus("Saved!"); - }).catch((e) => { - setError(e.message); - setStatus(""); - }); - } + const removeMedia = submit( + () => dispatch(api.admin.mediaCleanup(days)), + {setStatus, setError} + ); return ( <> @@ -61,7 +54,7 @@ module.exports = function AdminActionPanel() { setDays(e.target.value)}/> - + ); diff --git a/web/source/settings-panel/admin/federation.js b/web/source/settings-panel/admin/federation.js index 1c5070efc..d57c0e1a4 100644 --- a/web/source/settings-panel/admin/federation.js +++ b/web/source/settings-panel/admin/federation.js @@ -28,6 +28,7 @@ const { formFields } = require("../components/form-fields"); const api = require("../lib/api"); const adminActions = require("../redux/reducers/admin").actions; +const submit = require("../lib/submit"); const base = "/settings/admin/federation"; @@ -334,31 +335,15 @@ function InstancePage({domain, Form}) { } } - function submit() { - setStatus("PATCHing"); - setError(""); - return Promise.try(() => { - return dispatch(api.admin.updateDomainBlock(domain)); - }).then(() => { - setStatus("Saved!"); - }).catch((e) => { - setError(e.message); - setStatus(""); - }); - } + const updateBlock = submit( + () => dispatch(api.admin.updateDomainBlock(domain)), + {setStatus, setError} + ); - function removeBlock() { - setStatus("removing"); - setError(""); - return Promise.try(() => { - return dispatch(api.admin.removeDomainBlock(domain)); - }).then(() => { - setStatus("removed"); - }).catch((e) => { - setError(e.message); - setStatus(""); - }); - } + const removeBlock = submit( + () => dispatch(api.admin.removeDomainBlock(domain)), + {setStatus, setError, startStatus: "Removing", successStatus: "Removed!"} + ); return (
@@ -381,7 +366,7 @@ function InstancePage({domain, Form}) { />
- + {!entry.new && diff --git a/web/source/settings-panel/admin/settings.js b/web/source/settings-panel/admin/settings.js index c88aa4549..09c8580ac 100644 --- a/web/source/settings-panel/admin/settings.js +++ b/web/source/settings-panel/admin/settings.js @@ -25,6 +25,8 @@ const Redux = require("react-redux"); const Submit = require("../components/submit"); const api = require("../lib/api"); +const submit = require("../lib/submit"); + const adminActions = require("../redux/reducers/instances").actions; const { @@ -35,23 +37,14 @@ const { module.exports = function AdminSettings() { const dispatch = Redux.useDispatch(); - const instance = Redux.useSelector(state => state.instances.adminSettings); const [errorMsg, setError] = React.useState(""); const [statusMsg, setStatus] = React.useState(""); - function submit() { - setStatus("PATCHing"); - setError(""); - return Promise.try(() => { - return dispatch(api.admin.updateInstance()); - }).then(() => { - setStatus("Saved!"); - }).catch((e) => { - setError(e.message); - setStatus(""); - }); - } + const updateSettings = submit( + () => dispatch(api.admin.updateInstance()), + {setStatus, setError} + ); return (
@@ -111,7 +104,7 @@ module.exports = function AdminSettings() { />
*/} - +
); }; \ No newline at end of file diff --git a/web/source/settings-panel/lib/submit.js b/web/source/settings-panel/lib/submit.js index 4092b292b..f268b5cf9 100644 --- a/web/source/settings-panel/lib/submit.js +++ b/web/source/settings-panel/lib/submit.js @@ -34,7 +34,6 @@ module.exports = function submit(func, { }).then(() => { setStatus(successStatus); if (onSuccess != undefined) { - console.log("running", onSuccess); return onSuccess(); } }).catch((e) => { diff --git a/web/source/settings-panel/user/profile.js b/web/source/settings-panel/user/profile.js index e06d3cde1..7cf3a7b52 100644 --- a/web/source/settings-panel/user/profile.js +++ b/web/source/settings-panel/user/profile.js @@ -26,6 +26,7 @@ const Submit = require("../components/submit"); const api = require("../lib/api"); const user = require("../redux/reducers/user").actions; +const submit = require("../lib/submit"); const { formFields } = require("../components/form-fields"); @@ -46,18 +47,10 @@ module.exports = function UserProfile() { const [errorMsg, setError] = React.useState(""); const [statusMsg, setStatus] = React.useState(""); - function submit() { - setStatus("PATCHing"); - setError(""); - return Promise.try(() => { - return dispatch(api.user.updateProfile()); - }).then(() => { - setStatus("Saved!"); - }).catch((e) => { - setError(e.message); - setStatus(""); - }); - } + const saveProfile = submit( + () => dispatch(api.user.updateProfile()), + {setStatus, setError} + ); return (
@@ -114,7 +107,7 @@ module.exports = function UserProfile() { Learn more about custom profile CSS (opens in a new tab) } - +
); }; \ No newline at end of file diff --git a/web/source/settings-panel/user/settings.js b/web/source/settings-panel/user/settings.js index 29f382cb9..47d1a41d7 100644 --- a/web/source/settings-panel/user/settings.js +++ b/web/source/settings-panel/user/settings.js @@ -24,6 +24,7 @@ const Redux = require("react-redux"); const api = require("../lib/api"); const user = require("../redux/reducers/user").actions; +const submit = require("../lib/submit"); const Languages = require("../components/languages"); const Submit = require("../components/submit"); @@ -39,18 +40,10 @@ module.exports = function UserSettings() { const [errorMsg, setError] = React.useState(""); const [statusMsg, setStatus] = React.useState(""); - function submit() { - setStatus("PATCHing"); - setError(""); - return Promise.try(() => { - return dispatch(api.user.updateSettings()); - }).then(() => { - setStatus("Saved!"); - }).catch((e) => { - setError(e.message); - setStatus(""); - }); - } + const updateSettings = submit( + () => dispatch(api.user.updateSettings()), + {setStatus, setError} + ); return ( <> @@ -82,7 +75,7 @@ module.exports = function UserSettings() { name="Mark my posts as sensitive by default" /> - +
@@ -101,7 +94,7 @@ function PasswordChange() { const [newPassword, setNewPassword] = React.useState(""); const [newPasswordConfirm, setNewPasswordConfirm] = React.useState(""); - function submit() { + function changePassword() { if (newPassword !== newPasswordConfirm) { setError("New password and confirm new password did not match!"); return; @@ -141,7 +134,7 @@ function PasswordChange() { setNewPasswordConfirm(e.target.value)} />
- + ); } \ No newline at end of file