-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(app/automatic/page): ensure
window.location
is defined
* Replaced custom input logic with reusable SetpointInput component * Introduced mount-time check * ensure `window.location` is accessed only on the client
- Loading branch information
1 parent
3676121
commit 8c30717
Showing
2 changed files
with
82 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"use client"; | ||
|
||
import React, { useEffect, useState } from "react"; | ||
import useWebSocket from "../hooks/useWebSocket"; | ||
import { handleSetpointChange } from "./apiCalls"; | ||
import { Switch } from "antd"; | ||
import { InputConfirmed } from "../actuators/ui/inputConfirmed"; | ||
|
||
export interface SetpointInputProps { | ||
websocketHostname: string; | ||
route: string; | ||
} | ||
|
||
const SetpointInput: React.FC<SetpointInputProps> = ({ | ||
websocketHostname, | ||
route, | ||
}) => { | ||
let [setpoint, setSetpoint] = useState<number>(0); | ||
|
||
const { data: websocketState, error } = useWebSocket<number | null>({ | ||
hostname: websocketHostname, | ||
route: route, | ||
}); | ||
|
||
useEffect(() => { | ||
if (typeof websocketState === "number") { | ||
setSetpoint(websocketState); | ||
} | ||
}, [websocketState]); | ||
|
||
const onSetpointChange = (newSetpoint: number) => { | ||
setSetpoint(newSetpoint); | ||
}; | ||
|
||
const onSetpointConfirm = () => { | ||
handleSetpointChange(setpoint, 0); | ||
}; | ||
|
||
const handleToggle = (checked: boolean) => { | ||
if (checked) { | ||
handleSetpointChange(setpoint, 0); | ||
} else { | ||
handleSetpointChange(null, 0); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div> | ||
<Switch | ||
checked={typeof websocketState === "number"} | ||
onClick={(checked) => handleToggle(checked)} | ||
/> | ||
</div> | ||
<div> | ||
<InputConfirmed | ||
min={0.0} | ||
max={25} | ||
precision={2} | ||
value={setpoint} | ||
onValueChange={onSetpointChange} | ||
onConfirm={onSetpointConfirm} | ||
disabled={!(typeof websocketState === "number")} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SetpointInput; |