mirror of
https://github.com/pds-nest/nest.git
synced 2024-11-26 06:54:18 +00:00
🔧 Refactor and document RepositoryEditor
This commit is contained in:
parent
26dcf3014e
commit
e7471a29f4
1 changed files with 64 additions and 12 deletions
|
@ -1,32 +1,84 @@
|
||||||
import React, { useState } from "react"
|
import React, { useCallback, useState } from "react"
|
||||||
import ContextRepositoryEditor from "../../contexts/ContextRepositoryEditor"
|
import ContextRepositoryEditor from "../../contexts/ContextRepositoryEditor"
|
||||||
import useArrayState from "../../hooks/useArrayState"
|
import useArrayState from "../../hooks/useArrayState"
|
||||||
|
|
||||||
|
|
||||||
export default function RepositoryEditor({ children, id, name, start, end, conditions }) {
|
export default function RepositoryEditor({
|
||||||
|
children,
|
||||||
|
id = null,
|
||||||
|
name,
|
||||||
|
is_active: isActive,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
conditions,
|
||||||
|
evaluation_mode: evaluationMode,
|
||||||
|
}) {
|
||||||
|
/** The repository name. */
|
||||||
const [_name, setName] = useState(name)
|
const [_name, setName] = useState(name)
|
||||||
|
|
||||||
|
/** The repository state (active / archived). */
|
||||||
|
const [_isActive, setActive] = useState(isActive)
|
||||||
|
|
||||||
|
/** The start date of the data gathering. */
|
||||||
const [_start, setStart] = useState(start)
|
const [_start, setStart] = useState(start)
|
||||||
|
|
||||||
|
/** The end date of the data gathering. */
|
||||||
const [_end, setEnd] = useState(end)
|
const [_end, setEnd] = useState(end)
|
||||||
|
|
||||||
|
/** The conditions of the data gathering. */
|
||||||
const {
|
const {
|
||||||
value: _conditions,
|
value: _conditions,
|
||||||
|
setValue: setConditions,
|
||||||
appendValue: appendCondition,
|
appendValue: appendCondition,
|
||||||
removeValue: removeCondition,
|
removeValue: removeCondition,
|
||||||
spliceValue: spliceCondition,
|
spliceValue: spliceCondition,
|
||||||
} = useArrayState(conditions)
|
} = useArrayState(conditions)
|
||||||
|
|
||||||
|
/** 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")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[id]
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancel the changes made so far to the repository.
|
||||||
|
*/
|
||||||
|
const revert = useCallback(
|
||||||
|
() => {
|
||||||
|
setName(name)
|
||||||
|
setActive(isActive)
|
||||||
|
setStart(start)
|
||||||
|
setEnd(end)
|
||||||
|
setConditions(conditions)
|
||||||
|
setEvaluationMode(evaluationMode)
|
||||||
|
},
|
||||||
|
[name, isActive, start, end, conditions, evaluationMode]
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ContextRepositoryEditor.Provider value={{
|
<ContextRepositoryEditor.Provider value={{
|
||||||
id,
|
id,
|
||||||
name: _name,
|
name: _name, setName,
|
||||||
setName,
|
isActive: _isActive, setActive,
|
||||||
start: _start,
|
start: _start, setStart,
|
||||||
setStart,
|
end: _end, setEnd,
|
||||||
end: _end,
|
conditions: _conditions, appendCondition, removeCondition, spliceCondition,
|
||||||
setEnd,
|
evaluationMode: _evaluationMode, setEvaluationMode,
|
||||||
conditions: _conditions,
|
revert, save,
|
||||||
appendCondition,
|
|
||||||
removeCondition,
|
|
||||||
spliceCondition,
|
|
||||||
}}>
|
}}>
|
||||||
{children}
|
{children}
|
||||||
</ContextRepositoryEditor.Provider>
|
</ContextRepositoryEditor.Provider>
|
||||||
|
|
Loading…
Reference in a new issue