diff --git a/code/frontend/src/components/RepositoryEditor.js b/code/frontend/src/components/RepositoryEditor.js new file mode 100644 index 0000000..ac7b5c2 --- /dev/null +++ b/code/frontend/src/components/RepositoryEditor.js @@ -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 ( + + {children} + + ) +} diff --git a/code/frontend/src/contexts/ContextRepositoryEditor.js b/code/frontend/src/contexts/ContextRepositoryEditor.js new file mode 100644 index 0000000..5d9ff86 --- /dev/null +++ b/code/frontend/src/contexts/ContextRepositoryEditor.js @@ -0,0 +1,5 @@ +import {createContext} from "react"; + + +const ContextRepositoryEditor = createContext(null) +export default ContextRepositoryEditor diff --git a/code/frontend/src/hooks/useArrayState.js b/code/frontend/src/hooks/useArrayState.js new file mode 100644 index 0000000..a0d312e --- /dev/null +++ b/code/frontend/src/hooks/useArrayState.js @@ -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} +} \ No newline at end of file