From 133851fe0db6dce69d4665304592c96fcfc6da96 Mon Sep 17 00:00:00 2001 From: Lukas Heiligenbrunner Date: Fri, 10 Jul 2020 01:18:23 +0200 Subject: [PATCH] added php code to get settings from database to react state add onchange events to change state on field change --- api/Settings.php | 26 ++++++++++++++++ src/pages/SettingsPage/GeneralSettings.js | 38 ++++++++++++++++------- 2 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 api/Settings.php diff --git a/api/Settings.php b/api/Settings.php new file mode 100644 index 0000000..4afb181 --- /dev/null +++ b/api/Settings.php @@ -0,0 +1,26 @@ +getConnection(); + +if (isset($_POST['action'])) { + $action = $_POST['action']; + switch ($action) { + case "loadGeneralSettings": + $query = "SELECT * from settings"; + + $result = $conn->query($query); + if ($result->num_rows > 1) { + // todo throw error + } + + $r = mysqli_fetch_assoc($result); + if ($r['password'] != "-1") { + $r['passwordEnabled'] = true; + } else { + $r['passwordEnabled'] = true; + } + echo json_encode($r); + break; + } +} diff --git a/src/pages/SettingsPage/GeneralSettings.js b/src/pages/SettingsPage/GeneralSettings.js index 7e1d914..6ec0a98 100644 --- a/src/pages/SettingsPage/GeneralSettings.js +++ b/src/pages/SettingsPage/GeneralSettings.js @@ -7,9 +7,12 @@ class GeneralSettings extends React.Component { super(props); this.state = { - tvshowsupport: false, + passwordsupport: false, + videopath: "", - tvshowpath: "" + tvshowpath: "", + mediacentername: "", + password: "" }; } @@ -17,11 +20,16 @@ class GeneralSettings extends React.Component { const updateRequest = new FormData(); updateRequest.append('action', 'loadGeneralSettings'); - fetch('/api/settings.php', {method: 'POST', body: updateRequest}) + fetch('/api/Settings.php', {method: 'POST', body: updateRequest}) .then((response) => response.json() .then((result) => { - // todo 2020-07-3: set state here - // todo 2020-07-4: php and test code + this.setState({ + videopath: result.video_path, + tvshowpath: result.episode_path, + mediacentername: result.mediacenter_name, + password: result.password, + passwordsupport: result.passwordEnabled + }); })); } @@ -36,12 +44,15 @@ class GeneralSettings extends React.Component { Video Path - + this.setState({videopath: ee.target.value})}/> TV Show Path - + this.setState({tvshowpath: e.target.value})}/> @@ -49,21 +60,24 @@ class GeneralSettings extends React.Component { type="switch" id="custom-switch" label="Enable Password support" + checked={this.state.passwordsupport} onChange={() => { - this.setState({tvshowsupport: !this.state.tvshowsupport}) + this.setState({passwordsupport: !this.state.passwordsupport}) }} /> - {this.state.tvshowsupport ? + {this.state.passwordsupport ? Password - + this.setState({password: e.target.value})}/> : null } - + The name of the Mediacenter - + this.setState({mediacentername: e.target.value})}/>