From 5662a6e6e5b86f5664a388eba444a55ee6d26347 Mon Sep 17 00:00:00 2001 From: lukas Date: Mon, 13 Jul 2020 22:56:43 +0200 Subject: [PATCH] add initial fetch of generalsettings on appstart to get password support and mediacentername --- api/Settings.php | 19 ++++++++++++++++- src/App.js | 53 +++++++++++++++++++++++++++++++++++------------- src/App.test.js | 7 +++++++ 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/api/Settings.php b/api/Settings.php index 059a23d..06933fc 100644 --- a/api/Settings.php +++ b/api/Settings.php @@ -18,7 +18,7 @@ if (isset($_POST['action'])) { if ($r['password'] != "-1") { $r['passwordEnabled'] = true; } else { - $r['passwordEnabled'] = true; + $r['passwordEnabled'] = false; } echo json_encode($r); break; @@ -41,5 +41,22 @@ if (isset($_POST['action'])) { echo '{"success": true}'; } break; + case "loadInitialData": + $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'] = false; + } + unset($r['password']); + echo json_encode($r); + break; } } diff --git a/src/App.js b/src/App.js index 4beed12..fd943e1 100644 --- a/src/App.js +++ b/src/App.js @@ -11,37 +11,62 @@ import CategoryPage from "./pages/CategoryPage/CategoryPage"; class App extends React.Component { constructor(props, context) { super(props, context); - this.state = {page: "default"}; + this.state = { + page: "default", + generalSettingsLoaded: false, + passwordsupport: null + }; // bind this to the method for being able to call methods such as this.setstate this.changeRootElement = this.changeRootElement.bind(this); this.returnToLastElement = this.returnToLastElement.bind(this); } + generaldata = {}; + + componentDidMount() { + const updateRequest = new FormData(); + updateRequest.append('action', 'loadInitialData'); + + fetch('/api/Settings.php', {method: 'POST', body: updateRequest}) + .then((response) => response.json() + .then((result) => { + this.generaldata = { + videopath: result.video_path, + tvshowpath: result.episode_path, + mediacentername: result.mediacenter_name + }; + + this.setState({ + generalSettingsLoaded: true, + passwordsupport: result.passwordEnabled + }); + })); + } + newElement = null; + constructViewBinding(){ + return { + changeRootElement: this.changeRootElement, + returnToLastElement: this.returnToLastElement, + generalsettings: this.generaldata + }; + } + MainBody() { let page; if (this.state.page === "default") { - page = ; + page = ; this.mypage = page; } else if (this.state.page === "random") { - page = ; + page = ; this.mypage = page; } else if (this.state.page === "settings") { page = ; this.mypage = page; } else if (this.state.page === "categories") { - page = ; + page = ; this.mypage = page; } else if (this.state.page === "video") { // show videoelement if neccessary @@ -90,7 +115,7 @@ class App extends React.Component { - {this.MainBody()} + {this.state.generalSettingsLoaded ? this.MainBody() : "loading"} ); } diff --git a/src/App.test.js b/src/App.test.js index a4bbdb1..97d2c3f 100644 --- a/src/App.test.js +++ b/src/App.test.js @@ -20,6 +20,7 @@ describe('', function () { it('simulate video view change ', function () { const wrapper = shallow(); + wrapper.setState({generalSettingsLoaded: true}); // simulate fetch to have already finisheed wrapper.instance().changeRootElement(
); @@ -28,6 +29,7 @@ describe('', function () { it('test hide video again', function () { const wrapper = shallow(); + wrapper.setState({generalSettingsLoaded: true}); // simulate fetch to have already finisheed wrapper.instance().changeRootElement(
); @@ -40,6 +42,7 @@ describe('', function () { it('test fallback to last loaded page', function () { const wrapper = shallow(); + wrapper.setState({generalSettingsLoaded: true}); // simulate fetch to have already finisheed wrapper.find(".nav-link").findWhere(t => t.text() === "Random Video" && t.type() === "div").simulate("click"); @@ -54,6 +57,8 @@ describe('', function () { it('test home click', function () { const wrapper = shallow(); + wrapper.setState({generalSettingsLoaded: true}); // simulate fetch to have already finisheed + wrapper.setState({page: "wrongvalue"}); expect(wrapper.find("HomePage")).toHaveLength(0); wrapper.find(".nav-link").findWhere(t => t.text() === "Home" && t.type() === "div").simulate("click"); @@ -62,6 +67,7 @@ describe('', function () { it('test category click', function () { const wrapper = shallow(); + wrapper.setState({generalSettingsLoaded: true}); // simulate fetch to have already finisheed expect(wrapper.find("CategoryPage")).toHaveLength(0); wrapper.find(".nav-link").findWhere(t => t.text() === "Categories" && t.type() === "div").simulate("click"); @@ -70,6 +76,7 @@ describe('', function () { it('test settings click', function () { const wrapper = shallow(); + wrapper.setState({generalSettingsLoaded: true}); // simulate fetch to have already finisheed expect(wrapper.find("SettingsPage")).toHaveLength(0); wrapper.find(".nav-link").findWhere(t => t.text() === "Settings" && t.type() === "div").simulate("click");