-
Notifications
You must be signed in to change notification settings - Fork 405
/
10-example-app.html
144 lines (119 loc) · 3.09 KB
/
10-example-app.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!doctype html>
<title>10 Example App - React From Zero</title>
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/[email protected]/create-react-class.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<div id="app"></div>
<script type="text/babel">
var Idle = function(props) {
return (
<React.Fragment>
<button onClick={props.onStart}>Start timer</button>
<h2>No Timer Running.</h2>
</React.Fragment>
);
};
var Timer = createReactClass({
getInitialState: function() {
return { seconds: this.props.minutes * 60 };
},
countDown: function() {
var onFinish = this.props.onFinish;
this.setState(function(prevState) {
var secondsLeft = prevState.seconds - 1;
if (secondsLeft < 1) return onFinish();
return { seconds: secondsLeft };
});
},
interval: null,
componentDidMount: function() {
this.interval = setInterval(this.countDown, 1000);
},
componentWillUnmount: function() {
clearInterval(this.interval);
},
render: function() {
var seconds = this.state.seconds;
var minutes = Math.floor(seconds / 60);
seconds = seconds % 60;
return (
<React.Fragment>
<button onClick={this.props.onStop}>Stop timer</button>
<h2 style={{ color: this.props.color }}>
{this.props.title} for {minutes} minutes and {seconds}{" "}
seconds.
</h2>
</React.Fragment>
);
}
});
var PomodoroApp = createReactClass({
IDLE: 0,
WORK: 1,
PAUSE: 2,
getInitialState: function() {
return {
count: 0,
step: this.IDLE
};
},
handleWork: function() {
this.setState({ step: this.WORK });
},
handlePause: function() {
this.setState(function(prevState) {
return {
count: prevState.count + 1,
step: this.PAUSE
};
});
},
handleIdle: function() {
this.setState({ step: this.IDLE });
},
getTimerElement: function() {
var step = this.state.step;
if (step == this.PAUSE)
return (
<Timer
key="pause"
title="Pause"
color="green"
minutes={5}
onFinish={this.handleWork}
onStop={this.handleIdle}
/>
);
if (step == this.WORK)
return (
<Timer
key="work"
title="Work"
color="orange"
minutes={25}
onFinish={this.handlePause}
onStop={this.handleIdle}
/>
);
return <Idle onStart={this.handleWork} />;
},
style: {
display: "flex",
flexDirection: "column",
alignItems: "center"
},
render: function() {
var count = this.state.count;
return (
<div style={this.style}>
<h1>Pomodoro Timer</h1>
{this.getTimerElement()}
{!!count && <h2>You worked {count * 25} minutes today!</h2>}
</div>
);
}
});
var renderTarget = document.getElementById("app");
ReactDOM.render(<PomodoroApp />, renderTarget);
</script>