-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from bcgov/feature/linking-rules
JDM Update and Linking Rules
- Loading branch information
Showing
11 changed files
with
1,550 additions
and
965 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
app/components/RulesDecisionGraph/LinkRuleComponent.module.css
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,8 @@ | ||
.ruleSelect { | ||
min-width: 350px; | ||
margin-bottom: 16px; | ||
} | ||
|
||
.linkDrawer { | ||
width: 85% !important; | ||
} |
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,89 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { Button, Drawer, Flex, Select, Spin } from "antd"; | ||
import { DefaultOptionType } from "antd/es/cascader"; | ||
import { EditOutlined } from "@ant-design/icons"; | ||
import { GraphNode, useDecisionGraphActions, useDecisionGraphState } from "@gorules/jdm-editor"; | ||
import type { GraphNodeProps } from "@gorules/jdm-editor"; | ||
import { getAllRuleData } from "@/app/utils/api"; | ||
import SimulationViewer from "../SimulationViewer"; | ||
import styles from "./LinkRuleComponent.module.css"; | ||
|
||
export default function LinkRuleComponent({ specification, id, isSelected, name }: GraphNodeProps) { | ||
const { updateNode } = useDecisionGraphActions(); | ||
const node = useDecisionGraphState((state) => (state.decisionGraph?.nodes || []).find((n) => n.id === id)); | ||
const goRulesJSONFilename = node?.content?.key; | ||
|
||
const [openRuleDrawer, setOpenRuleDrawer] = useState(false); | ||
const [ruleOptions, setRuleOptions] = useState<DefaultOptionType[]>([]); | ||
|
||
useEffect(() => { | ||
const getRuleOptions = async () => { | ||
const ruleData = await getAllRuleData(); | ||
setRuleOptions( | ||
ruleData.map(({ title, goRulesJSONFilename }) => ({ | ||
label: title || goRulesJSONFilename, | ||
value: goRulesJSONFilename, | ||
})) | ||
); | ||
}; | ||
if (openRuleDrawer) { | ||
getRuleOptions(); | ||
} | ||
}, [openRuleDrawer]); | ||
|
||
const showRuleDrawer = () => { | ||
setOpenRuleDrawer(true); | ||
}; | ||
|
||
const closeRuleDrawer = () => { | ||
setOpenRuleDrawer(false); | ||
}; | ||
|
||
const onChangeSelection = (jsonFilename: string) => { | ||
// Update the graph with the jsonFilename. We use "key" to keep in line with how goRules handing linking rules | ||
updateNode(id, (draft) => { | ||
draft.content = { key: jsonFilename }; | ||
return draft; | ||
}); | ||
}; | ||
|
||
const getShortFilenameOnly = (filepath: string, maxLength: number = 25) => { | ||
const filepathSections = filepath.split("/"); | ||
const filename = filepathSections[filepathSections.length - 1]; | ||
return filename.length > maxLength ? `${filename.substring(0, maxLength - 3)}...` : filename; | ||
}; | ||
|
||
return ( | ||
<GraphNode id={id} specification={specification} name={name} isSelected={isSelected}> | ||
<Button onClick={showRuleDrawer}> | ||
{goRulesJSONFilename ? getShortFilenameOnly(goRulesJSONFilename) : "Add rule"} | ||
<EditOutlined /> | ||
</Button> | ||
<Drawer title={name} onClose={closeRuleDrawer} open={openRuleDrawer} width="80%"> | ||
{ruleOptions ? ( | ||
<> | ||
<Flex gap="small"> | ||
<Select | ||
showSearch | ||
placeholder="Select rule" | ||
filterOption={(input, option) => | ||
(option?.label ?? "").toString().toLowerCase().includes(input.toLowerCase()) | ||
} | ||
options={ruleOptions} | ||
onChange={onChangeSelection} | ||
value={goRulesJSONFilename} | ||
className={styles.ruleSelect} | ||
/> | ||
<Button onClick={closeRuleDrawer}>Done</Button> | ||
</Flex> | ||
{goRulesJSONFilename && <SimulationViewer ruleId={id} jsonFile={goRulesJSONFilename} />} | ||
</> | ||
) : ( | ||
<Spin tip="Loading rules..." size="large" className={styles.spinner}> | ||
<div className="content" /> | ||
</Spin> | ||
)} | ||
</Drawer> | ||
</GraphNode> | ||
); | ||
} |
3 changes: 0 additions & 3 deletions
3
app/components/RulesDecisionGraph/RulesDecisionGraph.module.css
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from "react"; | ||
import JSON5 from "json5"; | ||
import { GraphSimulator } from "@gorules/jdm-editor"; | ||
import { SubmissionData } from "@/app/types/submission"; | ||
|
||
interface SimulatorPanelProps { | ||
contextToSimulate?: SubmissionData | null; | ||
setContextToSimulate: (results: Record<string, any>) => void; | ||
runSimulation: (results: unknown) => void; | ||
} | ||
|
||
export default function SimulatorPanel({ | ||
contextToSimulate, | ||
runSimulation, | ||
setContextToSimulate, | ||
}: SimulatorPanelProps) { | ||
return ( | ||
<GraphSimulator | ||
defaultRequest={JSON5.stringify(contextToSimulate)} | ||
onRun={({ context }: { context: unknown }) => runSimulation(context)} | ||
onClear={() => setContextToSimulate({})} | ||
/> | ||
); | ||
} |
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 |
---|---|---|
|
@@ -9,4 +9,9 @@ | |
|
||
.inputSection button { | ||
width: 10rem; | ||
} | ||
|
||
.spinner { | ||
min-height: 500px; | ||
|
||
} |
Oops, something went wrong.