Skip to content
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
29 changes: 29 additions & 0 deletions src/app/(protected)/dashboard/[id]/edit/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { apiRequest } from "@/lib/apiRequest";
import { Dashboard } from "@/features/dashboard/types";

export default async function Layout({
children,
params,
}: {
children: React.ReactNode;
params: any;
}) {
const cookieStore = await cookies();
const cookie = cookieStore.get("accessToken")?.value;
const cookieHeader = cookie ? `accessToken=${cookie}` : "";

const { id } = params as { id: string };

const dashboard = await apiRequest<Dashboard>(`/dashboards/${id}`, {
method: "GET",
cookieHeader,
});

if (!dashboard.createdByMe) {
redirect(`/dashboard/${id}`);
}

return <>{children}</>;
}
3 changes: 3 additions & 0 deletions src/app/(protected)/dashboard/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ export default function DashboardIdEdit() {

setDisplayName(dashboardName);
alert("대시보드가 성공적으로 수정되었습니다.");

queryClient.invalidateQueries({ queryKey: ["dashboards"] });
queryClient.invalidateQueries({ queryKey: ["dashboard", dashboardId] });
} catch (e) {
console.error("대시보드 수정 실패", e);
alert("대시보드 수정에 실패했습니다.");
Expand Down
14 changes: 12 additions & 2 deletions src/lib/apiRequest.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
const BASE_URL = "/api/proxy";
const isServer = typeof window === "undefined";

const BASE_URL = isServer ? `${process.env.NEXT_PUBLIC_FRONT_URL}/api/proxy` : "/api/proxy";

interface FetchOptions extends Omit<RequestInit, "body"> {
isFormData?: boolean;
data?: unknown;
cookieHeader?: string;
}

export async function apiRequest<Response>(
endpoint: string,
options: FetchOptions = {},
): Promise<Response> {
const { isFormData, headers, data, ...rest } = options;
const { isFormData, headers, data, cookieHeader, ...rest } = options;

const fetchOptions: RequestInit = {
...rest,
Expand All @@ -19,6 +22,13 @@ export async function apiRequest<Response>(
},
};

if (cookieHeader) {
fetchOptions.headers = {
...fetchOptions.headers,
Cookie: cookieHeader,
};
}

if (!isFormData && data !== undefined) {
fetchOptions.headers = {
...fetchOptions.headers,
Expand Down