Skip to content

Commit

Permalink
refactor: remove not used parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jordybsk committed Jul 4, 2024
1 parent 4742f8f commit 14bb4f3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion nextjs-interface/src/app/ui/dashboard/RenameElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function RenameElement({ id }: { id: string }) {
}
};
return (
<div className="flex gap-2 cursor-pointer">
<div className="flex cursor-pointer gap-2">
<Popover>
<PopoverTrigger asChild>
<EllipsisVertical />
Expand Down
20 changes: 11 additions & 9 deletions nextjs-interface/src/lib/data.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useEffect, useState } from "react";
import { getToken, data, avgData, esp } from "@/lib/context";

export const fetchWithAuth = async (url: string,token: string | null | undefined) => {
export const fetchWithAuth = async (url: string) => {
try {
const response = await fetch(url, {headers: {Authorization: `Bearer ${getToken()}`}});
const response = await fetch(url, {
headers: { Authorization: `Bearer ${getToken()}` },
});
if (!response.ok) {
if (response.status === 401) {
window.location.href = '/login';
window.location.href = "/login";
}
throw new Error(`HTTP error! Status: ${response.status}`);
}
Expand All @@ -27,7 +29,7 @@ export const useFetchData = (

useEffect(() => {
const url = `/postgrest/rpc/avg_date?delta=${precision}&ip=eq.${ip}&and=(date.gte.${from},date.lt.${to})`;
fetchWithAuth(url,getToken())
fetchWithAuth(url);
fetch(url, { headers: { Authorization: `Bearer ${getToken()}` } })
.then((response) => response.json())
.then((apiData: avgData[]) => {
Expand All @@ -46,7 +48,7 @@ export function useLastData(type: string, ip: string) {

useEffect(() => {
const url = `/postgrest/data_view?limit=1&order=timestamp.desc&ip=eq.${ip}`;
fetchWithAuth(url,getToken())
fetchWithAuth(url);
fetch(url, { headers: { Authorization: `Bearer ${getToken()}` } })
.then((response) => response.json())
.then((apiData: data[]) => {
Expand All @@ -69,7 +71,7 @@ export const useAllEsp = () => {

useEffect(() => {
const url = `/postgrest/esp`;
fetchWithAuth(url,getToken())
fetchWithAuth(url);
fetch(url, { headers: { Authorization: `Bearer ${getToken()}` } })
.then((response) => response.json())
.then((apiEsp: esp[]) => {
Expand All @@ -88,7 +90,7 @@ export default function useFindIpById(id: string) {

useEffect(() => {
const url = `/postgrest/esp?select=ip&id=eq.${id}`;
fetchWithAuth(url,getToken())
fetchWithAuth(url);
fetch(url, { headers: { Authorization: `Bearer ${getToken()}` } })
.then((response) => response.json())
.then((apiIp: esp[]) => {
Expand All @@ -107,7 +109,7 @@ export function useFindNameById(id: string) {

useEffect(() => {
const url = `/postgrest/esp?select=name&id=eq.${id}`;
fetchWithAuth(url,getToken())
fetchWithAuth(url);
fetch(url, { headers: { Authorization: `Bearer ${getToken()}` } })
.then((response) => response.json())
.then((apiIp: esp[]) => {
Expand All @@ -126,7 +128,7 @@ export function GetEspPosition(id: string) {

useEffect(() => {
const url = `/postgrest/esp?select=x,y,name,ip&id=eq.${id}`;
fetchWithAuth(url,getToken())
fetchWithAuth(url);
fetch(url, { headers: { Authorization: `Bearer ${getToken()}` } })
.then((response) => response.json())
.then((apiPosition: esp[]) => {
Expand Down
14 changes: 8 additions & 6 deletions nextjs-interface/src/middleware.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import {fetchWithAuth} from "@/lib/data";

export async function middleware(request: NextRequest) {


const token = request.cookies.get("token");
const {pathname} = request.nextUrl;
const { pathname } = request.nextUrl;

if (pathname === "/" && !token) {
const loginUrl = new URL("/login", request.nextUrl.origin).toString();
return NextResponse.redirect(loginUrl);

}

if ((pathname === "/dashboard" || pathname === "/dashboard/plan") && !token) {
Expand All @@ -28,5 +24,11 @@ export async function middleware(request: NextRequest) {
}

export const config = {
matcher: ["/", "/dashboard", "/login", "/dashboard/plan", "/dashboard/esp/:path*"],
matcher: [
"/",
"/dashboard",
"/login",
"/dashboard/plan",
"/dashboard/esp/:path*",
],
};

0 comments on commit 14bb4f3

Please sign in to comment.