2021-05-07 01:44:05 +00:00
|
|
|
import React, { useCallback, useState } from "react"
|
2021-04-29 14:58:31 +00:00
|
|
|
import ContextRepositoryEditor from "../../contexts/ContextRepositoryEditor"
|
|
|
|
import useArrayState from "../../hooks/useArrayState"
|
2021-04-29 02:28:33 +00:00
|
|
|
|
|
|
|
|
2021-05-07 01:44:05 +00:00
|
|
|
export default function RepositoryEditor({
|
|
|
|
children,
|
2021-05-07 02:48:25 +00:00
|
|
|
refresh,
|
2021-05-07 01:44:05 +00:00
|
|
|
id = null,
|
|
|
|
name,
|
|
|
|
is_active: isActive,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
conditions,
|
|
|
|
evaluation_mode: evaluationMode,
|
|
|
|
}) {
|
|
|
|
/** The repository name. */
|
2021-04-29 02:28:33 +00:00
|
|
|
const [_name, setName] = useState(name)
|
2021-05-07 01:44:05 +00:00
|
|
|
|
|
|
|
/** The repository state (active / archived). */
|
|
|
|
const [_isActive, setActive] = useState(isActive)
|
|
|
|
|
|
|
|
/** The start date of the data gathering. */
|
2021-04-29 02:28:33 +00:00
|
|
|
const [_start, setStart] = useState(start)
|
2021-05-07 01:44:05 +00:00
|
|
|
|
|
|
|
/** The end date of the data gathering. */
|
2021-04-29 02:28:33 +00:00
|
|
|
const [_end, setEnd] = useState(end)
|
2021-05-07 01:44:05 +00:00
|
|
|
|
|
|
|
/** The conditions of the data gathering. */
|
2021-04-29 03:03:58 +00:00
|
|
|
const {
|
|
|
|
value: _conditions,
|
2021-05-07 02:48:25 +00:00
|
|
|
setValue: setRawConditions,
|
|
|
|
appendValue: appendRawCondition,
|
|
|
|
removeValue: removeRawCondition,
|
|
|
|
spliceValue: spliceRawCondition,
|
2021-04-29 03:03:58 +00:00
|
|
|
} = useArrayState(conditions)
|
2021-04-29 02:28:33 +00:00
|
|
|
|
2021-05-07 01:44:05 +00:00
|
|
|
/** The operator the conditions should be evaluated with. */
|
|
|
|
const [_evaluationMode, setEvaluationMode] = useState(evaluationMode)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invia al backend le modifiche effettuate.
|
|
|
|
*/
|
|
|
|
const save = useCallback(
|
|
|
|
() => {
|
|
|
|
if(id === null) {
|
|
|
|
// POST
|
|
|
|
throw new Error("Not yet implemented")
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// PUT
|
|
|
|
throw new Error("Not yet implemented")
|
|
|
|
}
|
2021-05-07 02:48:25 +00:00
|
|
|
|
|
|
|
refresh()
|
2021-05-07 01:44:05 +00:00
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel the changes made so far to the repository.
|
|
|
|
*/
|
|
|
|
const revert = useCallback(
|
|
|
|
() => {
|
|
|
|
setName(name)
|
|
|
|
setActive(isActive)
|
|
|
|
setStart(start)
|
|
|
|
setEnd(end)
|
2021-05-07 02:48:25 +00:00
|
|
|
setRawConditions(conditions)
|
2021-05-07 01:44:05 +00:00
|
|
|
setEvaluationMode(evaluationMode)
|
|
|
|
},
|
|
|
|
[name, isActive, start, end, conditions, evaluationMode]
|
|
|
|
)
|
|
|
|
|
2021-05-07 02:48:25 +00:00
|
|
|
/**
|
|
|
|
* 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]
|
|
|
|
)
|
|
|
|
|
2021-04-29 02:28:33 +00:00
|
|
|
return (
|
|
|
|
<ContextRepositoryEditor.Provider value={{
|
|
|
|
id,
|
2021-05-07 01:44:05 +00:00
|
|
|
name: _name, setName,
|
|
|
|
isActive: _isActive, setActive,
|
|
|
|
start: _start, setStart,
|
|
|
|
end: _end, setEnd,
|
2021-05-07 02:48:25 +00:00
|
|
|
conditions: _conditions, addCondition, appendRawCondition, removeRawCondition, spliceRawCondition,
|
2021-05-07 01:44:05 +00:00
|
|
|
evaluationMode: _evaluationMode, setEvaluationMode,
|
|
|
|
revert, save,
|
2021-04-29 02:28:33 +00:00
|
|
|
}}>
|
|
|
|
{children}
|
|
|
|
</ContextRepositoryEditor.Provider>
|
|
|
|
)
|
|
|
|
}
|