From eda2ab8c6ed115bd1b1a9dbcfaed36c3ac1e9273 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi <256895@studenti.unimore.it> Date: Fri, 7 May 2021 04:48:25 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Add=20new=20features=20to=20Repo?= =?UTF-8?q?sitoryEditor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/providers/RepositoryEditor.js | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) 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 (