Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbjoralt committed Oct 13, 2023
1 parent 371b72f commit abb0ff6
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 49 deletions.
2 changes: 1 addition & 1 deletion backend/Api/Departments/DeparmentController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Database.DatabaseContext;
using Microsoft.AspNetCore.Mvc;

[Route("/departments")]
[Route("/v0/departments")]
[ApiController]
public class DepartmentController : ControllerBase {

Expand Down
5 changes: 3 additions & 2 deletions frontend/src/app/bemanning/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import FilteredConsultantsList from "@/components/FilteredConsultantsList";
import { getConsultants } from "@/data/getConsultants";
import { fetchWithToken } from "@/data/fetchWithToken";
import { Variant } from "@/types";

export default async function Bemanning() {
const consultants = await getConsultants();
const consultants = await fetchWithToken<Variant[]>("variants") ?? [];

return (
<div>
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/components/DepartmentFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import FilterButton from "./FilterButton";
import getDepartment from "@/data/getDepartment";
import { fetchWithToken } from "@/data/fetchWithToken";
import { Department } from "@/types";

export default async function DepartmentFilter() {
const departments = await getDepartment();
const departments = await fetchWithToken<Department[]>("departments")?? [];

if (departments.length > 0) {
return (
Expand Down
17 changes: 0 additions & 17 deletions frontend/src/components/SignInField.tsx

This file was deleted.

31 changes: 16 additions & 15 deletions frontend/src/data/fetchWithToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import {
getCustomServerSession,
} from "@/app/api/auth/[...nextauth]/route";

export async function fetchWithToken(path: string) {
export async function fetchWithToken<T>(path: string): Promise<T | undefined> {
if (process.env.NEXT_PUBLIC_NO_AUTH) {
return mockedCall(path);
return mockedCall<T>(path);
}

const session = await getCustomServerSession(authOptions);

if(!session || !session.access_token) return;


const apiBackendUrl =
process.env.NEXT_PUBLIC_VIBES_BACKEND_URL ?? "http://localhost:7172/v0";

Expand All @@ -26,19 +29,17 @@ export async function fetchWithToken(path: string) {
headers: headers,
};

try {
const response = await fetch(`${apiBackendUrl}/${path}`, options);
return await response.json();
} catch (error) {
console.error(error);
}
const response = await fetch(`${apiBackendUrl}/${path}`, options);
return await response.json() as T;
}

function mockedCall(path: string) {
if (path.includes("variants")) {
return MockConsultants;
}
if (path.includes("departments")) {
return MockDepartments;
}
function mockedCall<T>(path: string): Promise<T> {
return new Promise((resolve) => {
if (path.includes("variants")) {
resolve(MockConsultants as T);
}
if (path.includes("departments")) {
resolve(MockDepartments as T);
}
})
}
6 changes: 0 additions & 6 deletions frontend/src/data/getConsultants.ts

This file was deleted.

6 changes: 0 additions & 6 deletions frontend/src/data/getDepartment.ts

This file was deleted.

9 changes: 9 additions & 0 deletions frontend/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { withAuth } from "next-auth/middleware";

export default withAuth({
callbacks: {
authorized: ({ req, token }) => {
return !!token && !req.nextUrl.basePath.startsWith("/login");
},
},
});

0 comments on commit abb0ff6

Please sign in to comment.