1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-24 22:14:18 +00:00

🔧 Add new features to RepositoryEditor

This commit is contained in:
Stefano Pigozzi 2021-05-07 04:48:25 +02:00
parent 1ccff60b3e
commit eda2ab8c6e
Signed by untrusted user who does not match committer: steffo
GPG key ID: 6965406171929D01

View file

@ -5,6 +5,7 @@ import useArrayState from "../../hooks/useArrayState"
export default function RepositoryEditor({ export default function RepositoryEditor({
children, children,
refresh,
id = null, id = null,
name, name,
is_active: isActive, is_active: isActive,
@ -28,10 +29,10 @@ export default function RepositoryEditor({
/** The conditions of the data gathering. */ /** The conditions of the data gathering. */
const { const {
value: _conditions, value: _conditions,
setValue: setConditions, setValue: setRawConditions,
appendValue: appendCondition, appendValue: appendRawCondition,
removeValue: removeCondition, removeValue: removeRawCondition,
spliceValue: spliceCondition, spliceValue: spliceRawCondition,
} = useArrayState(conditions) } = useArrayState(conditions)
/** The operator the conditions should be evaluated with. */ /** The operator the conditions should be evaluated with. */
@ -50,6 +51,8 @@ export default function RepositoryEditor({
// PUT // PUT
throw new Error("Not yet implemented") throw new Error("Not yet implemented")
} }
refresh()
}, },
[id] [id]
) )
@ -63,12 +66,42 @@ export default function RepositoryEditor({
setActive(isActive) setActive(isActive)
setStart(start) setStart(start)
setEnd(end) setEnd(end)
setConditions(conditions) setRawConditions(conditions)
setEvaluationMode(evaluationMode) setEvaluationMode(evaluationMode)
}, },
[name, isActive, start, end, conditions, 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 ( return (
<ContextRepositoryEditor.Provider value={{ <ContextRepositoryEditor.Provider value={{
id, id,
@ -76,7 +109,7 @@ export default function RepositoryEditor({
isActive: _isActive, setActive, isActive: _isActive, setActive,
start: _start, setStart, start: _start, setStart,
end: _end, setEnd, end: _end, setEnd,
conditions: _conditions, appendCondition, removeCondition, spliceCondition, conditions: _conditions, addCondition, appendRawCondition, removeRawCondition, spliceRawCondition,
evaluationMode: _evaluationMode, setEvaluationMode, evaluationMode: _evaluationMode, setEvaluationMode,
revert, save, revert, save,
}}> }}>