Skip to content

Commit

Permalink
Merge pull request #191 from rit-sse/remove-api-wrapper
Browse files Browse the repository at this point in the history
Remove api wrapper
  • Loading branch information
rtyocum authored Nov 17, 2024
2 parents 88f8a15 + 7541473 commit 12cd17d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 144 deletions.
27 changes: 17 additions & 10 deletions next/app/go/GoLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -42,13 +41,16 @@ const GoLink: React.FC<GoLinkProps> = ({

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) {
Expand All @@ -60,7 +62,10 @@ const GoLink: React.FC<GoLinkProps> = ({

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();
Expand Down Expand Up @@ -283,7 +288,9 @@ const EditAndDelete: React.FC<GoLinkProps> = ({

useEffect(() => {
(async () => {
const data = await fetchAuthLevel();
const data = await fetch("/api/authLevel").then((response) =>
response.json()
);
setIsOfficer(data.isOfficer);
})();
}, []);
Expand Down
24 changes: 14 additions & 10 deletions next/app/go/MakeNewGoLink.tsx
Original file line number Diff line number Diff line change
@@ -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<CreateGoLinkProps> = ({ fetchData }) => {
const { data: session }: any = useSession();
Expand All @@ -26,12 +25,15 @@ export const GoLinkButton: React.FC<CreateGoLinkProps> = ({ 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) {
Expand All @@ -45,11 +47,13 @@ export const GoLinkButton: React.FC<CreateGoLinkProps> = ({ 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) {
Expand Down
10 changes: 8 additions & 2 deletions next/app/go/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
Expand All @@ -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,
Expand Down
121 changes: 0 additions & 121 deletions next/lib/api.ts

This file was deleted.

2 changes: 1 addition & 1 deletion next/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 12cd17d

Please sign in to comment.