Skip to content

Commit

Permalink
refresh if you've been logged out
Browse files Browse the repository at this point in the history
  • Loading branch information
harryob committed Apr 26, 2024
1 parent 76e9067 commit 044b0c1
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 41 deletions.
7 changes: 2 additions & 5 deletions src/components/cidLookup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GlobalContext } from "../types/global";
import { ConnectionHistory } from "../types/loginTriplet";
import { TripletList } from "./tripletsList";
import { StickybanMatch } from "./stickybanMatch";
import { callApi } from "../helpers/api";

interface CidLookupProps extends PropsWithChildren {
initialCid?: string;
Expand Down Expand Up @@ -37,11 +38,7 @@ export const CidLookup: React.FC<CidLookupProps> = (props: CidLookupProps) => {
if (override) {
setCid(override);
}
fetch(
`${import.meta.env.VITE_API_PATH}/Connections/Cid?cid=${
override ? override : cid
}`
).then((value) =>
callApi(`/Connections/Cid?cid=${override ? override : cid}`).then((value) =>
value.json().then((json) => {
setLoading(false);
if (json.status == 404) {
Expand Down
7 changes: 2 additions & 5 deletions src/components/ipLookup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GlobalContext } from "../types/global";
import { TripletList } from "./tripletsList";
import { ConnectionHistory } from "../types/loginTriplet";
import { StickybanMatch } from "./stickybanMatch";
import { callApi } from "../helpers/api";

interface IpLookupProps extends PropsWithChildren {
initialIp?: string;
Expand Down Expand Up @@ -38,11 +39,7 @@ export const IpLookup: React.FC<IpLookupProps> = (props: IpLookupProps) => {
if (override) {
setIp(override);
}
fetch(
`${import.meta.env.VITE_API_PATH}/Connections/Ip?ip=${
override ? override : ip
}`
).then((value) =>
callApi(`/Connections/Ip?ip=${override ? override : ip}`).then((value) =>
value.json().then((json) => {
setLoading(false);
if (json.status == 404) {
Expand Down
3 changes: 2 additions & 1 deletion src/components/roundData.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { PropsWithChildren, useEffect, useState } from "react";
import { callApi } from "../helpers/api";

type RoundData = {
mode: string;
Expand Down Expand Up @@ -26,7 +27,7 @@ export const RoundData: React.FC = () => {

useEffect(() => {
if (!roundData) {
fetch(`${import.meta.env.VITE_API_PATH}/Round`).then((value) =>
callApi(`/Round`).then((value) =>
value.json().then((json) => setRoundData(json.data))
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/components/stickybanMatch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Dialog } from "./dialog";
import { StickybanModal } from "./stickybanModal";
import { offset, useFloating } from "@floating-ui/react";
import { GlobalContext } from "../types/global";
import { callApi } from "../helpers/api";

export const StickybanMatch: React.FC<StickybanMatch> = (
props: StickybanMatch
Expand All @@ -22,7 +23,7 @@ export const StickybanMatch: React.FC<StickybanMatch> = (
const getPath = () => {
if (ip) return `/Stickyban/Ip?ip=${ip}`;
if (ckey) return `/Stickyban/Ckey?ckey=${ckey}`;
if (cid) return `/Stickyban/Cid?cid=${cid}`;
return `/Stickyban/Cid?cid=${cid}`;
};

const getText = () => {
Expand All @@ -33,7 +34,7 @@ export const StickybanMatch: React.FC<StickybanMatch> = (

useEffect(() => {
if (!stickyData) {
fetch(`${import.meta.env.VITE_API_PATH}${getPath()}`).then((value) =>
callApi(getPath()).then((value) =>
value.json().then((json) => setStickyData(json))
);
}
Expand Down Expand Up @@ -86,7 +87,7 @@ const Whitelist = (props: { ckey: string }) => {
const global = useContext(GlobalContext);

const doWhitelist = () => {
fetch(`${import.meta.env.VITE_API_PATH}/User//Whitelist`, {
callApi(`/Stickyban/Whitelist?ckey=${ckey}`, {
method: "POST",
}).then((value) => {
value.text().then((value) => {
Expand Down
13 changes: 4 additions & 9 deletions src/components/stickybanModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "../types/stickyban";
import { Link } from "./link";
import { Dialog } from "./dialog";
import { callApi } from "../helpers/api";

type StickybanModalProps = {
stickybans: Stickyban[];
Expand Down Expand Up @@ -67,25 +68,19 @@ const ExpandDetails = (props: { stickyban: Stickyban }) => {
const check = () => {
setOpen(true);

fetch(
`${import.meta.env.VITE_API_PATH}/Stickyban/Match/Cid?id=${stickyban.id}`
).then((value) =>
callApi(`/Stickyban/Match/Cid?id=${stickyban.id}`).then((value) =>
value.json().then((json) => {
setCids(json);
})
);

fetch(
`${import.meta.env.VITE_API_PATH}/Stickyban/Match/Ckey?id=${stickyban.id}`
).then((value) =>
callApi(`/Stickyban/Match/Ckey?id=${stickyban.id}`).then((value) =>
value.json().then((json) => {
setCkeys(json);
})
);

fetch(
`${import.meta.env.VITE_API_PATH}/Stickyban/Match/Ip?id=${stickyban.id}`
).then((value) =>
callApi(`/Stickyban/Match/Ip?id=${stickyban.id}`).then((value) =>
value.json().then((json) => {
setIps(json);
})
Expand Down
3 changes: 2 additions & 1 deletion src/components/stickybans.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { useEffect, useState } from "react";
import { Stickyban } from "../types/stickyban";
import { StickybanModal } from "./stickybanModal";
import { callApi } from "../helpers/api";

export const Stickybans: React.FC<Record<string, never>> = () => {
const [stickybanData, setStickybanData] = useState<Stickyban[] | null>(null);

useEffect(() => {
if (!stickybanData) {
fetch(`${import.meta.env.VITE_API_PATH}/Stickyban`).then((value) =>
callApi(`/Stickyban`).then((value) =>
value.json().then((json) => setStickybanData(json))
);
}
Expand Down
34 changes: 17 additions & 17 deletions src/components/userLookup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TripletList } from "./tripletsList";
import { Link } from "./link";
import { Expand } from "./expand";
import { StickybanMatch } from "./stickybanMatch";
import { callApi } from "../helpers/api";

type ActiveLookupType = {
updateUser: (string?: string) => void;
Expand All @@ -41,19 +42,18 @@ export const LookupMenu: React.FC<LookupMenuProps> = (
const updateUser = useCallback(
(override?: string) => {
setLoading(true);
fetch(`${import.meta.env.VITE_API_PATH}/User?ckey=${override}`).then(
(value) =>
value.json().then((json) => {
setLoading(false);
if (json.status == 404) {
global?.updateAndShowToast("Failed to find user.");
if (close) {
close();
}
} else {
setUserData(json);
callApi(`/User?ckey=${override}`).then((value) =>
value.json().then((json) => {
setLoading(false);
if (json.status == 404) {
global?.updateAndShowToast("Failed to find user.");
if (close) {
close();
}
})
} else {
setUserData(json);
}
})
);
},
[setLoading, setUserData, close, global]
Expand Down Expand Up @@ -375,7 +375,7 @@ const ConnectionTypeDetails = (props: {

useEffect(() => {
if (!connectionData) {
fetch(`${import.meta.env.VITE_API_PATH}${path}${value}`).then((value) =>
callApi(`${path}${value}`).then((value) =>
value.json().then((json) => setConnectionData(json))
);
}
Expand Down Expand Up @@ -472,9 +472,9 @@ const ViewAppliedNotes = (props: { player: Player }) => {
const { player } = props;

const openDialog = () => {
fetch(
`${import.meta.env.VITE_API_PATH}/User/${player.id}/AppliedNotes`
).then((val) => val.json().then((json) => setNotes(json)));
callApi(`/User/${player.id}/AppliedNotes`).then((val) =>
val.json().then((json) => setNotes(json))
);
};

return (
Expand Down Expand Up @@ -507,7 +507,7 @@ const AddNote = (props: { player: Player }) => {
const refetch = useContext(ActiveLookupContext);

const send = () => {
fetch(`${import.meta.env.VITE_API_PATH}/User/${player.id}/Note`, {
callApi(`/User/${player.id}/Note`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Expand Down
7 changes: 7 additions & 0 deletions src/helpers/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const callApi = async (toCall: string, init?: RequestInit | undefined): Promise<Response> => {
const response = await fetch(`${import.meta.env.VITE_API_PATH}${toCall}`, init)
if (response.status == 400) {
location.reload()
}
return response
}

0 comments on commit 044b0c1

Please sign in to comment.