Skip to content

Commit

Permalink
SystemControls component
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Jan 1, 2024
1 parent 0a07170 commit 5eeb905
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
32 changes: 6 additions & 26 deletions ui/src/App.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,26 @@
import React, { useState } from 'react';
import './App.css';
import { Container, Form, Button } from 'react-bootstrap';
import { Container, Form } 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';
import PourTimeField from './components/PourTimeField';
import SystemControls from './components/SystemControls';

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

const handleStart = async () => {
try {
await waterPump.start(pourTime);
NotificationsSystem.alert('Water pump started successfully!');
} catch (error) {
NotificationsSystem.alert('Error starting water pump: ' + error.message);
}
};

const handleStop = async () => {
try {
await waterPump.stop();
NotificationsSystem.alert('Water pump stopped successfully!');
} catch (error) {
NotificationsSystem.alert('Error stopping water pump: ' + error.message);
}
};

return (
<Container className="App">
<h1>Tea System UI</h1>
<NotificationsArea />
<Form>
<APIAddressField />
<PourTimeField onChange={setPourTime} min={100} max={10000} />
<Button variant="primary" onClick={handleStart}>Start</Button>{' '}
<Button variant="secondary" onClick={handleStop}>Stop</Button>

<SystemControls pouringTime={pourTime} />
</Form>
<div className="spacer my-3" />
<NotificationsArea />
</Container>
);
}
Expand Down
41 changes: 41 additions & 0 deletions ui/src/components/SystemControls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import { Button } from 'react-bootstrap';

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

function SystemControls({ pouringTime }) {
const waterPump = useWaterPumpAPI().API;
const NotificationsSystem = useNotificationsSystem();

const handleStart = async () => {
try {
await waterPump.start(pouringTime);
NotificationsSystem.alert('Water pump started successfully!');
} catch (error) {
NotificationsSystem.alert('Error starting water pump: ' + error.message);
}
};

const handleStop = async () => {
try {
await waterPump.stop();
NotificationsSystem.alert('Water pump stopped successfully!');
} catch (error) {
NotificationsSystem.alert('Error stopping water pump: ' + error.message);
}
};

return (
<>
<Button variant="primary" onClick={handleStart}>
Start
</Button>{' '}
<Button variant="secondary" onClick={handleStop}>
Stop
</Button>
</>
);
}

export default SystemControls;

0 comments on commit 5eeb905

Please sign in to comment.