Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Jan 3, 2024
1 parent 2b66c7b commit c4177ff
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 30 deletions.
22 changes: 12 additions & 10 deletions ui/src/App.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import React, { useState } from 'react';
import React from 'react';
import './App.css';
import { Container, Form } from 'react-bootstrap';
import { connect } from 'react-redux';

import NotificationsArea from './components/NotificationsArea.js';
import APIAddressField from './components/APIAddressField';
import PourTimeField from './components/PourTimeField';
import SystemControls from './components/SystemControls';
import SystemStatusArea from './components/SystemStatusArea';

function App() {
// TODO: Move this to a redux store
const [pourTime, setPourTime] = useState(1000);
function App({ isConnected }) {
// TODO: Add a fake countdown timer of timeLeft
const systemStatus = null; // TODO: Remove usage of this variable and use redux store instead
return (
<Container className="App">
<h1>Tea System UI</h1>
<SystemStatusArea />

<Form>
<APIAddressField />
{(null === systemStatus) ? null : (
{isConnected ? (
<>
<PourTimeField onChange={setPourTime} min={100} max={systemStatus.waterThreshold} />
<SystemControls pouringTime={pourTime} systemStatus={systemStatus} />
<PourTimeField />
<SystemControls />
</>
)}
) : null}
</Form>

<div className="spacer my-3" />
Expand All @@ -34,4 +32,8 @@ function App() {
);
}

export default App;
export default connect(
state => ({
isConnected: (null !== state.systemStatus),
}), []
)(App);
32 changes: 15 additions & 17 deletions ui/src/components/PourTimeField.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import React, { useState, useEffect } from 'react';
import React from 'react';
import { Form, Row, Col } from 'react-bootstrap';
import { connect } from 'react-redux';
import { updatePouringTime } from '../store/slices/UI';

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]);

export function PourTimeFieldComponent({ onChange, min, max, value }) {
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);
onChange(newTime);
};

return (
Expand All @@ -29,10 +19,18 @@ function PourTimeField({ onChange, min, max }) {
Run Time (ms):
</Form.Label>
<Col sm="10">
<Form.Control type="number" value={pourTime} onChange={handleChange} />
<Form.Control type="number" value={value} onChange={handleChange} />
</Col>
</Form.Group>
);
}

export default PourTimeField;
export default connect(
state => ({
min: 100,
max: state.systemStatus?.waterThreshold,
value: state.UI.pouringTime,
}), {
onChange: updatePouringTime,
}
)(PourTimeFieldComponent);
10 changes: 8 additions & 2 deletions ui/src/components/SystemControls.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';
import { connect } from 'react-redux';
import { Button } from 'react-bootstrap';

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

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

Expand Down Expand Up @@ -39,4 +40,9 @@ function SystemControls({ pouringTime, systemStatus }) {
);
}

export default SystemControls;
export default connect(
state => ({
pouringTime: state.UI.pouringTime,
systemStatus: state.systemStatus,
}), []
)(SystemControlsComponent);
1 change: 1 addition & 0 deletions ui/src/store/slices/SystemStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const systemStatus = {
},
updated: new Date(),
};
// NOTE: SystemStatusSlice can't store unseralizable data, such as Date objects!

// slice for system status
export const SystemStatusSlice = createSlice({
Expand Down
18 changes: 18 additions & 0 deletions ui/src/store/slices/UI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createSlice } from '@reduxjs/toolkit';

const INITIAL_STATE = {
pouringTime: 0,
};
// slice for system status
export const UISlice = createSlice({
name: 'UI',
initialState: INITIAL_STATE,
reducers: {
updatePouringTime(state, action) {
state.pouringTime = action.payload;
}
},
});

export const actions = UISlice.actions;
export const { updatePouringTime } = actions;
3 changes: 2 additions & 1 deletion ui/src/store/slices/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SystemStatusSlice } from "./SystemStatus";
import { UISlice } from "./UI";

const slices = [ SystemStatusSlice ];
const slices = [ SystemStatusSlice, UISlice ];
// export all slices as an object { [sliceName]: slice }
export const ALL_APP_SLICES = slices.reduce((acc, slice) => {
acc[slice.name] = slice;
Expand Down

0 comments on commit c4177ff

Please sign in to comment.