1
Fork 0
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:
Stefano Pigozzi 2021-04-29 04:28:33 +02:00
parent 875c5e0da9
commit 0fcbc64191
Signed by untrusted user who does not match committer: steffo
GPG key ID: 6965406171929D01
3 changed files with 62 additions and 0 deletions

View 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>
)
}

View file

@ -0,0 +1,5 @@
import {createContext} from "react";
const ContextRepositoryEditor = createContext(null)
export default ContextRepositoryEditor

View 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}
}