Skip to content

Commit 5ac8553

Browse files
committed
fix: fuel selection
1 parent 59fa731 commit 5ac8553

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

frontend/src/GlobalState.ts

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export interface UserDataState {
2222
TargetE10: number;
2323
}
2424

25+
export enum FuelType {
26+
E5 = 'e5',
27+
E10 = 'e10',
28+
Diesel = 'diesel'
29+
}
30+
2531
const GlobalState = {
2632
jwtToken: '',
2733
userNameState: atom({
@@ -52,6 +58,10 @@ const GlobalState = {
5258
key: 'loginModalState',
5359
default: true
5460
}),
61+
fuelTypeState: atom({
62+
key: 'fuelTypeState',
63+
default: FuelType.E5
64+
}),
5565
webWorkerRefState: atom<null|Worker>({
5666
key: 'webWorkerRefState',
5767
default: null

frontend/src/components/Chart.tsx

+8-12
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ import Radio from '@mui/material/Radio';
1616
import RadioGroup from '@mui/material/RadioGroup';
1717
import FormControlLabel from '@mui/material/FormControlLabel';
1818
import FormControl from '@mui/material/FormControl';
19-
20-
enum FuelType {
21-
E5 = 'e5',
22-
E10 = 'e10',
23-
Diesel = 'diesel'
24-
}
19+
import GlobalState, { FuelType } from '../GlobalState';
20+
import { useRecoilState } from 'recoil';
2521

2622
export interface GsPoint {
2723
timestamp: string;
@@ -41,8 +37,8 @@ export interface ChartProps {
4137

4238
export default function Chart(props: ChartProps) {
4339
//console.log(props.timeSlots);
44-
const [gsValues, setGsValues] = useState([] as GsPoint[]);
45-
const [fuelType, setFuelType] = useState(FuelType.E5);
40+
const [gsValues, setGsValues] = useState([] as GsPoint[]);
41+
const [fuelTypeState, setfuelTypeState] = useRecoilState(GlobalState.fuelTypeState);
4642
const [lineColor, setLineColor] = useState('#8884d8');
4743
const [avgValue, setAvgValue] = useState(0.0);
4844

@@ -52,11 +48,11 @@ export default function Chart(props: ChartProps) {
5248
}, []);
5349

5450
function updateChart() {
55-
if (fuelType === FuelType.E5) {
51+
if (fuelTypeState === FuelType.E5) {
5652
setLineColor('#8884d8')
5753
setAvgValue(props.timeSlots.reduce((acc, value) => value.e5 + acc, 0) / (props.timeSlots.length || 1));
5854
setGsValues(props.timeSlots.map(myValue => ({ timestamp: myValue.x, price: myValue.e5 - avgValue } as GsPoint)))
59-
} else if (fuelType === FuelType.E10) {
55+
} else if (fuelTypeState === FuelType.E10) {
6056
setLineColor('#82ca9d')
6157
setAvgValue(props.timeSlots.reduce((acc, value) => value.e10 + acc, 0) / (props.timeSlots.length || 1));
6258
setGsValues(props.timeSlots.map(myValue => ({ timestamp: myValue.x, price: myValue.e10 - avgValue } as GsPoint)))
@@ -68,7 +64,7 @@ export default function Chart(props: ChartProps) {
6864
}
6965

7066
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
71-
setFuelType(((event.target as HTMLInputElement).value) as FuelType);
67+
setfuelTypeState(((event.target as HTMLInputElement).value) as FuelType);
7268
updateChart();
7369
};
7470
return (<div>
@@ -88,7 +84,7 @@ export default function Chart(props: ChartProps) {
8884
row
8985
aria-labelledby="demo-row-radio-buttons-group-label"
9086
name="row-radio-buttons-group"
91-
value={fuelType}
87+
value={fuelTypeState}
9288
onChange={handleChange}
9389
>
9490
<FormControlLabel value={FuelType.E5} control={<Radio />} label="E5" />

0 commit comments

Comments
 (0)