Skip to content

Commit

Permalink
use select in dashboard to filter
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximilianAnzinger committed Dec 11, 2024
1 parent 0e72d35 commit 9917a7f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
9 changes: 6 additions & 3 deletions atlas-metrics/app/components/custom/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import { DateRangePicker } from "@components/custom/DateRangePicker";
import { ServiceSelect } from "@components/custom/ServiceSelect";

const services = ["Service A", "Service B"];
interface MenuProps {
selectedService?: string;
services: string[];
}

export function Menu() {
export function Menu({ selectedService, services }: MenuProps) {
return (
<div className="grid auto-cols-max grid-flow-col justify-end gap-5 pb-5">
<ServiceSelect selectedService={services[0]} services={services} />
<ServiceSelect selectedService={selectedService} services={services} />
<DateRangePicker />
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion atlas-metrics/app/components/custom/ServiceSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "@components/ui/select";

interface ServiceSelectProps {
selectedService: string;
selectedService?: string;
services: string[];
}

Expand Down
31 changes: 28 additions & 3 deletions atlas-metrics/app/dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,39 @@ import { PieChartDonut } from "@components/custom/PieChartDonut";
import { Menu } from "@components/custom/Menu";
import { useEffect, useState } from "react";
import { DashboardDataDAO, DashboardDataSchema } from "@server/domain/dao/dashboardData";
import { z } from "zod";
import { useSearchParams } from "next/navigation";

export default function Dashboard() {
const searchParams = useSearchParams();
const selectedService = searchParams.get("service") ?? undefined;
const [services, setServices] = useState<string[]>([]);
const [metrics, setMetrics] = useState<DashboardDataDAO | undefined>(undefined);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
async function fetchServices() {
try {
const response = await fetch("/api/data/service");
const data = await response.json();
const parsed = z.array(z.string()).safeParse(data);
if (parsed.success) {
setServices(parsed.data);
} else {
setError(`Invalid response type: ${parsed.error}`);
}
} catch (error) {
setError((error as Error).message);
}
}

async function fetchMetrics() {
try {
const response = await fetch("/api/data/dashboard");
const requestUrl = selectedService
? `/api/data/dashboard?service=${selectedService}`
: "/api/data/dashboard";
const response = await fetch(requestUrl);
const data = await response.json();
const parsed = DashboardDataSchema.safeParse(data);

Expand All @@ -26,7 +49,9 @@ export default function Dashboard() {
}
}

fetchMetrics().finally(() => setLoading(false));
fetchServices()
.then(fetchMetrics)
.finally(() => setLoading(false));
}, []);

if (loading) {
Expand All @@ -39,7 +64,7 @@ export default function Dashboard() {

return (
<div className="m-5 min-h-screen text-center">
<Menu />
<Menu selectedService={selectedService} services={services} />
<div className="pb-5">
<EndpointActivity
title="Endpoint Activity"
Expand Down

0 comments on commit 9917a7f

Please sign in to comment.