From 0fcbc64191588940e9a4249c6fc6c7ce49000757 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi <256895@studenti.unimore.it> Date: Thu, 29 Apr 2021 04:28:33 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Add=20component=20RepositoryEdit?= =?UTF-8?q?or?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Documentation is missing --- .../src/components/RepositoryEditor.js | 28 ++++++++++++++++++ .../src/contexts/ContextRepositoryEditor.js | 5 ++++ code/frontend/src/hooks/useArrayState.js | 29 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 code/frontend/src/components/RepositoryEditor.js create mode 100644 code/frontend/src/contexts/ContextRepositoryEditor.js create mode 100644 code/frontend/src/hooks/useArrayState.js 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