mirror of
https://github.com/pds-nest/nest.git
synced 2024-11-25 14:34:19 +00:00
🚧 Add component RepositoryEditor
Documentation is missing
This commit is contained in:
parent
875c5e0da9
commit
0fcbc64191
3 changed files with 62 additions and 0 deletions
28
code/frontend/src/components/RepositoryEditor.js
Normal file
28
code/frontend/src/components/RepositoryEditor.js
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
import React, { useState } from "react"
|
||||||
|
import ContextRepositoryEditor from "../contexts/ContextRepositoryEditor"
|
||||||
|
import useArrayState from "../hooks/useArrayState"
|
||||||
|
|
||||||
|
|
||||||
|
export default function RepositoryEditor({ children, id, name, start, end, conditions }) {
|
||||||
|
const [_name, setName] = useState(name)
|
||||||
|
const [_start, setStart] = useState(start)
|
||||||
|
const [_end, setEnd] = useState(end)
|
||||||
|
const {_conditions, appendCondition, removeCondition} = useArrayState(conditions)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ContextRepositoryEditor.Provider value={{
|
||||||
|
id,
|
||||||
|
name: _name,
|
||||||
|
setName,
|
||||||
|
start: _start,
|
||||||
|
setStart,
|
||||||
|
end: _end,
|
||||||
|
setEnd,
|
||||||
|
conditions: _conditions,
|
||||||
|
appendCondition,
|
||||||
|
removeCondition,
|
||||||
|
}}>
|
||||||
|
{children}
|
||||||
|
</ContextRepositoryEditor.Provider>
|
||||||
|
)
|
||||||
|
}
|
5
code/frontend/src/contexts/ContextRepositoryEditor.js
Normal file
5
code/frontend/src/contexts/ContextRepositoryEditor.js
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import {createContext} from "react";
|
||||||
|
|
||||||
|
|
||||||
|
const ContextRepositoryEditor = createContext(null)
|
||||||
|
export default ContextRepositoryEditor
|
29
code/frontend/src/hooks/useArrayState.js
Normal file
29
code/frontend/src/hooks/useArrayState.js
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import { useCallback, useState } from "react"
|
||||||
|
|
||||||
|
|
||||||
|
export default function useArrayState(def) {
|
||||||
|
const [value, setValue] = useState(def ?? [])
|
||||||
|
|
||||||
|
const appendValue = useCallback(
|
||||||
|
newSingle => {
|
||||||
|
setValue(
|
||||||
|
oldArray => [...oldArray, newSingle]
|
||||||
|
)
|
||||||
|
},
|
||||||
|
[value, setValue]
|
||||||
|
)
|
||||||
|
|
||||||
|
const removeValue = useCallback(
|
||||||
|
position => {
|
||||||
|
setValue(
|
||||||
|
oldArray => {
|
||||||
|
// TODO: Hope this doesn't break anything...
|
||||||
|
oldArray.splice(position, 1)
|
||||||
|
return oldArray
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return {value, setValue, appendValue, removeValue}
|
||||||
|
}
|
Loading…
Reference in a new issue