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({
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 (
<ContextRepositoryEditor.Provider value={{
id,
@ -76,7 +109,7 @@ export default function RepositoryEditor({
isActive: _isActive, setActive,
start: _start, setStart,
end: _end, setEnd,
conditions: _conditions, appendCondition, removeCondition, spliceCondition,
conditions: _conditions, addCondition, appendRawCondition, removeRawCondition, spliceRawCondition,
evaluationMode: _evaluationMode, setEvaluationMode,
revert, save,
}}>