-
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 #31 from bcgov/feature/klamm-connection-warnings
New warnings when editing a file that show missing Klamm connections
- Loading branch information
Showing
4 changed files
with
144 additions
and
2 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
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,107 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { Tag, App, FloatButton } from "antd"; | ||
import { WarningFilled } from "@ant-design/icons"; | ||
import { DecisionGraphType } from "@gorules/jdm-editor"; | ||
import { getRuleMap, generateSchemaFromRuleContent } from "@/app/utils/api"; | ||
import styles from "./SavePublish.module.css"; | ||
|
||
interface SavePublishProps { | ||
filePath: string; | ||
ruleContent: DecisionGraphType; | ||
isSaving: boolean; | ||
} | ||
|
||
interface MisconnectedField { | ||
field: string; | ||
describer: string; | ||
} | ||
|
||
export default function SavePublishWarnings({ filePath, ruleContent, isSaving }: SavePublishProps) { | ||
const { notification } = App.useApp(); | ||
const [misconnectedFields, setMisconnectedFields] = useState<MisconnectedField[]>([]); | ||
const [misconnectedFieldsPanelOpen, setMisconnectedFieldsPanelOpen] = useState(false); | ||
|
||
const getMisconnectedFields = async () => { | ||
// TODO: Move these API calls locally to reduce strain on server (if this solution is working well) | ||
// Get map from input/output nodes | ||
const inputOutputSchemaMap = await getRuleMap(filePath, ruleContent); | ||
const existingKlammInputs = inputOutputSchemaMap.inputs.map(({ field }) => field as string); | ||
const existingKlammOutputs = inputOutputSchemaMap.resultOutputs.map(({ field }) => field as string); | ||
|
||
// Get map the old way for comparrison | ||
const generatedSchemaMap = await generateSchemaFromRuleContent(ruleContent); | ||
const generatedInputs = generatedSchemaMap.inputs.map(({ field }) => field as string); | ||
const generatedOutputs = generatedSchemaMap.resultOutputs.map(({ field }) => field as string); | ||
|
||
const updatedMisconnectedFields: MisconnectedField[] = []; | ||
|
||
// Helper function to find missing or unused fields | ||
const findMisconnectedFields = (sourceFields: string[], targetFields: string[], describer: string) => { | ||
sourceFields.forEach((field) => { | ||
if (field && !targetFields.includes(field)) { | ||
updatedMisconnectedFields.push({ describer, field }); | ||
} | ||
}); | ||
}; | ||
|
||
// Check if there are fields unlinked to Klamm input/output field | ||
findMisconnectedFields(generatedInputs, existingKlammInputs, "MISSING INPUT"); | ||
findMisconnectedFields(generatedOutputs, existingKlammOutputs, "MISSING OUTPUT"); | ||
|
||
// Check if there are fields in input/output schema that are unused | ||
findMisconnectedFields(existingKlammInputs, generatedInputs, "UNUSED INPUT"); | ||
findMisconnectedFields(existingKlammOutputs, generatedOutputs, "UNUSED OUTPUT"); | ||
|
||
setMisconnectedFields(updatedMisconnectedFields); | ||
}; | ||
|
||
const warnOfMisconnectedFields = async () => { | ||
notification.warning({ | ||
key: "klamm-warning", | ||
message: "Misconnected Klamm Fields", | ||
description: ( | ||
<ul className={styles.notificationWarningList}> | ||
{misconnectedFields.map(({ describer, field }) => ( | ||
<li key={`${describer}:${field}`}> | ||
<Tag color={describer.includes("UNUSED") ? "orange" : "red"}> | ||
<small>{describer}</small> | ||
</Tag> | ||
{field} | ||
</li> | ||
))} | ||
</ul> | ||
), | ||
placement: "bottomRight", | ||
duration: 0, | ||
onClose: () => setMisconnectedFieldsPanelOpen(false), | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
getMisconnectedFields(); | ||
}, [ruleContent]); | ||
|
||
useEffect(() => { | ||
if (isSaving) { | ||
setMisconnectedFieldsPanelOpen(true); | ||
} | ||
}, [isSaving]); | ||
|
||
useEffect(() => { | ||
if (misconnectedFieldsPanelOpen) { | ||
warnOfMisconnectedFields(); | ||
} | ||
}, [misconnectedFields, misconnectedFieldsPanelOpen]); | ||
|
||
return ( | ||
<> | ||
{!misconnectedFieldsPanelOpen && ( | ||
<FloatButton | ||
icon={<WarningFilled style={{ color: "orange" }} />} | ||
badge={{ count: misconnectedFields.length, color: "orange" }} | ||
onClick={() => setMisconnectedFieldsPanelOpen(true)} | ||
/> | ||
)} | ||
</> | ||
); | ||
} |
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