1- import React , { useState } from "react" ;
2- import { Slider } from 'react-bootstrap' ;
1+ import React , { useState } from 'react' ;
32
4- function TrainSimulator ( ) {
5- const [ isPlaying , setIsPlaying ] = useState ( false ) ;
3+ function useSimulation ( ) {
4+ const [ speed , setSpeed ] = useState ( 0 ) ;
5+ const [ throttle , setThrottle ] = useState ( 0 ) ;
6+ const [ pressure , setPressure ] = useState ( 0 ) ;
67 const [ intervalId , setIntervalId ] = useState ( null ) ;
7- const [ speed , setSpeed ] = useState ( 50 ) ;
8- const [ throttle , setThrottle ] = useState ( 50 ) ;
9- const [ pressure , setPressure ] = useState ( 50 ) ;
10-
11- const handleSpeedChange = ( event , value ) => {
12- setSpeed ( value ) ;
13- } ;
148
15- const handleThrottleChange = ( event , value ) => {
16- setThrottle ( value ) ;
17- } ;
9+ function startSimulation ( ) {
10+ const newIntervalId = setInterval ( ( ) => {
11+ const simulationData = { speed, throttle, pressure } ;
12+ console . log ( simulationData ) ;
13+ } , 1000 ) ;
1814
19- const handlePressureChange = ( event , value ) => {
20- setPressure ( value ) ;
21- } ;
22-
23- const startSimulation = ( ) => {
24- setIsPlaying ( true ) ;
25- setIntervalId (
26- setInterval ( ( ) => {
27- const data = {
28- speed,
29- throttle,
30- pressure,
31- } ;
32- console . log ( data ) ;
33- } , 1000 )
34- ) ;
35- } ;
15+ setIntervalId ( newIntervalId ) ;
16+ }
3617
37- const stopSimulation = ( ) => {
38- setIsPlaying ( false ) ;
18+ function stopSimulation ( ) {
3919 clearInterval ( intervalId ) ;
4020 setIntervalId ( null ) ;
21+ }
22+
23+ function handleSpeedChange ( event ) {
24+ setSpeed ( parseInt ( event . target . value ) ) ;
25+
26+ }
27+
28+ function handleThrottleChange ( event ) {
29+ setThrottle ( parseInt ( event . target . value ) ) ;
30+
31+ }
32+
33+ function handlePressureChange ( event ) {
34+ setPressure ( parseInt ( event . target . value ) ) ;
35+
36+ }
37+
38+ return {
39+ speed,
40+ throttle,
41+ pressure,
42+ intervalId,
43+ startSimulation,
44+ stopSimulation,
45+ handleSpeedChange,
46+ handleThrottleChange,
47+ handlePressureChange,
4148 } ;
49+ }
50+
51+ function TrainSimulator ( ) {
52+ const {
53+ speed,
54+ throttle,
55+ pressure,
56+ intervalId,
57+ startSimulation,
58+ stopSimulation,
59+ handleSpeedChange,
60+ handleThrottleChange,
61+ handlePressureChange,
62+ } = useSimulation ( ) ;
63+
64+ function stopSimulationHandler ( ) {
65+ stopSimulation ( ) ;
66+ }
67+
68+
4269
4370 return (
4471 < div >
4572 < h1 > Train Simulator</ h1 >
46- < div style = { { display : "flex" , justifyContent : "center" } } >
47- < div >
48- < h2 > Speed</ h2 >
49- < Slider
50- value = { speed }
51- min = { 0 }
52- max = { 100 }
53- onChange = { handleSpeedChange }
54- aria-labelledby = "speed-slider"
55- />
56- < p style = { { textAlign : "center" } } > { speed } </ p >
73+
74+ < div className = "row" >
75+ < div className = "col" >
76+ < div className = "card" >
77+ < div className = "card-body" >
78+ < h5 className = "card-title" > Speed</ h5 >
79+ < input type = "range" min = "0" max = "100" value = { speed } className = "slider" id = "speed-slider" onChange = { handleSpeedChange } />
80+ < p className = "card-text text-center" > { speed } </ p >
81+ </ div >
82+ </ div >
5783 </ div >
58- < div >
59- < h2 > Throttle</ h2 >
60- < Slider
61- value = { throttle }
62- min = { 0 }
63- max = { 100 }
64- onChange = { handleThrottleChange }
65- aria-labelledby = "throttle-slider"
66- />
67- < p style = { { textAlign : "center" } } > { throttle } </ p >
84+
85+ < div className = "col" >
86+ < div className = "card" >
87+ < div className = "card-body" >
88+ < h5 className = "card-title" > Throttle</ h5 >
89+ < input type = "range" min = "0" max = "100" value = { throttle } className = "slider" id = "throttle-slider" onChange = { handleThrottleChange } />
90+ < p className = "card-text text-center" > { throttle } </ p >
91+ </ div >
92+ </ div >
6893 </ div >
69- < div >
70- < h2 > Pressure</ h2 >
71- < Slider
72- value = { pressure }
73- min = { 0 }
74- max = { 100 }
75- onChange = { handlePressureChange }
76- aria-labelledby = "pressure-slider"
77- />
78- < p style = { { textAlign : "center" } } > { pressure } </ p >
94+
95+ < div className = "col" >
96+ < div className = "card" >
97+ < div className = "card-body" >
98+ < h5 className = "card-title" > Pressure</ h5 >
99+ < input type = "range" min = "0" max = "100" value = { pressure } className = "slider" id = "pressure-slider" onChange = { handlePressureChange } />
100+ < p className = "card-text text-center" > { pressure } </ p >
101+ </ div >
102+ </ div >
79103 </ div >
80104 </ div >
81- < div style = { { display : "flex" , justifyContent : "center" } } >
82- < button disabled = { isPlaying } onClick = { startSimulation } >
83- Play
84- </ button >
85- < button disabled = { ! isPlaying } onClick = { stopSimulation } >
86- Stop
87- </ button >
105+
106+ < div className = "row" >
107+ < div className = "col text-center" >
108+ < button type = "button" className = "btn btn-success" onClick = { startSimulation } > Play</ button >
109+ </ div >
110+
111+ < div className = "col text-center" >
112+ < button type = "button" className = "btn btn-danger" onClick = { stopSimulationHandler } disabled = { ! intervalId } > Stop</ button >
113+ </ div >
88114 </ div >
89115 </ div >
116+
117+
90118 ) ;
91119}
92120
93- export default TrainSimulator ;
121+ export default TrainSimulator ;
0 commit comments