diff --git a/code/frontend/src/components/providers/RepositoryEditor.js b/code/frontend/src/components/providers/RepositoryEditor.js index 4684361..2098604 100644 --- a/code/frontend/src/components/providers/RepositoryEditor.js +++ b/code/frontend/src/components/providers/RepositoryEditor.js @@ -5,6 +5,7 @@ import useArrayState from "../../hooks/useArrayState" export default function RepositoryEditor({ children, + refresh, id = null, name, is_active: isActive, @@ -28,10 +29,10 @@ export default function RepositoryEditor({ /** The conditions of the data gathering. */ const { value: _conditions, - setValue: setConditions, - appendValue: appendCondition, - removeValue: removeCondition, - spliceValue: spliceCondition, + setValue: setRawConditions, + appendValue: appendRawCondition, + removeValue: removeRawCondition, + spliceValue: spliceRawCondition, } = useArrayState(conditions) /** The operator the conditions should be evaluated with. */ @@ -50,6 +51,8 @@ export default function RepositoryEditor({ // PUT throw new Error("Not yet implemented") } + + refresh() }, [id] ) @@ -63,12 +66,42 @@ export default function RepositoryEditor({ setActive(isActive) setStart(start) setEnd(end) - setConditions(conditions) + setRawConditions(conditions) setEvaluationMode(evaluationMode) }, [name, isActive, start, end, conditions, evaluationMode] ) + /** + * Try to add a new condition, logging a message to the console if something goes wrong. + */ + const addCondition = useCallback( + (newCond) => { + // Check for content + if(!newCond.content) { + console.debug("Refusing to add ", newCond, ": content is empty.") + return + } + + // Check for duplicates + let duplicate = null; + for(const oldCond of _conditions) { + if(newCond.type === oldCond.type && newCond.content === oldCond.content) { + duplicate = oldCond + break + } + } + if(duplicate) { + console.debug("Refusing to add ", newCond, ": ", duplicate, " already exists.") + return + } + + console.debug("Adding ", newCond, " to the Repository Conditions") + appendRawCondition(newCond) + }, + [_conditions] + ) + return (