Skip to content

Commit

Permalink
Merge pull request #29 from KNU-AEYE/27-token-expired-message
Browse files Browse the repository at this point in the history
27-token-expired-message
  • Loading branch information
ilp-sys authored Apr 15, 2024
2 parents 43a2d89 + dd4793d commit 63ea675
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 64 deletions.
25 changes: 7 additions & 18 deletions aeye/app/(nav)/cams/page.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
"use client";

import { useEffect } from "react";
import fetchWithInterception from "@/app/fetchWrapper";

export default function Cams() {
const fetchVids = async () => {
try {
const res = await fetch("https://api.a-eye.live/video", {
method: "GET",
headers: {
Authorization: `Bearer ${localStorage.getItem("accessToken")}`,
},
});
if (res.ok) {
const jsonData = await res.json();
console.log(jsonData.data);
}
} catch (err) {
console.error(err);
}
};

useEffect(() => {
fetchVids();
fetchWithInterception("https://api.a-eye.live/video", {
method: "GET",
})
.then((response) => response.json())
.then((jsonData) => console.log(jsonData.data))
.catch((error) => console.error(error));
}, []);
return <div></div>;
}
52 changes: 23 additions & 29 deletions aeye/app/(nav)/home/SearchResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { searchQueryState } from "@/app/recoil-states";
import { useEffect, useState } from "react";
import Vidpane from "@/app/components/Vidpane";
import { Grid, Paper, Typography } from "@mui/material";
import fetchWithInterception from "@/app/fetchWrapper";

const FETCH_TIMEOUT = 200;

function Vidgroup({ videos }: { videos: VideoDocument[] }) {
return (
Expand All @@ -22,39 +25,30 @@ function Vidgroup({ videos }: { videos: VideoDocument[] }) {
export default function SearchResult() {
const searchQuery = useRecoilValue(searchQueryState);
const [results, setResults] = useState<Vidarr>();
const [searchTimeout, setSearchTimeout] = useState<NodeJS.Timeout | null>(
null
);

const fetchResults = async () => {
const res = await fetch(
`https://api.a-eye.live/video/search?keyword=${searchQuery}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${localStorage.getItem("accessToken")}`,
},
}
);
if (res.ok) {
const jsonData = await res.json();
console.log(jsonData.data);
setResults(jsonData.data);
}
};

useEffect(() => {
if (searchTimeout) {
clearTimeout(searchTimeout);
}
let searchTimeout: NodeJS.Timeout;

setSearchTimeout(setTimeout(fetchResults, 200));

return () => {
if (searchTimeout) {
clearTimeout(searchTimeout);
}
const fetchResults = () => {
fetchWithInterception(
`https://api.a-eye.live/video/search?keyword=${searchQuery}`,
{ method: "GET" }
)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((jsonData) => setResults(jsonData.data))
.catch((error) => console.error(error));
};

if (searchQuery) {
searchTimeout = setTimeout(fetchResults, FETCH_TIMEOUT);
}

return () => clearTimeout(searchTimeout);
}, [searchQuery]);

return (
Expand Down
24 changes: 7 additions & 17 deletions aeye/app/(nav)/my/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React, { useEffect, useState } from "react";
import { styled } from "@mui/material/styles";
import { Box, Grid, Paper, Avatar, Tooltip, Typography } from "@mui/material";
import fetchWithInterception from "@/app/fetchWrapper";

const StyledPaper = styled(Paper)(({ theme }) => ({
backgroundColor: theme.palette.mode === "dark" ? "#1A2027" : "#fff",
Expand All @@ -26,23 +27,12 @@ function ProfileAvatar() {
const [member, setMember] = useState<Member>();

useEffect(() => {
const fetchMember = async () => {
try {
const res = await fetch("https://api.a-eye.live/member/detail", {
method: "GET",
headers: {
Authorization: `Bearer ${localStorage.getItem("accessToken")}`,
},
});
if (res.ok) {
const jsonData = await res.json();
setMember(jsonData.data);
}
} catch (err) {
console.log(err);
}
};
fetchMember();
fetchWithInterception("https://api.a-eye.live/member/detail", {
method: "GET",
})
.then((response) => response.json())
.then((jsonData) => setMember(jsonData.data))
.catch((error) => console.error(error));
}, []);

return (
Expand Down
33 changes: 33 additions & 0 deletions aeye/app/fetchWrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Example usage:
// fetchWithInterception(apiUrl, { method: 'GET' })
// .then(response => response.json())
// .then(data => console.log(data))
// .catch(error => console.error(error));

const fetchWithInterception = async (
url: string,
options?: RequestInit
): Promise<Response> => {
const modifiedOptions = { ...options };
modifiedOptions.headers = {
...modifiedOptions.headers,
Authorization: `Bearer ${localStorage.getItem("accessToken")}`,
};
return fetch(url, modifiedOptions)
.then(async (response: Response) => {
if (!response.ok) {
if (response.status === 401) {
alert("토근이 만료되었습니다. 다시 로그인해주세요.");
window.location.href = "/";
}
throw new Error("Request failed");
}
return response;
})
.catch((error: Error) => {
console.error("Fetch error:", error);
throw error;
});
};

export default fetchWithInterception;

0 comments on commit 63ea675

Please sign in to comment.