-
Notifications
You must be signed in to change notification settings - Fork 702
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
224 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
export interface MappingRule { | ||
id?: number; | ||
id: number; | ||
tenant_id: string; | ||
priority: number; | ||
name: string; | ||
description?: string; | ||
file_name?: string; | ||
created_by?: string; | ||
created_at: Date; | ||
disabled: boolean; | ||
override: boolean; | ||
condition?: string; | ||
matchers: string[]; | ||
rows: { [key: string]: any }[]; | ||
attributes?: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { | ||
Badge, | ||
Button, | ||
Table, | ||
TableBody, | ||
TableCell, | ||
TableHead, | ||
TableHeaderCell, | ||
TableRow, | ||
} from "@tremor/react"; | ||
import { MappingRule } from "./models"; | ||
import { | ||
DisplayColumnDef, | ||
createColumnHelper, | ||
flexRender, | ||
getCoreRowModel, | ||
useReactTable, | ||
} from "@tanstack/react-table"; | ||
import { MdRemoveCircle } from "react-icons/md"; | ||
import { useSession } from "next-auth/react"; | ||
import { getApiURL } from "utils/apiUrl"; | ||
import { useMappings } from "utils/hooks/useMappingRules"; | ||
import { toast } from "react-toastify"; | ||
|
||
const columnHelper = createColumnHelper<MappingRule>(); | ||
|
||
export default function RulesTable({ mappings }: { mappings: MappingRule[] }) { | ||
const { data: session } = useSession(); | ||
const { mutate } = useMappings(); | ||
|
||
const columns = [ | ||
columnHelper.display({ | ||
id: "id", | ||
header: "#", | ||
cell: (context) => context.row.original.id, | ||
}), | ||
columnHelper.display({ | ||
id: "name", | ||
header: "Name", | ||
cell: (context) => context.row.original.name, | ||
}), | ||
columnHelper.display({ | ||
id: "description", | ||
header: "Description", | ||
cell: (context) => context.row.original.description, | ||
}), | ||
columnHelper.display({ | ||
id: "fileName", | ||
header: "Original File Name", | ||
cell: (context) => context.row.original.file_name, | ||
}), | ||
columnHelper.display({ | ||
id: "matchers", | ||
header: "Matchers", | ||
cell: (context) => context.row.original.matchers.join(","), | ||
}), | ||
columnHelper.display({ | ||
id: "attributes", | ||
header: "Attributes", | ||
cell: (context) => ( | ||
<div className="flex flex-wrap"> | ||
{context.row.original.attributes?.map((attr) => ( | ||
<Badge key={attr} color="orange" size="xs"> | ||
{attr} | ||
</Badge> | ||
))} | ||
</div> | ||
), | ||
}), | ||
columnHelper.display({ | ||
id: "delete", | ||
header: "", | ||
cell: (context) => ( | ||
<Button | ||
color="red" | ||
size="xs" | ||
variant="secondary" | ||
icon={MdRemoveCircle} | ||
onClick={() => deleteRule(context.row.original.id!)} | ||
/> | ||
), | ||
}), | ||
] as DisplayColumnDef<MappingRule>[]; | ||
|
||
const table = useReactTable({ | ||
columns, | ||
data: mappings, | ||
getCoreRowModel: getCoreRowModel(), | ||
}); | ||
|
||
const deleteRule = (ruleId: number) => { | ||
const apiUrl = getApiURL(); | ||
if (confirm("Are you sure you want to delete this rule?")) { | ||
fetch(`${apiUrl}/mapping/${ruleId}`, { | ||
method: "DELETE", | ||
headers: { | ||
Authorization: `Bearer ${session?.accessToken}`, | ||
}, | ||
}).then((response) => { | ||
if (response.ok) { | ||
mutate(); | ||
toast.success("Rule deleted successfully"); | ||
} else { | ||
toast.error("Failed to delete rule, contact us if this persists"); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<Table> | ||
<TableHead> | ||
{table.getHeaderGroups().map((headerGroup) => ( | ||
<TableRow | ||
className="border-b border-tremor-border dark:border-dark-tremor-border" | ||
key={headerGroup.id} | ||
> | ||
{headerGroup.headers.map((header) => { | ||
return ( | ||
<TableHeaderCell | ||
className="text-tremor-content-strong dark:text-dark-tremor-content-strong" | ||
key={header.id} | ||
> | ||
{flexRender( | ||
header.column.columnDef.header, | ||
header.getContext() | ||
)} | ||
</TableHeaderCell> | ||
); | ||
})} | ||
</TableRow> | ||
))} | ||
</TableHead> | ||
<TableBody> | ||
{table.getRowModel().rows.map((row) => ( | ||
<TableRow | ||
className="even:bg-tremor-background-muted even:dark:bg-dark-tremor-background-muted hover:bg-slate-100" | ||
key={row.id} | ||
> | ||
{row.getVisibleCells().map((cell) => ( | ||
<TableCell key={cell.id}> | ||
{flexRender(cell.column.columnDef.cell, cell.getContext())} | ||
</TableCell> | ||
))} | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters