mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-02 03:38:22 +00:00
refactor: add tryCatch utility for error handling, update device-related components and types, and clean up unused code in payment actions
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 13m55s
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 13m55s
This commit is contained in:
61
queries/devices.ts
Normal file
61
queries/devices.ts
Normal file
@ -0,0 +1,61 @@
|
||||
"use server";
|
||||
|
||||
import { authOptions } from "@/app/auth";
|
||||
import type { Api400Error, ApiResponse, Device } from "@/lib/backend-types";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
type GetDevicesProps = {
|
||||
query?: string;
|
||||
page?: number;
|
||||
sortBy?: string;
|
||||
status?: string;
|
||||
};
|
||||
export async function getDevices({ query }: GetDevicesProps) {
|
||||
const session = await getServerSession(authOptions);
|
||||
const respose = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/devices/?name=${query}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Token ${session?.apiToken}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
const data = (await respose.json()) as ApiResponse<Device>;
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function addDevice({
|
||||
name,
|
||||
mac,
|
||||
}: {
|
||||
name: string;
|
||||
mac: string;
|
||||
}) {
|
||||
type SingleDevice = Pick<Device, "name" | "mac">;
|
||||
const session = await getServerSession(authOptions);
|
||||
const response = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/devices/`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Token ${session?.apiToken}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: name,
|
||||
mac: mac,
|
||||
}),
|
||||
},
|
||||
);
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
// Throw an error with the message from the API
|
||||
throw new Error(errorData.message || "Something went wrong.");
|
||||
}
|
||||
const data = (await response.json()) as SingleDevice;
|
||||
revalidatePath("/devices");
|
||||
return data;
|
||||
}
|
Reference in New Issue
Block a user