Skip to content

Commit

Permalink
PourTimeField component
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Jan 1, 2024
1 parent 9ed676e commit 0a07170
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
38 changes: 7 additions & 31 deletions ui/src/App.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
import React, { useState, useEffect } from 'react';
import React, { useState } from 'react';
import './App.css';
import { Container, Row, Col, Form, Button } from 'react-bootstrap';
import { Container, Form, Button } from 'react-bootstrap';

import { useWaterPumpAPI } from './contexts/WaterPumpAPIContext';
import { useNotificationsSystem } from './contexts/NotificationsContext.js';
import NotificationsArea from './components/NotificationsArea.js';
import APIAddressField from './components/APIAddressField';

const STORE_RUNTIME = 'runTime';
import PourTimeField from './components/PourTimeField';

function App() {
const waterPump = useWaterPumpAPI().API;
const [runTime, setRunTime] = useState(1000);
const NotificationsSystem = useNotificationsSystem();

useEffect(() => {
let storedRunTime = localStorage.getItem(STORE_RUNTIME);
if (storedRunTime) {
if (typeof storedRunTime === 'string') {
storedRunTime = parseInt(storedRunTime, 10);
}
setRunTime(storedRunTime);
}
}, []);

const handleRunTimeChange = (event) => {
const runTime = parseInt(event.target.value, 10);
setRunTime(runTime);
localStorage.setItem(STORE_RUNTIME, runTime);
};
const NotificationsSystem = useNotificationsSystem();
const [pourTime, setPourTime] = useState(1000);

const handleStart = async () => {
try {
await waterPump.start(runTime);
await waterPump.start(pourTime);
NotificationsSystem.alert('Water pump started successfully!');
} catch (error) {
NotificationsSystem.alert('Error starting water pump: ' + error.message);
Expand All @@ -54,14 +37,7 @@ function App() {
<NotificationsArea />
<Form>
<APIAddressField />
<Form.Group as={Row} className="mb-3">
<Form.Label column sm="2">
Run Time (ms):
</Form.Label>
<Col sm="10">
<Form.Control type="number" value={runTime} onChange={handleRunTimeChange} />
</Col>
</Form.Group>
<PourTimeField onChange={setPourTime} min={100} max={10000} />
<Button variant="primary" onClick={handleStart}>Start</Button>{' '}
<Button variant="secondary" onClick={handleStop}>Stop</Button>
</Form>
Expand Down
38 changes: 38 additions & 0 deletions ui/src/components/PourTimeField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { useState, useEffect } from 'react';
import { Form, Row, Col } from 'react-bootstrap';

const STORE_POURRTIME = 'pourTime';

function PourTimeField({ onChange, min, max }) {
const [pourTime, setPourTime] = useState(1000);

useEffect(() => {
const time = localStorage.getItem(STORE_POURRTIME);
if (time) setPourTime(parseInt(time, 10));
}, []); // on mount
// call onChange when pourTime changes
useEffect(() => onChange(pourTime), [pourTime, onChange]);

const handleChange = (event) => {
let newTime = parseInt(event.target.value, 10);
if (isNaN(newTime)) return;
if (newTime < min) newTime = min;
if (max < newTime) newTime = max;

setPourTime(newTime);
localStorage.setItem(STORE_POURRTIME, newTime);
};

return (
<Form.Group as={Row} className="mb-3">
<Form.Label column sm="2">
Run Time (ms):
</Form.Label>
<Col sm="10">
<Form.Control type="number" value={pourTime} onChange={handleChange} />
</Col>
</Form.Group>
);
}

export default PourTimeField;

0 comments on commit 0a07170

Please sign in to comment.