Skip to content

Commit

Permalink
Updated error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jaaimino committed Oct 30, 2024
1 parent 01e5787 commit bf777c1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
68 changes: 50 additions & 18 deletions src/hooks/use-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,58 @@ import React from "react";

export const API_PATH = "https://ktdash.app/api";

export const request = (endpoint, content) => {
return fetch(`${API_PATH}${endpoint}`, {
credentials: "include",
method: "GET",
...content
}).then(response => response.json()).catch(e => console.log(e));
export const request = async (endpoint, content) => {
let response = null;
try {
response = await fetch(`${API_PATH}${endpoint}`, {
credentials: "include",
method: "GET",
...content
});
if (response.status !== 200) {
return response.statusText;
}
response = await response.json();
} catch (error) {
return error;
}
return response;
}
export const requestJson = (endpoint, content) => {
return fetch(`${API_PATH}${endpoint}`, {
credentials: "include",
method: "GET",
...content
}).then(response => response.json()).catch(e => console.log(e));

export const requestJSON = async (endpoint, content) => {
let response = null;
try {
response = await fetch(`${API_PATH}${endpoint}`, {
credentials: "include",
method: "GET",
...content
});
if (response.status !== 200) {
return response.statusText;
}
response = await response.json();
} catch (error) {
return error;
}
return response;
}
export const requestText = (endpoint, content) => {
return fetch(`${API_PATH}${endpoint}`, {
credentials: "include",
method: "GET",
...content
}).then(response => response.text()).catch(e => console.log(e));

export const requestText = async (endpoint, content) => {
let response = null;
try {
response = await fetch(`${API_PATH}${endpoint}`, {
credentials: "include",
method: "GET",
...content
});
if (response.status !== 200) {
return response.statusText;
}
response = await response.text();
} catch (error) {
return error;
}
return response;
}

export function useRequest(endpoint, content, fetchCondition) {
Expand Down
11 changes: 11 additions & 0 deletions src/pages/roster/modals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import useAuth from '../../../hooks/use-auth';
import { useForm } from '@mantine/form';
import { useLocalStorage } from '@mantine/hooks';
import { DEFAULT_SETTINGS } from '../../settings';
import { notifications } from '@mantine/notifications';

export function UpdateRosterPotraitModal(props) {
const { onClose, roster } = props;
Expand All @@ -32,9 +33,19 @@ export function UpdateRosterPotraitModal(props) {
method: "POST",
body: formData
}).then((data) => {
console.log(data);
if (data?.success) {
notifications.show({
title: 'Upload Succeeded',
message: `Successfully uploaded roster portrait.`,
})
modals.close("update-portrait");
onClose(Date.now())
} else {
notifications.show({
title: 'Upload Failed',
message: `${data}`,
})
}
})
};
Expand Down

0 comments on commit bf777c1

Please sign in to comment.