Skip to content

Commit

Permalink
Merge pull request #41 from bcgov/feature/validation-errors
Browse files Browse the repository at this point in the history
Feature/validation errors
  • Loading branch information
brysonjbest authored Oct 21, 2024
2 parents b03099f + d41a8b2 commit 9320534
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
60 changes: 43 additions & 17 deletions app/admin/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";
import { useState, useEffect } from "react";
import Link from "next/link";
import { Table, Input, Button, Flex } from "antd";
import { Table, Input, Button, Flex, Tooltip } from "antd";
import { ColumnsType, TablePaginationConfig } from "antd/es/table";
import { FilterValue } from "antd/es/table/interface";
import { HomeOutlined } from "@ant-design/icons";
Expand All @@ -14,8 +14,6 @@ enum ACTION_STATUS {
DELETE = "delete",
}

const PAGE_SIZE = 15;

interface TableParams {
pagination?: TablePaginationConfig;
searchTerm?: string;
Expand Down Expand Up @@ -73,10 +71,20 @@ export default function Admin() {
setRules(newRules);
};

const deleteRule = async (index: number) => {
const { current, pageSize } = tableParams?.pagination || {};
const deletionIndex = ((current || 1) - 1) * (pageSize || PAGE_SIZE) + index;
const newRules = [...rules.slice(0, deletionIndex), ...rules.slice(deletionIndex + 1, rules.length)];
const resetDraft = async (rule: RuleInfo) => {
setIsLoading(true);
try {
await updateRuleData(rule._id, rule);
} catch (error) {
console.error(`Error reseting draft for rule ${rule._id}: ${error}`);
} finally {
setIsLoading(false);
}
};

const deleteRule = (index: number) => {
const newRules = [...rules];
newRules.splice(index, 1);
setRules(newRules);
};

Expand Down Expand Up @@ -149,20 +157,38 @@ export default function Admin() {
{
dataIndex: "delete",
width: "60px",
render: (value: string, _: RuleInfo, index: number) => (
<Button danger onClick={() => deleteRule(index)}>
Delete
</Button>
),
render: (value: string, _: RuleInfo, index: number) => {
return (
<>
{_.isPublished ? (
<Tooltip title="To delete a published rule, please review the Github Rules Repo.">
<Button danger disabled={!_.ruleDraft} onClick={() => resetDraft(_)}>
Reset Draft
</Button>
</Tooltip>
) : (
<Tooltip title="Caution: Unpublished draft rules cannot be recovered once deleted.">
<Button danger onClick={() => deleteRule(index)}>
Delete Rule{" "}
</Button>
</Tooltip>
)}
</>
);
},
},
{
dataIndex: "view",
width: "60px",
render: (_: string, { _id }: RuleInfo) => (
<Link href={`/rule/${_id}`}>
<Button>View</Button>
</Link>
),
render: (_: string, { _id, isPublished }: RuleInfo) => {
const ruleLink = `/rule/${_id}`;
const draftLink = `${ruleLink}?version=draft`;
return (
<Link href={isPublished ? ruleLink : draftLink}>
<Button>View</Button>
</Link>
);
},
},
];

Expand Down
3 changes: 2 additions & 1 deletion app/components/RuleManager/RuleManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export default function RuleManager({
if (runContext) {
console.info("Simulate:", runContext);
try {
message.destroy();
const data = await postDecision(ruleContent, runContext);
console.info("Simulation Results:", data, data?.result);
// Check if data.result is an array and throw error as object is required
Expand All @@ -114,7 +115,7 @@ export default function RuleManager({
// Set the results of the simulation
setResultsOfSimulation(data?.result);
} catch (e: any) {
message.error("Error during simulation run: " + e);
message.error("Error during simulation run: " + e, 10);
console.error("Error during simulation run:", e);
}
} else {
Expand Down
10 changes: 8 additions & 2 deletions app/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,14 @@ export const postDecision = async (ruleContent: DecisionGraphType, context: unkn
});
return data;
} catch (error) {
console.error(`Error simulating decision: ${error}`);
throw error;
if (axios.isAxiosError(error) && error.response) {
const errorMessage = error.response.data.message;
console.error(errorMessage);
throw new Error(errorMessage);
} else {
console.error(`Error simulating decision: ${error}`);
throw error;
}
}
};

Expand Down

0 comments on commit 9320534

Please sign in to comment.