From 0e366843c7c85f0021188e0e288baa6ffbe80f6a Mon Sep 17 00:00:00 2001 From: James Barnsley Date: Sat, 29 Oct 2022 21:54:28 +1300 Subject: [PATCH] Upgrading Debug to functional component --- src/js/views/Debug.js | 571 +++++++++++++++++++----------------------- 1 file changed, 263 insertions(+), 308 deletions(-) diff --git a/src/js/views/Debug.js b/src/js/views/Debug.js index 2f706875..21edd5c9 100755 --- a/src/js/views/Debug.js +++ b/src/js/views/Debug.js @@ -1,360 +1,315 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import Header from '../components/Header'; import Icon from '../components/Icon'; import Button from '../components/Button'; import LinksSentence from '../components/LinksSentence'; -import * as uiActions from '../services/ui/actions'; -import * as pusherActions from '../services/pusher/actions'; -import * as mopidyActions from '../services/mopidy/actions'; -import * as spotifyActions from '../services/spotify/actions'; -import * as snapcastActions from '../services/snapcast/actions'; +import { + set, + setWindowTitle, + startProcess, + createNotification, +} from '../services/ui/actions'; +import { + debug as pusherDebug, + request, +} from '../services/pusher/actions'; +import { debug as mopidyDebug } from '../services/mopidy/actions'; +import { debug as snapcastDebug } from '../services/snapcast/actions'; import { I18n, i18n } from '../locale'; +import { useSelector } from 'react-redux'; +import { useDispatch } from 'react-redux'; +import { useNavigate } from 'react-router-dom'; -class Debug extends React.Component { - constructor(props) { - super(props); - this.state = { - mopidy_call: 'playlists.asList', - mopidy_data: '{}', - pusher_data: '{"method":"get_config"}', - snapcast_data: '{"method":"Server.GetStatus"}', - access_token: this.props.access_token, - toggling_test_mode: false, - }; - } +const Debug = () => { + const navigate = useNavigate(); + const dispatch = useDispatch(); + const { + debug_info, + log_actions, + log_mopidy, + log_pusher, + log_snapcast, + debug_response, + } = useSelector((state) => state.ui); + const uri_schemes = useSelector((state) => state.mopidy.uri_schemes); + const [form, setForm] = useState({ + mopidy_call: 'playlists.asList', + mopidy_data: '{}', + pusher_data: '{"method":"get_config"}', + snapcast_data: '{"method":"Server.GetStatus"}', + }); - componentDidMount() { - const { uiActions: { setWindowTitle } } = this.props; + useEffect(() => { setWindowTitle(i18n('debug.title')); - } + }, []); - onBack = () => { - const { history } = this.props; - history.push('/settings'); - } + const onInputChange = ({ target: { name, value } }) => + setForm((prev) => ({ ...prev, [name]: value })); - callMopidy = (e) => { - const { mopidyActions: { debug } } = this.props; - const { mopidy_call, mopidy_data } = this.state; + const callMopidy = (e) => { + const { mopidy_call, mopidy_data } = form; e.preventDefault(); - debug(mopidy_call, JSON.parse(mopidy_data)); + dispatch(mopidyDebug(mopidy_call, JSON.parse(mopidy_data))); } - callPusher = (e) => { - const { pusherActions: { debug } } = this.props; - const { pusher_data } = this.state; + const callPusher = (e) => { + const { pusher_data } = form; e.preventDefault(); - debug(JSON.parse(pusher_data)); + dispatch(pusherDebug(JSON.parse(pusher_data))); } - callSnapcast = (e) => { - const { snapcastActions: { debug } } = this.props; - const { snapcast_data } = this.state; + const callSnapcast = (e) => { + const { snapcast_data } = form; e.preventDefault(); - debug(JSON.parse(snapcast_data)); + dispatch(snapcastDebug(JSON.parse(snapcast_data))); } - toggleTestMode = () => { - const { uiActions: { set }, test_mode } = this.props; - this.setState({ toggling_test_mode: true }); - set({ test_mode: !test_mode }); + const options = ( + + ); - // Wait a second to allow state to update, and then refresh - setTimeout(location.reload(), 2500); - } + return ( +
+
+ + +
- render = () => { - const { - uiActions, - spotifyActions, - debug_response, - debug_info, - log_actions, - log_mopidy, - log_pusher, - log_snapcast, - access_token, - uri_schemes = [], - } = this.props; - const { - mopidy_call, - mopidy_data, - pusher_data, - snapcast_data, - } = this.state; - const { test_mode } = window; +
- const options = ( - - ); - - return ( -
-
- - -
- -
- -

-
-
-
Test mode
-
- -
+

+ +
+
Test mode
+
+
-
-
-
- -
+
+
+
+
+
-
-
-
- - - - -
+
+
+
+
+ + + +
-
-
-
- -
+
+
+
+ + - -
+ ) + } + tracking={{ category: 'Debug', action: 'Test process' }} + > + + +
- +
+ -

Spotify

+

+ +
- -