mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-04-20 03:50:20 +00:00
refactor: implement session checking utility, enhance device queries with session validation, and improve UI interactions for device management
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 6m36s
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 6m36s
This commit is contained in:
parent
9e2a2f430e
commit
daab793592
@ -123,11 +123,7 @@ export async function blockDevice({
|
|||||||
if (!macAddress) {
|
if (!macAddress) {
|
||||||
throw new Error("macAddress is a required parameter");
|
throw new Error("macAddress is a required parameter");
|
||||||
}
|
}
|
||||||
// const device = await prisma.device.findFirst({
|
|
||||||
// where: {
|
|
||||||
// mac: macAddress,
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
try {
|
try {
|
||||||
const baseUrl: string = process.env.OMADA_BASE_URL || "";
|
const baseUrl: string = process.env.OMADA_BASE_URL || "";
|
||||||
const url: string = `${baseUrl}/api/v2/sites/${process.env.OMADA_SITE_ID}/cmd/clients/${formatMacAddress(macAddress)}/${type}`;
|
const url: string = `${baseUrl}/api/v2/sites/${process.env.OMADA_SITE_ID}/cmd/clients/${formatMacAddress(macAddress)}/${type}`;
|
||||||
|
@ -1,3 +1,8 @@
|
|||||||
|
import BlockDeviceDialog from "@/components/block-device-dialog";
|
||||||
|
import Search from "@/components/search";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
import { getDevice } from "@/queries/devices";
|
||||||
|
import { tryCatch } from "@/utils/tryCatch";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
export default async function DeviceDetails({
|
export default async function DeviceDetails({
|
||||||
@ -6,20 +11,46 @@ export default async function DeviceDetails({
|
|||||||
params: Promise<{ deviceId: string }>;
|
params: Promise<{ deviceId: string }>;
|
||||||
}) {
|
}) {
|
||||||
const deviceId = (await params)?.deviceId;
|
const deviceId = (await params)?.deviceId;
|
||||||
|
const [error, device] = await tryCatch(getDevice({ deviceId: deviceId }));
|
||||||
|
if (error) return <div>{error.message}</div>;
|
||||||
|
if (!device) return null;
|
||||||
|
|
||||||
return null;
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div className="flex flex-col justify-between items-start text-gray-500 title-bg py-4 px-2 mb-4">
|
<div className="flex items-center justify-between title-bg title-bg ring-2 ring-sarLinkOrange/50 rounded-lg p-2">
|
||||||
<h3 className="text-2xl font-bold">{device?.name}</h3>
|
<div className="flex flex-col justify-between items-start">
|
||||||
<span>{device?.mac}</span>
|
<h3 className="text-2xl text-sarLinkOrange font-bold">
|
||||||
|
{device?.name}
|
||||||
|
</h3>
|
||||||
|
<Badge variant={"secondary"}>{device?.mac}</Badge>
|
||||||
|
<p className="text-muted-foreground text-sm mt-2">
|
||||||
|
Device active until{" "}
|
||||||
|
{new Date(device?.expiry_date || "").toLocaleDateString("en-US", {
|
||||||
|
month: "short",
|
||||||
|
day: "2-digit",
|
||||||
|
year: "numeric",
|
||||||
|
})}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-2 flex-col">
|
||||||
|
{device?.expiry_date && new Date() < new Date(device.expiry_date) && (
|
||||||
|
<p className="text-base font-semibold font-mono w-full text-center px-2 p-1 rounded-md bg-green-500/10 text-green-900 dark:text-green-400">
|
||||||
|
ACTIVE
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
<BlockDeviceDialog
|
||||||
|
device={device}
|
||||||
|
type={device.blocked ? "unblock" : "block"}
|
||||||
|
/>
|
||||||
|
<pre>{JSON.stringify(device.blocked, null, 2)}</pre>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
id="user-filters"
|
id="user-filters"
|
||||||
className=" pb-4 gap-4 flex sm:flex-row flex-col items-start justify-start"
|
className=" py-4 gap-4 flex sm:flex-row flex-col items-start justify-start"
|
||||||
>
|
>
|
||||||
{/* <Search /> */}
|
<Search />
|
||||||
{/* <Filter
|
{/* <Filter
|
||||||
options={sortfilterOptions}
|
options={sortfilterOptions}
|
||||||
defaultOption="asc"
|
defaultOption="asc"
|
||||||
|
@ -1,25 +1,28 @@
|
|||||||
'use client'
|
"use client";
|
||||||
import { deviceCartAtom } from '@/lib/atoms'
|
import { deviceCartAtom } from "@/lib/atoms";
|
||||||
import type { Device } from '@prisma/client'
|
import type { Device } from "@/lib/backend-types";
|
||||||
import { useAtomValue, useSetAtom } from 'jotai'
|
import { useAtomValue, useSetAtom } from "jotai";
|
||||||
import React from 'react'
|
import React from "react";
|
||||||
|
|
||||||
export default function AddDevicesToCartButton({ device }: { device: Device }) {
|
export default function AddDevicesToCartButton({ device }: { device: Device }) {
|
||||||
const setDeviceCart = useSetAtom(deviceCartAtom)
|
const setDeviceCart = useSetAtom(deviceCartAtom);
|
||||||
const devices = useAtomValue(deviceCartAtom)
|
const devices = useAtomValue(deviceCartAtom);
|
||||||
|
|
||||||
const isChecked = devices.some((d) => d.id === device.id);
|
const isChecked = devices.some((d) => d.id === device.id);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
className='peer accent-[#f49d1b] size-4'
|
disabled={device.blocked || device.is_active}
|
||||||
|
className="peer accent-[#f49d1b] size-4"
|
||||||
checked={isChecked}
|
checked={isChecked}
|
||||||
onChange={() => setDeviceCart((prev) =>
|
onChange={() =>
|
||||||
|
setDeviceCart((prev) =>
|
||||||
isChecked
|
isChecked
|
||||||
? prev.filter((d) => d.id !== device.id)
|
? prev.filter((d) => d.id !== device.id)
|
||||||
: [...prev, device]
|
: [...prev, device],
|
||||||
)}
|
|
||||||
/>
|
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ import {
|
|||||||
DialogTrigger,
|
DialogTrigger,
|
||||||
} from "@/components/ui/dialog";
|
} from "@/components/ui/dialog";
|
||||||
import { Label } from "@/components/ui/label";
|
import { Label } from "@/components/ui/label";
|
||||||
|
import type { Device } from "@/lib/backend-types";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import type { Device } from "@prisma/client";
|
|
||||||
import { OctagonX } from "lucide-react";
|
import { OctagonX } from "lucide-react";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { type SubmitHandler, useForm } from "react-hook-form";
|
import { type SubmitHandler, useForm } from "react-hook-form";
|
||||||
@ -94,7 +94,6 @@ export default function BlockDeviceDialog({
|
|||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|
||||||
{disabled ? <TextShimmer>Unblocking</TextShimmer> : "Unblock"}
|
{disabled ? <TextShimmer>Unblocking</TextShimmer> : "Unblock"}
|
||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : (
|
||||||
|
@ -19,9 +19,13 @@ export default function ClickableRow({
|
|||||||
<TableRow
|
<TableRow
|
||||||
key={device.id}
|
key={device.id}
|
||||||
className={cn(
|
className={cn(
|
||||||
parentalControl === false && "cursor-pointer hover:bg-muted",
|
(parentalControl === false && device.blocked) || device.is_active
|
||||||
|
? "cursor-not-allowed title-bg"
|
||||||
|
: "cursor-pointer hover:bg-muted",
|
||||||
)}
|
)}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
if (device.blocked) return;
|
||||||
|
if (device.is_active === true) return;
|
||||||
if (parentalControl === true) return;
|
if (parentalControl === true) return;
|
||||||
setDeviceCart((prev) =>
|
setDeviceCart((prev) =>
|
||||||
devices.some((d) => d.id === device.id)
|
devices.some((d) => d.id === device.id)
|
||||||
@ -33,7 +37,10 @@ export default function ClickableRow({
|
|||||||
<TableCell>
|
<TableCell>
|
||||||
<div className="flex flex-col items-start">
|
<div className="flex flex-col items-start">
|
||||||
<Link
|
<Link
|
||||||
className="font-medium hover:underline"
|
className={cn(
|
||||||
|
"hover:underline font-semibold",
|
||||||
|
device.is_active ? "text-green-600" : "",
|
||||||
|
)}
|
||||||
href={`/devices/${device.id}`}
|
href={`/devices/${device.id}`}
|
||||||
onClick={(e) => e.stopPropagation()}
|
onClick={(e) => e.stopPropagation()}
|
||||||
>
|
>
|
||||||
@ -49,7 +56,7 @@ export default function ClickableRow({
|
|||||||
})}
|
})}
|
||||||
</span>
|
</span>
|
||||||
) : (
|
) : (
|
||||||
<p>Inactive</p>
|
<p className="text-muted-foreground">Device Inactive</p>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{device.blocked_by === "ADMIN" && device.blocked && (
|
{device.blocked_by === "ADMIN" && device.blocked && (
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import { authOptions } from "@/app/auth";
|
import { authOptions } from "@/app/auth";
|
||||||
import type { Api400Error, ApiResponse, Device } from "@/lib/backend-types";
|
import type { Api400Error, ApiResponse, Device } from "@/lib/backend-types";
|
||||||
|
import { checkSession } from "@/utils/session";
|
||||||
import { getServerSession } from "next-auth";
|
import { getServerSession } from "next-auth";
|
||||||
import { revalidatePath } from "next/cache";
|
import { revalidatePath } from "next/cache";
|
||||||
|
|
||||||
@ -12,7 +13,7 @@ type GetDevicesProps = {
|
|||||||
status?: string;
|
status?: string;
|
||||||
};
|
};
|
||||||
export async function getDevices({ query }: GetDevicesProps) {
|
export async function getDevices({ query }: GetDevicesProps) {
|
||||||
const session = await getServerSession(authOptions);
|
const session = await checkSession();
|
||||||
const respose = await fetch(
|
const respose = await fetch(
|
||||||
`${process.env.SARLINK_API_BASE_URL}/api/devices/?name=${query}`,
|
`${process.env.SARLINK_API_BASE_URL}/api/devices/?name=${query}`,
|
||||||
{
|
{
|
||||||
@ -27,6 +28,22 @@ export async function getDevices({ query }: GetDevicesProps) {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getDevice({ deviceId }: { deviceId: string }) {
|
||||||
|
const session = await checkSession();
|
||||||
|
const respose = await fetch(
|
||||||
|
`${process.env.SARLINK_API_BASE_URL}/api/devices/${deviceId}/`,
|
||||||
|
{
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Token ${session?.apiToken}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const device = (await respose.json()) as Device;
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
export async function addDevice({
|
export async function addDevice({
|
||||||
name,
|
name,
|
||||||
mac,
|
mac,
|
||||||
|
11
utils/session.ts
Normal file
11
utils/session.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { authOptions } from "@/app/auth";
|
||||||
|
import { getServerSession } from "next-auth";
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
|
export async function checkSession() {
|
||||||
|
const session = await getServerSession(authOptions);
|
||||||
|
if (!session) return redirect("/auth/signin");
|
||||||
|
return session;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user