fixed error when data not found (#2449)

* fixed error when data not found

fixes: #2447

* change to useMemo
This commit is contained in:
Erik Jan de Wit 2022-04-20 21:10:30 +02:00 committed by GitHub
parent 709a9987ad
commit 50ba699ab8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { Controller, useFieldArray, useFormContext } from "react-hook-form";
import {
@ -56,7 +56,6 @@ const ValueInput = ({
}: ValueInputProps) => {
const { t } = useTranslation("common");
const { control, register, getValues } = useFormContext();
const [isValueOpenArray, setIsValueOpenArray] = useState([false]);
const toggleValueSelect = (rowIndex: number, open: boolean) => {
@ -65,19 +64,23 @@ const ValueInput = ({
setIsValueOpenArray(arr);
};
let attributeValues: { key: string; name: string }[] | undefined = [];
const scopeValues = resources?.find(
(resource) => resource.name === getValues().resources[rowIndex]?.key
)?.scopes;
const attributeValues = useMemo(() => {
let values: AttributeType[] | undefined = [];
if (selectableValues) {
attributeValues = defaultContextAttributes.find(
(attr) => attr.key === getValues().context[rowIndex]?.key
values = defaultContextAttributes.find(
(attr) => attr.key === getValues().context?.[rowIndex]?.key
)?.values;
}
return values;
}, [getValues]);
const renderSelectOptionType = () => {
const scopeValues = resources?.find(
(resource) => resource.name === getValues().resources?.[rowIndex]?.key
)?.scopes;
if (attributeValues?.length && !resources) {
return attributeValues.map((attr) => (
<SelectOption key={attr.key} value={attr.key}>
@ -165,7 +168,7 @@ export const KeyBasedAttributeInput = ({
useEffect(() => {
if (!fields.length) {
append({ key: "", value: "" });
append({ key: "", value: "" }, false);
}
}, [fields]);