diff --git a/next/app/go/GoLink.tsx b/next/app/go/GoLink.tsx index 5f31ca2..7eed101 100644 --- a/next/app/go/GoLink.tsx +++ b/next/app/go/GoLink.tsx @@ -2,7 +2,6 @@ import { GoLinkIcon } from "@/components/common/Icons"; import { GoLinkStar } from "@/components/common/Icons"; import { GoLinkEdit } from "@/components/common/Icons"; import { GoLinkDelete } from "@/components/common/Icons"; -import { fetchAuthLevel, goLinksApi } from "@/lib/api"; import { useSession } from "next-auth/react"; import { useCallback, useEffect, useState } from "react"; @@ -42,13 +41,16 @@ const GoLink: React.FC = ({ const handleEdit = async () => { try { - const response = await goLinksApi.update({ - id: id, - golink: newTitle, - url: newUrl, - description: newDescription, - isPinned: newPinned, - isPublic: !officer, + const response = await fetch("/api/golinks", { + method: "PUT", + body: JSON.stringify({ + id: id, + golink: newTitle, + url: newUrl, + description: newDescription, + isPinned: newPinned, + isPublic: !officer, + }), }); if (response.ok) { @@ -60,7 +62,10 @@ const GoLink: React.FC = ({ const handleDelete = async () => { try { - const response = await goLinksApi.delete(id); + const response = await fetch("/api/golinks", { + method: "DELETE", + body: JSON.stringify({ id }), + }); if (response.ok) { handleCancel(); @@ -283,7 +288,9 @@ const EditAndDelete: React.FC = ({ useEffect(() => { (async () => { - const data = await fetchAuthLevel(); + const data = await fetch("/api/authLevel").then((response) => + response.json() + ); setIsOfficer(data.isOfficer); })(); }, []); diff --git a/next/app/go/MakeNewGoLink.tsx b/next/app/go/MakeNewGoLink.tsx index 4136e94..86c52d7 100644 --- a/next/app/go/MakeNewGoLink.tsx +++ b/next/app/go/MakeNewGoLink.tsx @@ -1,7 +1,6 @@ import { useSession } from "next-auth/react"; import { use, useCallback, useEffect, useState } from "react"; import { CreateGoLinkProps } from "./page"; -import { goLinksApi, fetchAuthLevel } from "@/lib/api"; export const GoLinkButton: React.FC = ({ fetchData }) => { const { data: session }: any = useSession(); @@ -26,12 +25,15 @@ export const GoLinkButton: React.FC = ({ fetchData }) => { const handleCreate = async () => { try { - const response = await goLinksApi.create({ - golink: title, - url: url, - description: description, - isPinned: pinned, - isPublic: !officer, // If it is officer, it is not public + const response = await fetch("/api/golinks", { + method: "POST", + body: JSON.stringify({ + golink: title, + url: url, + description: description, + isPinned: pinned, + isPublic: !officer, // If it is officer, it is not public + }), }); if (response.ok) { @@ -45,11 +47,13 @@ export const GoLinkButton: React.FC = ({ fetchData }) => { const [isOfficer, setIsOfficer] = useState(false); useEffect(() => { - (async () => { - const data = await fetchAuthLevel(); + async () => { + const data = await fetch("/api/authLevel").then((response) => + response.json() + ); console.log(data); setIsOfficer(data.isOfficer); - }) + }; }, []); if (isOfficer) { diff --git a/next/app/go/page.tsx b/next/app/go/page.tsx index d7e8bc2..0dc4a7c 100644 --- a/next/app/go/page.tsx +++ b/next/app/go/page.tsx @@ -3,7 +3,6 @@ import GoLinksContainer from "@/app/go/GoLinksContainer"; import { GoLinkProps } from "./GoLink"; import { useCallback, useEffect, useState } from "react"; -import { goLinksApi } from "@/lib/api"; export interface CreateGoLinkProps { fetchData: () => Promise; @@ -17,7 +16,14 @@ export interface GoLinksContainerProps { const GoLinksPage = () => { const [goLinkData, setGoLinkData]: [any[], any] = useState([]); const fetchData = useCallback(async () => { - const data = await goLinksApi.fetch(); + const data: { + id: number; + golink: string; + url: string; + description: string; + isPinned: boolean; + isPublic: boolean; + }[] = await fetch("/api/golinks/").then((response) => response.json()); setGoLinkData( data.map((item) => ({ id: item.id, diff --git a/next/lib/api.ts b/next/lib/api.ts deleted file mode 100644 index 7bfa6a6..0000000 --- a/next/lib/api.ts +++ /dev/null @@ -1,121 +0,0 @@ -import { DateTime } from "next-auth/providers/kakao"; - -/** - * Remove the "id" key from an object type - */ -export type NoId = { - [P in keyof T as Exclude]: T[P]; -}; - -/** - * Make every key except "id" optional - */ -export type NonExhaustive = { - [P in keyof T]?: T[P]; -} & { id: number }; - -export type GoLink = { - id: number; - golink: string; - url: string; - description: string; - isPinned: boolean; - isPublic: boolean; -}; - -export type Skill = { - id: number; - skill: string; -}; - -export type HourBlock = { - id: number; - weekday: string; - startTime: DateTime; -}; - -export type Mentor = { - id: number; - userId: number; - expirationDate: DateTime; - isActive: boolean; -}; - -export type MentorRead = Mentor & {}; - -export type Schedule = { - id: number; - mentorId: number; - hourBlockId: number; -}; - -export type ScheduleRead = Schedule & { - mentor: Mentor; - hourBlock: HourBlock; -}; - -/** - * A collection of functions to wrap API methods working with a given type - * - * create: create a new item - * - * fetch: fetch all items - * - * update: update an existing item - * - * delete: remove an item - */ -export type ApiWrapper> = { - create: (item: W) => Promise; - fetch: () => Promise; - update: (item: NonExhaustive) => Promise; - delete: (id: number) => Promise; -}; - -function apiWrapperFactory(route: string): ApiWrapper { - return { - create: async (item) => - fetch("/api/" + route, { - method: "POST", - body: JSON.stringify(item), - }), - fetch: async () => - fetch("/api/" + route).then((response) => response.json()), - update: async (item) => - fetch("/api/" + route, { - method: "PUT", - body: JSON.stringify(item), - }), - delete: async (id) => - fetch("/api/" + route, { - method: "DELETE", - body: JSON.stringify({ id }), - }), - }; -} - -export const skillsApi: ApiWrapper = apiWrapperFactory("skills"); -export const hourBlockApi: ApiWrapper = - apiWrapperFactory("hourBlocks"); - -export const goLinksApi: ApiWrapper = { - ...apiWrapperFactory("golinks"), - fetch: async () => - fetch("/api/golinks/public").then((response) => response.json()), -}; - -export const scheduleApi: ApiWrapper = - apiWrapperFactory("schedule"); - -export type AuthLevel = { - isUser: boolean; - isMember: boolean; - isMentor: boolean; - isOfficer: boolean; -}; - -export async function fetchAuthLevel(): Promise { - const response = await fetch("/api/authLevel"); - const data = await response.json(); - return data; -} diff --git a/next/package-lock.json b/next/package-lock.json index 072be22..3429b15 100644 --- a/next/package-lock.json +++ b/next/package-lock.json @@ -14,7 +14,7 @@ "@types/react": "18.2.21", "@types/react-dom": "18.2.7", "autoprefixer": "10.4.15", - "clsx": "^2.1.0", + "clsx": "^2.1.1", "eslint": "8.48.0", "eslint-config-next": "13.4.19", "next": "^14.2.8",