Skip to content
Snippets Groups Projects
Commit df75b05e authored by Mehmet Salih Hasanoglu's avatar Mehmet Salih Hasanoglu
Browse files

frontend state updated

parent ad0f43e1
No related branches found
No related tags found
No related merge requests found
...@@ -3,8 +3,6 @@ import Reactions from "./components/reactions"; ...@@ -3,8 +3,6 @@ import Reactions from "./components/reactions";
import "./App.css"; import "./App.css";
import logo from "./ZRoom_Logo.png"; import logo from "./ZRoom_Logo.png";
//import { render } from "@testing-library/react";
class App extends Component { class App extends Component {
state = { state = {
reactions: [ reactions: [
...@@ -27,13 +25,27 @@ class App extends Component { ...@@ -27,13 +25,27 @@ class App extends Component {
return new Promise(resolve => setTimeout(resolve, milliseconds)) return new Promise(resolve => setTimeout(resolve, milliseconds))
} }
componentDidUpdate(previousProps, previousState) { componentDidUpdate(prevProps, prevState) {
this.sleep(5000).then(r => { this.sleep(5000).then(r => {
if (previousState.reactions !== this.state.reactions) { for (var i = 0; i < this.state.reactions.length; i++) {
this.state.reactions = previousState.reactions if (this.state.reactions[i].value == "Befehl wird gesendet ...") {
this.setState(this.state.reactions); this.setState({
reactions: [
{ id: 1, value: "Frage ?" },
{ id: 2, value: "Daumen Hoch" },
{ id: 3, value: "Problem !" },
],
})
}
} }
}) })
// this.sleep(5000).then(r => {
// if (previousState.reactions !== this.state.reactions) {
// this.state.reactions = previousState.reactions
// this.setState(this.state.reactions);
// }
// })
} }
handleClick = (reaction) => { handleClick = (reaction) => {
...@@ -42,40 +54,24 @@ class App extends Component { ...@@ -42,40 +54,24 @@ class App extends Component {
reactions[index] = { ...reaction }; reactions[index] = { ...reaction };
reactions[index].value = "Befehl wird gesendet ..."; reactions[index].value = "Befehl wird gesendet ...";
// for (var i = 0; i < reactions.length; i++) { for (var i = 0; i < reactions.length; i++) {
// reactions[i].value = index === i ? "Befehl wird gesendet ..." : "Warten ..."; reactions[i].value = index === i ? "Befehl wird gesendet ..." : "Warten ...";
// } }
const requestURL = "http://localhost:5000/lamp/" + reactions[index].id + "/activate" const requestURL = "http://0.0.0.0:5000/lamp/" + reactions[index].id + "/activate"
//alert(requestURL)
fetch(requestURL) fetch(requestURL)
this.setState({ reactions }); this.setState({ reactions });
}; };
handleReset = () => {
const reactions = this.state.reactions.map((c) => {
c.value = 0;
return c;
});
this.setState({ reactions });
};
handleDelete = (reactionId) => {
const reactions = this.state.reactions.filter((c) => c.id !== reactionId);
this.setState({ reactions });
};
render() { render() {
console.log("App - Rendered"); console.log("App - Rendered");
return ( return (
<React.Fragment> <React.Fragment>
<main className="container"> <main className="container">
<img src={logo} alt="ZRoom Logo" class="rounded mx-auto d-block m-5" /> <img src={logo} alt="ZRoom Logo" className="rounded mx-auto d-block m-5" />
<Reactions <Reactions
reactions={this.state.reactions} reactions={this.state.reactions}
onReset={this.handleReset}
onClick={this.handleClick} onClick={this.handleClick}
onDelete={this.handleDelete}
/> />
</main> </main>
</React.Fragment> </React.Fragment>
......
...@@ -19,9 +19,9 @@ class Reaction extends Component { ...@@ -19,9 +19,9 @@ class Reaction extends Component {
componentDidUpdate(prevProps, prevState) { componentDidUpdate(prevProps, prevState) {
console.log("prevProps", prevProps); console.log("prevProps", prevProps);
console.log("prevState", prevState); console.log("prevState", prevState);
if (prevProps.reaction.value !== this.props.reaction.value) { // if (prevProps.reaction.value !== this.props.reaction.value) {
// Ajax call and get new data from the server // // Ajax call and get new data from the server
} // }
} }
componentWillUnmount() { componentWillUnmount() {
...@@ -31,19 +31,12 @@ class Reaction extends Component { ...@@ -31,19 +31,12 @@ class Reaction extends Component {
console.log("Reaction - Rendered"); console.log("Reaction - Rendered");
return ( return (
<div> <div>
{/* <span className={this.getBadgeClasses()}>{this.getValue()}</span> */} <button disabled={this.getValue() == "Warten ..." || this.getValue() == "Befehl wird gesendet ..."}
<button
onClick={() => this.props.onClick(this.props.reaction)} onClick={() => this.props.onClick(this.props.reaction)}
className={this.getBadgeClasses()} className={this.getBadgeClasses()}
> >
{this.getValue()} {this.getValue()}
</button> </button>
{/* <button
onClick={() => this.props.onDelete(this.props.reaction.id)}
className="btn btn-danger btn-sm m-2"
>
Delete
</button> */}
</div> </div>
); );
} }
......
...@@ -4,16 +4,12 @@ import Reaction from "./reaction"; ...@@ -4,16 +4,12 @@ import Reaction from "./reaction";
class Reactions extends Component { class Reactions extends Component {
render() { render() {
console.log("Reactions - Rendered"); console.log("Reactions - Rendered");
const { onReset, reactions, onDelete, onClick } = this.props; const { reactions, onClick } = this.props;
return ( return (
<div> <div>
<button onClick={onReset} className="btn btn-primary btn-sm m-2">
Reset
</button>
{reactions.map((reaction) => ( {reactions.map((reaction) => (
<Reaction <Reaction
key={reaction.id} key={reaction.id}
onDelete={onDelete}
onClick={onClick} onClick={onClick}
reaction={reaction} reaction={reaction}
/> />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment