Refactor: Break down single table component into multiple modular components

This commit is contained in:
2024-12-30 13:16:13 +01:00
parent 8c9688017b
commit 95380493cb
33 changed files with 1562 additions and 6642 deletions
+48 -45
View File
@@ -1,56 +1,59 @@
import pb from "@/app/lib/pocketbase";
import pb from "@/lib/pocketbase";
const { EMAIL, PASSWORD } = process.env;
async function authenticateSuperuser() {
if (!pb.authStore.isValid) {
await pb.collection("_superusers").authWithPassword(EMAIL!, PASSWORD!);
}
if (!EMAIL || !PASSWORD) {
throw new Error("Environment variables EMAIL and PASSWORD must be set");
}
if (!pb.authStore.isValid) {
await pb.collection("_superusers").authWithPassword(EMAIL, PASSWORD);
}
}
export async function GET() {
try {
await authenticateSuperuser();
const records = await pb.collection("budgetable").getFullList();
return new Response(JSON.stringify(records), {
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error("Error fetching data:", error);
return new Response(
JSON.stringify({
error: {
message: "Failed to fetch data",
},
}),
{
status: 500,
headers: { "Content-Type": "application/json" },
}
);
}
try {
await authenticateSuperuser();
const records = await pb.collection("budgetable").getFullList();
return new Response(JSON.stringify(records), {
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error("Error fetching data:", error);
return new Response(
JSON.stringify({
error: {
message: "Failed to fetch data",
},
}),
{
status: 500,
headers: { "Content-Type": "application/json" },
},
);
}
}
export async function POST(req: Request) {
try {
await authenticateSuperuser();
const data = await req.json();
const record = await pb.collection("budgetable").create(data);
return new Response(JSON.stringify(record), {
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error("Error adding data:", error);
return new Response(
JSON.stringify({
error: {
message: "Failed to add data",
},
}),
{
status: 500,
headers: { "Content-Type": "application/json" },
}
);
}
try {
await authenticateSuperuser();
const data = await req.json();
const record = await pb.collection("budgetable").create(data);
return new Response(JSON.stringify(record), {
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error("Error adding data:", error);
return new Response(
JSON.stringify({
error: {
message: "Failed to add data",
},
}),
{
status: 500,
headers: { "Content-Type": "application/json" },
},
);
}
}