Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 3 additions & 9 deletions frontend/src/app/plans/_components/share-plan-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { v4 as uuidv4 } from "uuid";
import { Icons } from "@/components/icons";
import { Button } from "@/components/ui/button";
import { env } from "@/env.mjs";
import { fetchClient } from "@/lib/fetch";
import type { PlanState } from "@/types";

export function SharePlanButton({ plan }: { plan: PlanState }) {
Expand Down Expand Up @@ -34,16 +35,9 @@ export function SharePlanButton({ plan }: { plan: PlanState }) {
const randomUUID = uuidv4();

try {
const csrfToken = document.cookie
.split("; ")
.find((row) => row.startsWith("XSRF-TOKEN="))
?.split("=")[1];
const response = await fetch(`${env.NEXT_PUBLIC_API_URL}/shared`, {
const response = await fetchClient({
url: "/shared",
method: "POST",
headers: {
"Content-Type": "application/json",
"X-XSRF-TOKEN": csrfToken ?? "",
},
body: JSON.stringify({
plan: JSON.stringify(preparedData),
id: randomUUID,
Expand Down
34 changes: 9 additions & 25 deletions frontend/src/app/plans/edit/[id]/page.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import {
SelectValue,
} from "@/components/ui/select";
import { Skeleton } from "@/components/ui/skeleton";
import { env } from "@/env.mjs";
import { useSession } from "@/hooks/use-session";
import { useShare } from "@/hooks/use-share";
import { fetchClient } from "@/lib/fetch";
import { usePlan } from "@/lib/use-plan";
import { registrationReplacer } from "@/lib/utils";
import { createOnlinePlan } from "@/lib/utils/create-online-plan";
Expand Down Expand Up @@ -79,18 +79,10 @@ export function CreateNewPlanPage({
enabled: faculty !== null && faculty !== "",
queryKey: ["registrations", faculty],
queryFn: async () => {
const csrfToken = document.cookie
.split("; ")
.find((row) => row.startsWith("XSRF-TOKEN="))
?.split("=")[1];
const response = await fetch(
`${env.NEXT_PUBLIC_API_URL}/departments/${encodeURIComponent(faculty ?? "")}/registrations`,
{
headers: {
"X-XSRF-TOKEN": csrfToken ?? "",
},
},
);
const response = await fetchClient({
url: `/departments/${encodeURIComponent(faculty ?? "")}/registrations`,
method: "GET",
});

if (!response.ok) {
throw new Error("Network response was not ok");
Expand Down Expand Up @@ -125,18 +117,10 @@ export function CreateNewPlanPage({
const coursesFunction = useMutation({
mutationKey: ["courses"],
mutationFn: async (registrationId: string) => {
const csrfToken = document.cookie
.split("; ")
.find((row) => row.startsWith("XSRF-TOKEN="))
?.split("=")[1];
const response = await fetch(
`${env.NEXT_PUBLIC_API_URL}/departments/${encodeURIComponent(faculty ?? "")}/registrations/${encodeURIComponent(registrationId)}/courses`,
{
headers: {
"X-XSRF-TOKEN": csrfToken ?? "",
},
},
);
const response = await fetchClient({
url: `/departments/${encodeURIComponent(faculty ?? "")}/registrations/${encodeURIComponent(registrationId)}/courses`,
method: "GET",
});

if (!response.ok) {
throw new Error("Network response was not ok");
Expand Down
34 changes: 34 additions & 0 deletions frontend/src/lib/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use client";

import { env } from "@/env.mjs";

export const fetchClient = async ({
url,
method,
body,
}: {
url: string;
method: RequestInit["method"];
body?: string | null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tutaj mogłeś dać dowolny obiekt i potem w środku robić JSON.stringify, fajnie by działało myślę @qamarq

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wsm niegłupie

}): Promise<Response> => {
const csrfToken = document.cookie
.split("; ")
.find((row) => row.startsWith("XSRF-TOKEN="))
?.split("=")[1];

const fetchOptions: RequestInit = {
method,
headers: {
"Content-Type": "application/json",
"X-XSRF-TOKEN": csrfToken ?? "",
},
credentials: "include",
};

if (method !== "GET" && method !== "HEAD" && body !== undefined) {
fetchOptions.body = body;
}

const response = fetch(`${env.NEXT_PUBLIC_API_URL}${url}`, fetchOptions);
return await response;
};