diff --git a/src/App.js b/src/App.js index 6a24a0d..20415a7 100644 --- a/src/App.js +++ b/src/App.js @@ -1,6 +1,7 @@ import React, { Component } from "react" import logo from "./logo.svg" import "./App.css" +import TrainSimulator from './TrainSimulator'; class LambdaDemo extends Component { constructor(props) { @@ -38,9 +39,10 @@ class App extends Component {
logo

- Edit src/App.js and save to reload. + Hi Neil Edit src/App.js and save to reload.

+
) diff --git a/src/TrainSimulator.js b/src/TrainSimulator.js new file mode 100644 index 0000000..710460f --- /dev/null +++ b/src/TrainSimulator.js @@ -0,0 +1,111 @@ +import React, { useState, useEffect, useRef } from 'react'; + +function TrainSimulator() { + const [speed, setSpeed] = useState(0); + const [throttle, setThrottle] = useState(0); + const [pressure, setPressure] = useState(0); + const [isRunning, setIsRunning] = useState(false); + const intervalRef = useRef(null); + const logRef = useRef(null); + + useEffect(() => { + if (isRunning) { + intervalRef.current = setInterval(() => { + //console.log({ speed, throttle, pressure }); + const logElement = document.createElement('div'); + logElement.innerText = JSON.stringify({ speed, throttle, pressure }); + logRef.current.appendChild(logElement); + if (logRef.current) { + logRef.current.scrollTop = logRef.current.scrollHeight; + } + + }, 1000); + } else { + clearInterval(intervalRef.current); + } + + + + + return () => { + clearInterval(intervalRef.current); + }; + }, [isRunning, speed, throttle, pressure]); + + const startSimulation = () => { + setIsRunning(true); + }; + + const stopSimulation = () => { + setIsRunning(false); + }; + + const handleSpeedChange = (event) => { + setSpeed(event.target.value); + }; + + const handleThrottleChange = (event) => { + setThrottle(event.target.value); + }; + + const handlePressureChange = (event) => { + setPressure(event.target.value); + }; + + + return ( +
+
+ +
+ + + {speed} +
+
+ + + {throttle} +
+
+ + + {pressure} +
+
+ {isRunning ? ( + + ) : ( + + )} +
+
+ ); +} + + +export default TrainSimulator; \ No newline at end of file diff --git a/src/lambda/hello.js b/src/lambda/hello.js index affe921..2c57b6c 100644 --- a/src/lambda/hello.js +++ b/src/lambda/hello.js @@ -1,8 +1,10 @@ // this uses the callback syntax, however, we encourage you to try the async/await syntax shown in async-dadjoke.js export function handler(event, context, callback) { console.log('queryStringParameters', event.queryStringParameters) + let x = (Math.random(1,100) *100); + const myMsg = 'Hello Cris, Your number is ' + x; callback(null, { statusCode: 200, - body: JSON.stringify({ msg: 'Hello, World!' }), + body: JSON.stringify({ msg: myMsg }), }) }