feat: post request to submit and add dev URL

This commit is contained in:
julius 2025-01-25 12:27:05 +01:00
parent deeabf2049
commit 3b9cf54747
Signed by: julius
GPG Key ID: C80A63E6A5FD7092
4 changed files with 35 additions and 12 deletions

1
.env Normal file
View File

@ -0,0 +1 @@
VITE_BASE_URL=

1
.env.development Normal file
View File

@ -0,0 +1 @@
VITE_BASE_URL=http://localhost:8000/

View File

@ -2,6 +2,7 @@ import "./App.css";
import { useState } from "react"; import { useState } from "react";
import { ReactSortable, ReactSortableProps } from "react-sortablejs"; import { ReactSortable, ReactSortableProps } from "react-sortablejs";
const baseUrl = import.meta.env.VITE_BASE_URL as string;
interface Player { interface Player {
id: number; id: number;
@ -42,6 +43,22 @@ function PlayerList(props: PlayerListProps) {
</ReactSortable> </ReactSortable>
); );
} }
async function api(path: string, data: any): Promise<any> {
const request = new Request(`${baseUrl}${path}/`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
let response: Response;
try {
response = await fetch(request);
} catch (e) {
throw new Error(`request failed: ${e}`);
}
return response;
}
function App() { function App() {
const players = loadPlayers(); const players = loadPlayers();
@ -52,17 +69,21 @@ function App() {
const [dialog, setDialog] = useState("dialog"); const [dialog, setDialog] = useState("dialog");
function handleSubmit() { async function handleSubmit() {
const dialog = document.querySelector("dialog"); const dialog = document.querySelector("dialog");
dialog?.showModal();
if (user.length < 1) { if (user.length < 1) {
setDialog("who are you?"); setDialog("who are you?");
} else { } else {
for (const u of user) { setDialog("sending...");
setDialog("sending..."); let _user = user.map(({ name }) => name)[0];
console.log(u.name); let left = playersLeft.map(({ name }) => name);
} let middle = playersMiddle.map(({ name }) => name);
let right = playersRight.map(({ name }) => name);
const data = { user: _user, hate: left, undecided: middle, love: right };
const response = await api("chemistry", data);
response.ok ? setDialog("success!") : setDialog("try sending again");
} }
dialog?.showModal();
} }
return ( return (
@ -88,8 +109,8 @@ function App() {
<div className="box"> <div className="box">
<h2>😬</h2> <h2>😬</h2>
<PlayerList <PlayerList
list={playersRight} list={playersLeft}
setList={setPlayersRight} setList={setPlayersLeft}
group={"shared"} group={"shared"}
className="dragbox" className="dragbox"
orderedList orderedList
@ -107,8 +128,8 @@ function App() {
<div className="box"> <div className="box">
<h2>😍</h2> <h2>😍</h2>
<PlayerList <PlayerList
list={playersLeft} list={playersRight}
setList={setPlayersLeft} setList={setPlayersRight}
group={"shared"} group={"shared"}
className="dragbox" className="dragbox"
orderedList orderedList

View File

@ -17,8 +17,8 @@
/* Linting */ /* Linting */
"strict": true, "strict": true,
"noUnusedLocals": true, "noUnusedLocals": false,
"noUnusedParameters": true, "noUnusedParameters": false,
"noFallthroughCasesInSwitch": true, "noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true "noUncheckedSideEffectImports": true
}, },