Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 157 additions & 14 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,162 @@
import { GameDay, Player } from "./types";
import { GameDay, GameDayPlayer, GameGroup, Player } from "./types";

const API_URL =
window.location.hostname === "localhost"
? "http://localhost:4000"
: "https://plankton-app-xoik3.ondigitalocean.app";

const putPlayer = async (player: Player) => {
const createGroup = async (name: string, players: Player[] = []) => {
try {
const res = await fetch(`${API_URL}/players/`, {
body: JSON.stringify(player),
const res = await fetch(`${API_URL}/groups`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ name, players }),
credentials: "include",
});
if (!res.ok) return null;
return (await res.json()) as { id: string; inviteCode: string };
} catch (error) {
console.error(error);
return null;
}
};

const getGroups = async () => {
try {
const res = await fetch(`${API_URL}/groups`, {
credentials: "include",
});
if (!res.ok) return [];
return (await res.json()) as GameGroup[];
} catch (error) {
console.error(error);
return [];
}
};

const getGroup = async (groupId: string) => {
try {
const res = await fetch(`${API_URL}/groups/${groupId}`, {
credentials: "include",
});
if (!res.ok) return null;
return (await res.json()) as GameGroup;
} catch (error) {
console.error(error);
return null;
}
};

const joinGroup = async (inviteCode: string) => {
try {
const res = await fetch(`${API_URL}/groups/join/${inviteCode}`, {
method: "PUT",
credentials: "include",
});
if (!res.ok) return null;
return (await res.json()) as GameGroup;
} catch (error) {
console.error(error);
return null;
}
};

const deleteGroup = async (groupId: string) => {
// É tratado como delete, mas é apenas um soft
try {
const res = await fetch(`${API_URL}/groups/${groupId}`, {
method: "DELETE",
credentials: "include",
});
return res.ok;
} catch (error) {
console.error(error);
return false;
}
};

const getGroupPlayers = async (groupId: string) => {
try {
const res = await fetch(`${API_URL}/groups/${groupId}/players`, {
credentials: "include",
});
if (!res.ok) return [];
return (await res.json()) as Player[];
} catch (error) {
console.error(error);
return [];
}
};

const addGroupPlayer = async (groupId: string, player: Player) => {
try {
const res = await fetch(`${API_URL}/groups/${groupId}/players`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(player),
credentials: "include",
});
return res.ok;
} catch (error) {
console.error(error);
return false;
}
};

const getGroupGameDays = async (groupId: string) => {
try {
const res = await fetch(`${API_URL}/groups/${groupId}/game-days`, {
credentials: "include",
});
if (!res.ok) return [];
return (await res.json()) as GameDay[];
} catch (error) {
console.error(error);
return [];
}
};

type CreateGroupGameDayParams = {
maxPoints: number;
playersPerTeam: number;
autoSwitchTeamsPoints: number;
isLive: boolean;
playedOn: Date;
players: (Player & Partial<GameDayPlayer>)[];
playingTeams: GameDayPlayer[][];
};

const createGroupGameDay = async (groupId: string, gameDay: CreateGroupGameDayParams) => {
try {
const res = await fetch(`${API_URL}/groups/${groupId}/game-days`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(gameDay),
credentials: "include",
});
if (!res.ok) return null;
return (await res.json()) as { id: string; courtId: string; joinCode: string };
} catch (error) {
console.error(error);
return null;
}
};

const updatePlayers = async (players: Player[]) => {
const putPlayer = async (player: Player) => {
try {
const res = await fetch(`${API_URL}/players/bulk`, {
method: "PUT",
const res = await fetch(`${API_URL}/players/`, {
body: JSON.stringify(player),
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(players),
credentials: "include",
method: "PUT",
});
return res.ok;
} catch (error) {
Expand All @@ -39,16 +165,20 @@ const updatePlayers = async (players: Player[]) => {
}
};

const getPlayers = async (params: URLSearchParams) => {
const updatePlayers = async (players: Player[]) => {
try {
const res = await fetch(`${API_URL}/players?${params.toString()}`, {
const res = await fetch(`${API_URL}/players/bulk`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(players),
credentials: "include",
});
const players = (await res.json()) as Player[];
return players;
return res.ok;
} catch (error) {
console.error(error);
return [];
return null;
}
};

Expand Down Expand Up @@ -204,9 +334,22 @@ async function restartGameDay(gameId: string) {
}

export const api = {
getPlayers,
// Groups
createGroup,
getGroups,
getGroup,
joinGroup,
deleteGroup,
// Group Players
getGroupPlayers,
addGroupPlayer,
// Group Game Days
getGroupGameDays,
createGroupGameDay,
// Players
updatePlayers,
putPlayer,
// Game Days
createGameDay,
getActiveGameDay,
updateGameDay,
Expand Down
13 changes: 13 additions & 0 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import History from "./pages/history/history";
import Layout from "./pages/layout";
import HistoryMatch from "./pages/history/history-match";
import EditGameDay from "./pages/game-day/edit/edit-game-day";
import Groups from "./pages/groups/groups";
import CreateGroup from "./pages/groups/create-group";
import GroupDetail from "./pages/groups/group-detail";
import CreateGroupGameDay from "./pages/groups/create-game-day/create-group-game-day";
import GroupHistory from "./pages/groups/history/group-history";

function App() {
return (
Expand All @@ -20,6 +25,14 @@ function App() {
<Route index element={<History />} />
<Route path=":id" element={<HistoryMatch />} />
</Route>
<Route path="grupos">
<Route index element={<Groups />} />
<Route path="criar" element={<CreateGroup />} />
<Route path=":id" element={<GroupDetail />} />
<Route path=":id/criar-pelada" element={<CreateGroupGameDay />} />
<Route path=":id/historico" element={<GroupHistory />} />
<Route path=":id/historico/:gameDayId" element={<HistoryMatch />} />
</Route>
</Route>
</Routes>
);
Expand Down
Loading