From 9b139b632098e6741b10fa87ff6224dcb5045947 Mon Sep 17 00:00:00 2001 From: f0x52 Date: Wed, 18 Jan 2023 14:45:14 +0100 Subject: [frogend] Settings refactor (#1318) * yakshave new form field structure * fully refactor user profile settings form * use rtk query api for profile settings * refactor user post settings * refactor password change form * refactor admin settings * FormWithData structure for user forms * admin actions refactor * whitespace * fix user settings data prop * remove superfluous logging * cleanup old code * refactor federation/suspend (overview, detail) * mostly abstracted (emoji) checkbox list * refactor parse-from-toot * refactor custom-emoji, progress on federation bulk * loading icon styling to prevent big spinny * refactor federation import-export interface * cleanup old files * [chore] Update/add license headers for 2023 * redux fixes * text-field exports * appease the linter * refactor authentication with RTK Query * fix login/logout state transition weirdness * fixes/cleanup * small linter-related fixes * add eslint license header check, fix existing files * remove old code, clarify comment * clarify suspend on subdomains * collapse if/else * fa-fw width info comment --- web/source/settings/index.js | 146 +++++++++---------------------------------- 1 file changed, 28 insertions(+), 118 deletions(-) (limited to 'web/source/settings/index.js') diff --git a/web/source/settings/index.js b/web/source/settings/index.js index fb9eb79f7..6de72581e 100644 --- a/web/source/settings/index.js +++ b/web/source/settings/index.js @@ -18,20 +18,16 @@ "use strict"; -const Promise = require("bluebird"); const React = require("react"); const ReactDom = require("react-dom/client"); -const Redux = require("react-redux"); -const { Switch, Route, Redirect } = require("wouter"); const { Provider } = require("react-redux"); const { PersistGate } = require("redux-persist/integration/react"); +const { Switch, Route, Redirect } = require("wouter"); -const { store, persistor } = require("./redux"); -const api = require("./lib/api"); -const oauth = require("./redux/reducers/oauth").actions; -const { AuthenticationError } = require("./lib/errors"); +const query = require("./lib/query"); -const Login = require("./components/login"); +const { store, persistor } = require("./redux"); +const AuthorizationGate = require("./components/authorization"); const Loading = require("./components/loading"); require("./style.css"); @@ -46,7 +42,7 @@ const nav = { adminOnly: true, "Instance Settings": require("./admin/settings.js"), "Actions": require("./admin/actions"), - "Federation": require("./admin/federation.js"), + "Federation": require("./admin/federation"), }, "Custom Emoji": { adminOnly: true, @@ -57,123 +53,37 @@ const nav = { const { sidebar, panelRouter } = require("./lib/get-views")(nav); -function App() { - const dispatch = Redux.useDispatch(); - - const { loginState, isAdmin } = Redux.useSelector((state) => state.oauth); - const reduxTempStatus = Redux.useSelector((state) => state.temporary.status); +function App({ account }) { + const isAdmin = account.role == "admin"; + const [logoutQuery] = query.useLogoutMutation(); - const [errorMsg, setErrorMsg] = React.useState(); - const [tokenChecked, setTokenChecked] = React.useState(false); - - React.useEffect(() => { - if (loginState == "login" || loginState == "callback") { - Promise.try(() => { - // Process OAUTH authorization token from URL if available - if (loginState == "callback") { - let urlParams = new URLSearchParams(window.location.search); - let code = urlParams.get("code"); - - if (code == undefined) { - setErrorMsg(new Error("Waiting for OAUTH callback but no ?code= provided. You can try logging in again:")); - } else { - return dispatch(api.oauth.tokenize(code)); - } - } - }).then(() => { - // Fetch current instance info - return dispatch(api.instance.fetch()); - }).then(() => { - // Check currently stored auth token for validity if available - return dispatch(api.user.fetchAccount()); - }).then(() => { - setTokenChecked(true); - - return dispatch(api.oauth.checkIfAdmin()); - }).catch((e) => { - if (e instanceof AuthenticationError) { - dispatch(oauth.remove()); - e.message = "Stored OAUTH token no longer valid, please log in again."; - } - setErrorMsg(e); - console.error(e); - }); - } - }, [loginState, dispatch]); - - let ErrorElement = null; - if (errorMsg != undefined) { - ErrorElement = ( -
- {errorMsg.type} - {errorMsg.message} + return ( + <> +
+ {sidebar.all} + {isAdmin && sidebar.admin} +
- ); - } - - const LogoutElement = ( - - ); - - if (reduxTempStatus != undefined) { - return ( -
- {reduxTempStatus} -
- ); - } else if (tokenChecked && loginState == "login") { - return ( - <> -
- {sidebar.all} - {isAdmin && sidebar.admin} - {LogoutElement} -
-
- {ErrorElement} - - {panelRouter.all} - {isAdmin && panelRouter.admin} - {/* default route */} - - - -
- - ); - } else if (loginState == "none") { - return ( - - ); - } else { - let status; - - if (loginState == "login") { - status = "Verifying stored login..."; - } else if (loginState == "callback") { - status = "Processing OAUTH callback..."; - } - - return ( -
-
- {status} -
- {ErrorElement} - {LogoutElement} +
+ + {panelRouter.all} + {isAdmin && panelRouter.admin} + + + +
- ); - } - + + ); } function Main() { return ( -
} persistor={persistor}> - + } persistor={persistor}> + ); -- cgit v1.2.3