Skip to content

Commit

Permalink
username/password logic cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jackschedel committed Nov 8, 2024
1 parent a157b51 commit 1fda124
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 48 deletions.
34 changes: 17 additions & 17 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,20 @@ import Settings from "./pages/Settings";
import { Modal, Form, Button } from "react-bootstrap";

export default function App() {
const username = localStorage.getItem("username");
const password = localStorage.getItem("password");

const usernameInput = useRef<HTMLInputElement>(null);
const passwordInput = useRef<HTMLInputElement>(null);

return (
<>
<BrowserRouter>
<Sidebar />
<div className="page-content">
<Routes>
<Route path="/history" element={<ArchivedTelemetry />} />
<Route path="/strategy" element={<Strategy />} />
<Route path="/count" element={<CountLaps />} />
<Route path="/settings" element={<Settings />} />
<Route path="*" element={<LiveTelemetry />} />
</Routes>
</div>
</BrowserRouter>
{localStorage.getItem("passwordNeedsSet") == "true" && (
<Modal show={true}>
<Modal
show={
!localStorage.getItem("username")?.trim() ||
!localStorage.getItem("password")?.trim()
}
centered
>
<Modal.Header>Enter Username/Password</Modal.Header>
<Modal.Body>
<Form
Expand All @@ -44,7 +36,6 @@ export default function App() {
"password",
String(passwordInput?.current?.value),
);
localStorage.setItem("passwordNeedsSet", "false");
}}
>
<Form.Group className="mb-3">
Expand All @@ -59,7 +50,16 @@ export default function App() {
</Form>
</Modal.Body>
</Modal>
)}
<div className="page-content">
<Routes>
<Route path="/history" element={<ArchivedTelemetry />} />
<Route path="/strategy" element={<Strategy />} />
<Route path="/count" element={<CountLaps />} />
<Route path="/settings" element={<Settings />} />
<Route path="*" element={<LiveTelemetry />} />
</Routes>
</div>
</BrowserRouter>
</>
);
}
24 changes: 8 additions & 16 deletions client/src/pages/LiveTelemetry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ function LiveTelemetry() {

React.useEffect(() => {
setInterval(() => {
if (
!localStorage.getItem("username")?.trim() ||
!localStorage.getItem("password")?.trim()
)
return;

telemetry
.getAll()
.then((response) => {
Expand All @@ -43,31 +49,17 @@ function LiveTelemetry() {
//Calculate speed
const rpm = response?.mitsuba?.rx0?.motorRPM ?? 0;
setSpeed(rpm * 60 * telemetry.WHEEL_RADIUS_MI);

// The password may needed to reset but it's actually empty so it didn't
if (localStorage.getItem("passwordNeedsSet") == "true") {
localStorage.setItem("passwordNeedsSet", "false");
window.location.reload();
}
})
.catch((reason) => {
// clear username/password if it changed for some reason
if (
reason.request.status == 403 &&
(!localStorage.getItem("passwordNeedsSet") ||
localStorage.getItem("passwordNeedsSet") == "false")
) {
localStorage.setItem("passwordNeedsSet", "true");
localStorage.setItem("username", "");
localStorage.setItem("password", "");
if (reason.request.status == 403) {
window.location.reload();
}
});
}, 1000);
}, []);

if (!data) {
return <p>Loading..</p>;
return <p>Loading...</p>;
}

// adjusting for tlm we have a 2024 ASC
Expand Down
28 changes: 13 additions & 15 deletions client/src/pages/Strategy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ function Strategy() {
const [isPressed, setIsPressed] = useState(false);

async function fetchData() {
if (
!localStorage.getItem("username")?.trim() ||
!localStorage.getItem("password")?.trim()
)
return;

let result;
let result2;

Expand Down Expand Up @@ -142,6 +148,12 @@ function Strategy() {
end: endTime,
};

if (
!localStorage.getItem("username")?.trim() ||
!localStorage.getItem("password")?.trim()
)
return;

setSearchParams(data);

localStorage.setItem("graph", JSON.stringify(data));
Expand Down Expand Up @@ -414,23 +426,9 @@ function Strategy() {

setEndTime(endAtTime);
setStartTime(startAtTime);

// The password may needed to reset but it's actually empty so it didn't
if (localStorage.getItem("passwordNeedsSet") == "true") {
localStorage.setItem("passwordNeedsSet", "false");
window.location.reload();
}
})
.catch((reason) => {
// clear username/password if it changed for some reason
if (
reason.request.status == 402 &&
(!localStorage.getItem("passwordNeedsSet") ||
localStorage.getItem("passwordNeedsSet") == "false")
) {
localStorage.setItem("passwordNeedsSet", "true");
localStorage.setItem("username", "");
localStorage.setItem("password", "");
if (reason.request.status == 402) {
window.location.reload();
}
});
Expand Down

0 comments on commit 1fda124

Please sign in to comment.