Updated code to use new Api provider

This commit is contained in:
2026-09-29 00:00:52 +02:00
parent b1d57c645b
commit 81f1bb4bda
4 changed files with 40 additions and 151 deletions

View File

@@ -4,6 +4,7 @@ import { Agent } from "./agent";
import { SkinTile } from "./skin_tile"; import { SkinTile } from "./skin_tile";
import { useNotification } from "~/hooks/notification"; import { useNotification } from "~/hooks/notification";
import { WeaponInfo, AgentInfo } from "~/types/skin"; import { WeaponInfo, AgentInfo } from "~/types/skin";
import { useApi } from "~/hooks/api";
export function Inventory({ side }: { side: "T" | "CT" }) { export function Inventory({ side }: { side: "T" | "CT" }) {
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
@@ -12,6 +13,7 @@ export function Inventory({ side }: { side: "T" | "CT" }) {
const [knife, setKnife] = useState<WeaponInfo>(); const [knife, setKnife] = useState<WeaponInfo>();
const [weapons, setWeapons] = useState<Array<WeaponInfo>>([]); const [weapons, setWeapons] = useState<Array<WeaponInfo>>([]);
const { notify } = useNotification(); const { notify } = useNotification();
const { get } = useApi();
const setWeapon = (index:number, newWeapon:WeaponInfo) => { const setWeapon = (index:number, newWeapon:WeaponInfo) => {
setWeapons(weapons.map((v,i) => (i===index ? newWeapon: v))) setWeapons(weapons.map((v,i) => (i===index ? newWeapon: v)))
@@ -19,81 +21,21 @@ export function Inventory({ side }: { side: "T" | "CT" }) {
useEffect(() => { useEffect(() => {
const fetch_weapons = async () => { const fetch_weapons = async () => {
let response = await fetch( let [success, data] = await get(`skin?team=${side}&category=Knives`)
`${import.meta.env.VITE_BASE_API_URL}/skin?team=${side}&category=Knives`,
{ credentials: "include" },
)
.then((r) => r.json())
.catch((e) => {
notify(
{
type: "danger",
loading: false,
title: "An error occured.",
message: `An error occured: ${e}`,
},
10000,
);
});
setKnife(new WeaponInfo(response[0])); setKnife(new WeaponInfo(data[0]));
response = await fetch( [success, data] = await get(`skin?team=${side}&category=Gloves`)
`${import.meta.env.VITE_BASE_API_URL}/skin?team=${side}&category=Gloves`,
{ credentials: "include" },
)
.then((r) => r.json())
.catch((e) => {
notify(
{
type: "danger",
loading: false,
title: "An error occured.",
message: `An error occured: ${e}`,
},
10000,
);
});
setGloves(new WeaponInfo(response[0])); setGloves(new WeaponInfo(data[0]));
response = await fetch( [success, data] = await get(`skin?team=${side}`);
`${import.meta.env.VITE_BASE_API_URL}/skin?team=${side}`,
{ credentials: "include" },
)
.then((r) => r.json())
.catch((e) => {
notify(
{
type: "danger",
loading: false,
title: "An error occured.",
message: `An error occured: ${e}`,
},
10000,
);
});
setWeapons(response.map((item: any) => (new WeaponInfo(item)))); setWeapons(data.map((item: any) => (new WeaponInfo(item))));
response = await fetch( [success, data] = await get(`agent?team=${side}`);
`${import.meta.env.VITE_BASE_API_URL}/agent?team=${side}`,
{ credentials: "include" },
)
.then((r) => r.json())
.catch((e) => {
notify(
{
type: "danger",
loading: false,
title: "An error occured.",
message: `An error occured: ${e}`,
},
10000,
);
});
setAgent(new AgentInfo(response)); setAgent(new AgentInfo(data));
setLoading(false); setLoading(false);
}; };

View File

@@ -13,6 +13,7 @@ import {
useOverlayState, useOverlayState,
} from "@heroui/react"; } from "@heroui/react";
import { useEffect, useState, type Dispatch, type SetStateAction } from "react"; import { useEffect, useState, type Dispatch, type SetStateAction } from "react";
import { useApi } from "~/hooks/api";
import { useAuth } from "~/hooks/auth"; import { useAuth } from "~/hooks/auth";
import { useNotification } from "~/hooks/notification"; import { useNotification } from "~/hooks/notification";
@@ -28,6 +29,7 @@ export function Settings({
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [canClose, setCanClose] = useState(true); const [canClose, setCanClose] = useState(true);
const deleteState = useOverlayState(); const deleteState = useOverlayState();
const { get, post } = useApi();
useEffect(() => { useEffect(() => {
if (!deleteState.isOpen) { if (!deleteState.isOpen) {
@@ -61,29 +63,7 @@ export function Settings({
const save = async () => { const save = async () => {
setLoading(true); setLoading(true);
let ok = await fetch( let ok = await post("update", data);
`${import.meta.env.VITE_BASE_API_URL}/update`,
{
body: JSON.stringify(data),
credentials: "include",
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
)
.then((r) => r.ok)
.catch((e) => {
notify(
{
type: "danger",
loading: false,
title: "An error occured.",
message: `An error occured: ${e}`,
},
10000,
);
});
if (ok) { if (ok) {
await update(); await update();
@@ -97,21 +77,7 @@ export function Settings({
const loadFromSteam = async () => { const loadFromSteam = async () => {
setLoading(true); setLoading(true);
let ok = await fetch(`${import.meta.env.VITE_BASE_API_URL}/reload`, { let [ok, _] = await get("reload");
credentials: "include",
})
.then((r) => r.ok)
.catch((e) => {
notify(
{
type: "danger",
loading: false,
title: "An error occured.",
message: `An error occured: ${e}`,
},
10000,
);
});
if (ok) { if (ok) {
await update(); await update();
@@ -123,21 +89,7 @@ export function Settings({
const deleteAccount = async () => { const deleteAccount = async () => {
setLoading(true); setLoading(true);
let ok = await fetch(`${import.meta.env.VITE_BASE_API_URL}/delete`, { let [ok, _] = await get("delete");
credentials: "include",
})
.then((r) => r.ok)
.catch((e) => {
notify(
{
type: "danger",
loading: false,
title: "An error occured.",
message: `An error occured: ${e}`,
},
10000,
);
});
if (ok) { if (ok) {
await logout(); await logout();

View File

@@ -1,5 +1,6 @@
import { Button, Modal, Typography } from "@heroui/react"; import { Button, Modal, Typography } from "@heroui/react";
import { useEffect, useState, type Dispatch, type SetStateAction } from "react"; import { useEffect, useState, type Dispatch, type SetStateAction } from "react";
import { useApi } from "~/hooks/api";
import { useNotification } from "~/hooks/notification"; import { useNotification } from "~/hooks/notification";
import { AgentInfo, WeaponInfo } from "~/types/skin"; import { AgentInfo, WeaponInfo } from "~/types/skin";
@@ -27,6 +28,7 @@ export function SkinSelector({
}) { }) {
const [skins, setSkins] = useState<Array<Info>>([]); const [skins, setSkins] = useState<Array<Info>>([]);
const { notify } = useNotification(); const { notify } = useNotification();
const { get } = useApi();
const setPaint = (v: Info) => { const setPaint = (v: Info) => {
if (skin instanceof WeaponInfo) { if (skin instanceof WeaponInfo) {
@@ -48,22 +50,7 @@ export function SkinSelector({
useEffect(() => { useEffect(() => {
const fetchSkins = async () => { const fetchSkins = async () => {
let response = await fetch( let [_, response] = await get(`index?weapon=${skin.weapon_id}&team=${side}${skin instanceof AgentInfo ? "&agent=true" : ""}&gloves=${skin.name.includes("gloves") || skin.name.includes("wraps")}`)
`${import.meta.env.VITE_BASE_API_URL}/index?weapon=${skin.weapon_id}&team=${side}${skin instanceof AgentInfo ? "&agent=true" : ""}&gloves=${skin.name.includes("gloves") || skin.name.includes("wraps")}`,
{ credentials: "include" },
)
.then((r) => r.json())
.catch((e) => {
notify(
{
type: "danger",
loading: false,
title: "An error occured.",
message: `An error occured: ${e}`,
},
10000,
);
});
setSkins(response); setSkins(response);
}; };

View File

@@ -8,21 +8,23 @@ import { useNotification } from "./notification";
import type { NotificationType } from "~/components/notification"; import type { NotificationType } from "~/components/notification";
type ApiContextType = { type ApiContextType = {
get: (arg0: string) => Promise<any>; get: (arg0: string) => Promise<[boolean, any]>;
post: (arg0: string, arg1: any) => Promise<void>; post: (arg0: string, arg1: any) => Promise<boolean>;
}; };
const apiContext = createContext<ApiContextType>({ const apiContext = createContext<ApiContextType>({
get: async (_: string) => {}, get: async (_: string) => {
post: async (_a: string, _b: any) => {}, return [true, undefined];
},
post: async (_a: string, _b: any) => {
return true;
},
}); });
export function ApiProvider({ children }: PropsWithChildren) { export function ApiProvider({ children }: PropsWithChildren) {
const { notify } = useNotification(); const { notify } = useNotification();
const get = async (endpoint: string) => { const get = async (endpoint: string): Promise<[boolean, any]> => {
let data = undefined;
let response = await fetch( let response = await fetch(
`${import.meta.env.VITE_BASE_API_URL}/${endpoint}`, `${import.meta.env.VITE_BASE_API_URL}/${endpoint}`,
{ credentials: "include" }, { credentials: "include" },
@@ -30,14 +32,17 @@ export function ApiProvider({ children }: PropsWithChildren) {
if (!response) { if (!response) {
notifyError(notify, 1, "No data in response from server."); notifyError(notify, 1, "No data in response from server.");
} else if (!response.ok) { return [false, undefined];
notifyError(notify, response.status, await response.text());
} else {
const text = await response.text();
data = text ? JSON.parse(text): null;
} }
return data; if (!response.ok) {
notifyError(notify, response.status, await response.text());
return [false, undefined];
}
const text = await response.text();
const data = text ? JSON.parse(text) : undefined;
return [true, data];
}; };
const post = async (endpoint: string, data: any) => { const post = async (endpoint: string, data: any) => {
@@ -55,9 +60,12 @@ export function ApiProvider({ children }: PropsWithChildren) {
if (!response) { if (!response) {
notifyError(notify, 1, "No data in response from server."); notifyError(notify, 1, "No data in response from server.");
return false;
} else if (!response.ok) { } else if (!response.ok) {
notifyError(notify, response.status, await response.text()); notifyError(notify, response.status, await response.text());
return false;
} }
return true;
}; };
return ( return (