Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove api wrapper #191

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.