Skip to content

Commit

Permalink
test add refresh button
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewThien committed Dec 13, 2024
1 parent d029d5b commit 9511f99
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 3 deletions.
8 changes: 6 additions & 2 deletions app/next-client-app/app/(protected)/scanreports/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DataTable } from "@/components/data-table";
import { objToQuery } from "@/lib/client-utils";
import { FilterParameters } from "@/types/filter";
import { DataTableFilter } from "@/components/data-table/DataTableFilter";
import ScanReportsTableClient from "@/components/jobs/SRTableTest";

interface ScanReportsTableProps {
params: {
Expand All @@ -24,7 +25,6 @@ export default async function ScanReportsTable({

const combinedParams = { ...defaultParams, ...searchParams };
const query = objToQuery(combinedParams);
const filter = <DataTableFilter filter="name" />;

const scanReportsTables = await getScanReportTables(id, query);
const permissions = await getScanReportPermissions(id);
Expand All @@ -42,12 +42,16 @@ export default async function ScanReportsTable({
return (
<div>
<div>
<DataTable
{/* <DataTable
columns={columns}
data={scanReportsResult}
count={scanReportsTables.count}
Filter={filter}
linkPrefix="tables/"
/> */}
<ScanReportsTableClient
scanReportId={id}
initialScanReportsResult={scanReportsResult}
/>
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion app/next-client-app/components/data-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from "@/components/ui/dropdown-menu";
import { DataTablePagination } from "./DataTablePagination";
import { Columns3 } from "lucide-react";
import { RefreshJobsButton } from "../jobs/RefreshButton";

interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
Expand All @@ -39,6 +40,7 @@ interface DataTableProps<TData, TValue> {
clickableRow?: boolean;
viewColumns?: boolean;
paginated?: boolean;
RefreshJobsButton?: JSX.Element;
defaultPageSize?: 10 | 20 | 30 | 40 | 50;
}

Expand All @@ -55,6 +57,7 @@ export function DataTable<TData, TValue>({
clickableRow = true,
viewColumns = true,
paginated = true,
RefreshJobsButton,
defaultPageSize,
}: DataTableProps<TData, TValue>) {
const [columnVisibility, setColumnVisibility] =
Expand Down Expand Up @@ -92,8 +95,9 @@ export function DataTable<TData, TValue>({

return (
<div>
<div className="flex justify-between mb-3">
<div className="flex justify-between items-center mb-3">
{Filter}
{RefreshJobsButton}
{/* Views Columns Menu */}
{viewColumns && (
<DropdownMenu>
Expand Down
54 changes: 54 additions & 0 deletions app/next-client-app/components/jobs/RefreshButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { RefreshCw } from "lucide-react";
import { getJobs } from "@/api/scanreports";

interface RefreshJobsButtonProps {
scanReportId: string;
onJobsRefresh: (updatedJobs: Job[]) => void;
}

export function RefreshJobsButton({
scanReportId,
onJobsRefresh,
}: RefreshJobsButtonProps) {
const [isRefreshing, setIsRefreshing] = useState(false);

const handleRefresh = async () => {
try {
setIsRefreshing(true);

// Fetch updated jobs
const updatedJobs = await getJobs(scanReportId);
if (updatedJobs) {
onJobsRefresh(updatedJobs);
}
// Call the provided callback to update parent component
} catch (error) {
console.error("Failed to refresh jobs:", error);
// Optionally add error toast or notification
} finally {
setIsRefreshing(false);
}
};

return (
<Button
variant="outline"
onClick={handleRefresh}
disabled={isRefreshing}
className="ml-2"
>
{isRefreshing ? (
<>Refreshing...</>
) : (
<>
<RefreshCw className="mr-2 h-4 w-4" />
Refresh Jobs Progress
</>
)}
</Button>
);
}
49 changes: 49 additions & 0 deletions app/next-client-app/components/jobs/SRTableTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use client";

import { useState } from "react";

import { DataTable } from "@/components/data-table";
import { RefreshJobsButton } from "./RefreshButton";
import { columns } from "@/app/(protected)/scanreports/[id]/columns";
import { DataTableFilter } from "../data-table/DataTableFilter";

export default function ScanReportsTableClient({
initialScanReportsResult,
scanReportId,
}: {
initialScanReportsResult: ScanReportTable[];
scanReportId: string;
}) {
const filter = <DataTableFilter filter="name" />;
const [scanReportsResult, setScanReportsResult] = useState(
initialScanReportsResult
);

const handleJobsRefresh = (updatedJobs: Job[]) => {
// Update the jobs for each table row
const updatedScanReportsResult = scanReportsResult.map((table) => ({
...table,
jobs: updatedJobs,
}));

setScanReportsResult(updatedScanReportsResult);
};

return (
<div>
<DataTable
columns={columns}
data={scanReportsResult}
count={scanReportsResult.length}
Filter={filter}
RefreshJobsButton={
<RefreshJobsButton
scanReportId={scanReportId}
onJobsRefresh={handleJobsRefresh}
/>
}
linkPrefix="tables/"
/>
</div>
);
}

0 comments on commit 9511f99

Please sign in to comment.