Fix endpoint setting stuff

This commit is contained in:
powermaker450 2024-09-09 12:25:15 -04:00
parent 732dc82823
commit 6d05f9e81f
2 changed files with 32 additions and 33 deletions

View file

@ -1,5 +1,4 @@
import { Alert, Grow, IconButton, Rating, Typography } from "@mui/material"; import { Alert, Grow, Rating, Typography } from "@mui/material";
import SettingsIcon from "@mui/icons-material/Settings";
import "./App.css"; import "./App.css";
import ButtonRow, { ActionProps } from "./components/ButtonRow"; import ButtonRow, { ActionProps } from "./components/ButtonRow";
import ReviewField, { ReviewFieldProps } from "./components/ReviewField"; import ReviewField, { ReviewFieldProps } from "./components/ReviewField";
@ -39,7 +38,7 @@ function App() {
}; };
const showThenHide = async () => { const showThenHide = async () => {
await fetch(endpoint, { await fetch(endpoint ? `${endpoint}/post` : "http://localhost:8080/post", {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
rating: rating, rating: rating,
@ -90,7 +89,7 @@ function App() {
const info = <Alert severity="info">{infoText}</Alert>; const info = <Alert severity="info">{infoText}</Alert>;
const alert = <Alert severity="error">{alertText}</Alert>; const alert = <Alert severity="error">{alertText}</Alert>;
const [endpoint, setEndpoint] = useState(""); const [endpoint, setEndpoint] = useState(JSON.parse(localStorage.getItem("apiEndpoint")!) || "");
return ( return (
<> <>
@ -109,6 +108,7 @@ function App() {
size="large" size="large"
value={rating} value={rating}
onChange={(event, newRating) => { onChange={(event, newRating) => {
event
setNewRating(newRating); setNewRating(newRating);
}} }}
/> />

View file

@ -8,7 +8,7 @@ import {
TextField, TextField,
} from "@mui/material"; } from "@mui/material";
import SettingsIcon from "@mui/icons-material/Settings"; import SettingsIcon from "@mui/icons-material/Settings";
import React, { FormEvent, useState } from "react"; import React, { useState } from "react";
export interface EndpointDialogProps { export interface EndpointDialogProps {
endpoint: [string, React.Dispatch<React.SetStateAction<string>>]; endpoint: [string, React.Dispatch<React.SetStateAction<string>>];
@ -30,9 +30,19 @@ const EndpointDialog = ({ endpoint }: EndpointDialogProps) => {
setErrorText(""); setErrorText("");
}; };
const setEndpoint = (newEndpoint: any) => { const showTheError = () => {
endpoint[1](newEndpoint); setErrorText("Please enter a valid URL.");
}; setError(true);
}
const closeTheError = () => {
setErrorText("");
setError(false);
}
const isValidEndpoint = () => {
return endpoint[0].startsWith("http://") || endpoint[0].startsWith("https://");
}
return ( return (
<> <>
@ -43,29 +53,6 @@ const EndpointDialog = ({ endpoint }: EndpointDialogProps) => {
<Dialog <Dialog
open={open} open={open}
onClose={closeSettings} onClose={closeSettings}
PaperProps={{
component: "form",
onSubmit: (event: FormEvent<HTMLFormElement>) => {
setError(false);
setErrorText("");
event.preventDefault();
const formData = new FormData(event.currentTarget);
const json = Object.fromEntries(formData.entries());
// @ts-ignore
if (
json.endpointBox.includes("http") ||
json.endpointBox.includes("https")
) {
setEndpoint(json.endpointBox);
closeSettings();
} else {
setError(true);
setErrorText("Please enter a valid endpoint.");
}
},
}}
> >
<DialogTitle>Endpoint URL</DialogTitle> <DialogTitle>Endpoint URL</DialogTitle>
<DialogContent> <DialogContent>
@ -73,7 +60,15 @@ const EndpointDialog = ({ endpoint }: EndpointDialogProps) => {
required required
name="endpointBox" name="endpointBox"
type="endpointBox" type="endpointBox"
placeholder={endpoint[0]} value={endpoint[0]}
onChange={({ target }) => {
endpoint[1](target.value);
isValidEndpoint()
? closeTheError()
: showTheError();
}}
placeholder={!endpoint[0] ? "http://localhost:8080" : ""}
error={error} error={error}
helperText={errorText} helperText={errorText}
/> />
@ -81,7 +76,11 @@ const EndpointDialog = ({ endpoint }: EndpointDialogProps) => {
<DialogActions> <DialogActions>
<Button onClick={closeSettings}>Close</Button> <Button onClick={closeSettings}>Close</Button>
<Button type="submit">Set</Button> <Button onClick={() => {
isValidEndpoint()
? closeSettings()! && closeTheError()
: showTheError();
}}>Set</Button>
</DialogActions> </DialogActions>
</Dialog> </Dialog>
</> </>