add initial fetch of generalsettings on appstart to get password support and mediacentername

This commit is contained in:
lukas 2020-07-13 22:56:43 +02:00
parent 537d869338
commit 5662a6e6e5
3 changed files with 64 additions and 15 deletions

View File

@ -18,7 +18,7 @@ if (isset($_POST['action'])) {
if ($r['password'] != "-1") { if ($r['password'] != "-1") {
$r['passwordEnabled'] = true; $r['passwordEnabled'] = true;
} else { } else {
$r['passwordEnabled'] = true; $r['passwordEnabled'] = false;
} }
echo json_encode($r); echo json_encode($r);
break; break;
@ -41,5 +41,22 @@ if (isset($_POST['action'])) {
echo '{"success": true}'; echo '{"success": true}';
} }
break; 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;
} }
} }

View File

@ -11,37 +11,62 @@ import CategoryPage from "./pages/CategoryPage/CategoryPage";
class App extends React.Component { class App extends React.Component {
constructor(props, context) { constructor(props, context) {
super(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 // bind this to the method for being able to call methods such as this.setstate
this.changeRootElement = this.changeRootElement.bind(this); this.changeRootElement = this.changeRootElement.bind(this);
this.returnToLastElement = this.returnToLastElement.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; newElement = null;
constructViewBinding(){
return {
changeRootElement: this.changeRootElement,
returnToLastElement: this.returnToLastElement,
generalsettings: this.generaldata
};
}
MainBody() { MainBody() {
let page; let page;
if (this.state.page === "default") { if (this.state.page === "default") {
page = <HomePage viewbinding={{ page = <HomePage viewbinding={this.constructViewBinding()}/>;
changeRootElement: this.changeRootElement,
returnToLastElement: this.returnToLastElement
}}/>;
this.mypage = page; this.mypage = page;
} else if (this.state.page === "random") { } else if (this.state.page === "random") {
page = <RandomPage viewbinding={{ page = <RandomPage viewbinding={this.constructViewBinding()}/>;
changeRootElement: this.changeRootElement,
returnToLastElement: this.returnToLastElement
}}/>;
this.mypage = page; this.mypage = page;
} else if (this.state.page === "settings") { } else if (this.state.page === "settings") {
page = <SettingsPage/>; page = <SettingsPage/>;
this.mypage = page; this.mypage = page;
} else if (this.state.page === "categories") { } else if (this.state.page === "categories") {
page = <CategoryPage viewbinding={{ page = <CategoryPage viewbinding={this.constructViewBinding()}/>;
changeRootElement: this.changeRootElement,
returnToLastElement: this.returnToLastElement
}}/>;
this.mypage = page; this.mypage = page;
} else if (this.state.page === "video") { } else if (this.state.page === "video") {
// show videoelement if neccessary // show videoelement if neccessary
@ -90,7 +115,7 @@ class App extends React.Component {
</li> </li>
</ul> </ul>
</nav> </nav>
{this.MainBody()} {this.state.generalSettingsLoaded ? this.MainBody() : "loading"}
</div> </div>
); );
} }

View File

@ -20,6 +20,7 @@ describe('<App/>', function () {
it('simulate video view change ', function () { it('simulate video view change ', function () {
const wrapper = shallow(<App/>); const wrapper = shallow(<App/>);
wrapper.setState({generalSettingsLoaded: true}); // simulate fetch to have already finisheed
wrapper.instance().changeRootElement(<div id='testit'></div>); wrapper.instance().changeRootElement(<div id='testit'></div>);
@ -28,6 +29,7 @@ describe('<App/>', function () {
it('test hide video again', function () { it('test hide video again', function () {
const wrapper = shallow(<App/>); const wrapper = shallow(<App/>);
wrapper.setState({generalSettingsLoaded: true}); // simulate fetch to have already finisheed
wrapper.instance().changeRootElement(<div id='testit'></div>); wrapper.instance().changeRootElement(<div id='testit'></div>);
@ -40,6 +42,7 @@ describe('<App/>', function () {
it('test fallback to last loaded page', function () { it('test fallback to last loaded page', function () {
const wrapper = shallow(<App/>); const wrapper = shallow(<App/>);
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"); wrapper.find(".nav-link").findWhere(t => t.text() === "Random Video" && t.type() === "div").simulate("click");
@ -54,6 +57,8 @@ describe('<App/>', function () {
it('test home click', function () { it('test home click', function () {
const wrapper = shallow(<App/>); const wrapper = shallow(<App/>);
wrapper.setState({generalSettingsLoaded: true}); // simulate fetch to have already finisheed
wrapper.setState({page: "wrongvalue"}); wrapper.setState({page: "wrongvalue"});
expect(wrapper.find("HomePage")).toHaveLength(0); expect(wrapper.find("HomePage")).toHaveLength(0);
wrapper.find(".nav-link").findWhere(t => t.text() === "Home" && t.type() === "div").simulate("click"); wrapper.find(".nav-link").findWhere(t => t.text() === "Home" && t.type() === "div").simulate("click");
@ -62,6 +67,7 @@ describe('<App/>', function () {
it('test category click', function () { it('test category click', function () {
const wrapper = shallow(<App/>); const wrapper = shallow(<App/>);
wrapper.setState({generalSettingsLoaded: true}); // simulate fetch to have already finisheed
expect(wrapper.find("CategoryPage")).toHaveLength(0); expect(wrapper.find("CategoryPage")).toHaveLength(0);
wrapper.find(".nav-link").findWhere(t => t.text() === "Categories" && t.type() === "div").simulate("click"); wrapper.find(".nav-link").findWhere(t => t.text() === "Categories" && t.type() === "div").simulate("click");
@ -70,6 +76,7 @@ describe('<App/>', function () {
it('test settings click', function () { it('test settings click', function () {
const wrapper = shallow(<App/>); const wrapper = shallow(<App/>);
wrapper.setState({generalSettingsLoaded: true}); // simulate fetch to have already finisheed
expect(wrapper.find("SettingsPage")).toHaveLength(0); expect(wrapper.find("SettingsPage")).toHaveLength(0);
wrapper.find(".nav-link").findWhere(t => t.text() === "Settings" && t.type() === "div").simulate("click"); wrapper.find(".nav-link").findWhere(t => t.text() === "Settings" && t.type() === "div").simulate("click");