From d6a2c0682c06ca605583b37928f50449f0ab6cef Mon Sep 17 00:00:00 2001 From: Ryo Okubo Date: Sat, 29 Apr 2023 19:28:14 +0900 Subject: [PATCH] Simplify EntityForm state --- .../src/components/entry/EntryForm.test.tsx | 11 +++- frontend/src/components/entry/EntryForm.tsx | 36 ++++++------- .../entryForm/AttributeValueField.test.tsx | 25 ++------- .../entry/entryForm/AttributeValueField.tsx | 52 +++++++------------ frontend/src/pages/EditEntryPage.tsx | 18 +++---- 5 files changed, 60 insertions(+), 82 deletions(-) diff --git a/frontend/src/components/entry/EntryForm.test.tsx b/frontend/src/components/entry/EntryForm.test.tsx index fb11d0e79..ecda4d69c 100644 --- a/frontend/src/components/entry/EntryForm.test.tsx +++ b/frontend/src/components/entry/EntryForm.test.tsx @@ -17,6 +17,15 @@ test("should render a component with essential props", function () { schema: { id: 0, name: "testEntity" }, attrs: {}, }; + const entity = { + id: 2, + name: "bbb", + note: "", + isToplevel: false, + attrs: [], + webhooks: [], + }; + const setIsAnchorLink = () => { /* do nothing */ }; @@ -27,7 +36,7 @@ test("should render a component with essential props", function () { }); return ( ({ })); export interface EntryFormProps { - // TODO simplify props more - entryInfo: Schema; + entity: EntityDetail; setIsAnchorLink: Dispatch>; control: Control; setValue: UseFormSetValue; } export const EntryForm: FC = ({ - entryInfo, + entity, setIsAnchorLink, control, setValue, @@ -82,14 +83,14 @@ export const EntryForm: FC = ({ - {Object.entries(entryInfo.attrs).map(([attrId, attrValue]) => ( - + {entity.attrs.map(({ id, name }) => ( + setIsAnchorLink(true)} > - {attrValue.schema.name} + {name} @@ -134,30 +135,27 @@ export const EntryForm: FC = ({ /> - {Object.entries(entryInfo.attrs) - .sort((a, b) => a[1].index - b[1].index) - .map(([attrId, attrValue]) => ( - + {entity.attrs + .sort((a, b) => a.index - b.index) + .map(({ id, name, type, isMandatory }) => ( + {/* an anchor link adjusted fixed headers etc. */} - - {attrValue.schema.name} - - {attrValue.isMandatory && ( - 必須 - )} + {name} + {isMandatory && 必須} diff --git a/frontend/src/components/entry/entryForm/AttributeValueField.test.tsx b/frontend/src/components/entry/entryForm/AttributeValueField.test.tsx index 68f6fbefa..87ce66537 100644 --- a/frontend/src/components/entry/entryForm/AttributeValueField.test.tsx +++ b/frontend/src/components/entry/entryForm/AttributeValueField.test.tsx @@ -9,7 +9,6 @@ import { DjangoContext } from "../../../services/DjangoContext"; import { ReactHookFormTestWrapper } from "../../../services/ReactHookFormTestWrapper"; import { AttributeValueField } from "./AttributeValueField"; -import { EditableEntryAttrValue } from "./EditableEntry"; import { Schema } from "./EntryFormSchema"; beforeAll(() => { @@ -120,16 +119,8 @@ attributes.forEach((attribute) => { )} /> @@ -157,16 +148,8 @@ arrayAttributes.forEach((arrayAttribute) => { )} /> diff --git a/frontend/src/components/entry/entryForm/AttributeValueField.tsx b/frontend/src/components/entry/entryForm/AttributeValueField.tsx index 1294a731d..fb5dcbfbb 100644 --- a/frontend/src/components/entry/entryForm/AttributeValueField.tsx +++ b/frontend/src/components/entry/entryForm/AttributeValueField.tsx @@ -4,7 +4,6 @@ import { UseFormSetValue } from "react-hook-form/dist/types/form"; import { BooleanAttributeValueField } from "./BooleanAttributeValueField"; import { DateAttributeValueField } from "./DateAttributeValueField"; -import { EditableEntryAttrs } from "./EditableEntry"; import { Schema } from "./EntryFormSchema"; import { GroupAttributeValueField } from "./GroupAttributeValueField"; import { @@ -21,32 +20,29 @@ import { import { DjangoContext } from "services/DjangoContext"; interface Props { - attrInfo: EditableEntryAttrs; control: Control; setValue: UseFormSetValue; + type: number; + schemaId: number; } export const AttributeValueField: FC = ({ - attrInfo, control, setValue, + type, + schemaId, }) => { const djangoContext = DjangoContext.getInstance(); - switch (attrInfo.type) { + switch (type) { case djangoContext?.attrTypeValue.string: - return ( - - ); + return ; case djangoContext?.attrTypeValue.text: return ( ); @@ -54,24 +50,19 @@ export const AttributeValueField: FC = ({ case djangoContext?.attrTypeValue.date: return ( ); case djangoContext?.attrTypeValue.boolean: - return ( - - ); + return ; case djangoContext?.attrTypeValue.object: return ( @@ -80,7 +71,7 @@ export const AttributeValueField: FC = ({ case djangoContext?.attrTypeValue.group: return ( @@ -89,7 +80,7 @@ export const AttributeValueField: FC = ({ case djangoContext?.attrTypeValue.role: return ( @@ -98,7 +89,7 @@ export const AttributeValueField: FC = ({ case djangoContext?.attrTypeValue.named_object: return ( @@ -107,7 +98,7 @@ export const AttributeValueField: FC = ({ case djangoContext?.attrTypeValue.array_object: return ( = ({ case djangoContext?.attrTypeValue.array_group: return ( = ({ case djangoContext?.attrTypeValue.array_role: return ( = ({ case djangoContext?.attrTypeValue.array_string: return ( - + ); case djangoContext?.attrTypeValue.array_named_object: return ( @@ -154,7 +142,7 @@ export const AttributeValueField: FC = ({ case djangoContext?.attrTypeValue.array_named_object_boolean: return ( = ({ ); default: - throw new Error(`Unknown attribute type: ${attrInfo.type}`); + throw new Error(`Unknown attribute type: ${type}`); } }; diff --git a/frontend/src/pages/EditEntryPage.tsx b/frontend/src/pages/EditEntryPage.tsx index 96fedf7a9..06c8fa3e9 100644 --- a/frontend/src/pages/EditEntryPage.tsx +++ b/frontend/src/pages/EditEntryPage.tsx @@ -44,8 +44,8 @@ export const EditEntryPage: FC = ({ const history = useHistory(); const { enqueueSubmitResult } = useFormNotification("エントリ", willCreate); - const [entryInfo, setEntryInfo] = useState(); const [isAnchorLink, setIsAnchorLink] = useState(false); + const [initialized, setInitialized] = useState(false); const { formState: { isValid, isDirty, isSubmitting, isSubmitSuccessful }, @@ -72,13 +72,13 @@ export const EditEntryPage: FC = ({ useEffect(() => { if (willCreate) { if (!entity.loading && entity.value != null) { - const _entryInfo = formalizeEntryInfo( + const entryInfo = formalizeEntryInfo( undefined, entity.value, excludeAttrs ); - reset(_entryInfo); - setEntryInfo(_entryInfo); + reset(entryInfo); + setInitialized(true); } } else { if ( @@ -87,13 +87,13 @@ export const EditEntryPage: FC = ({ !entry.loading && entry.value != null ) { - const _entryInfo = formalizeEntryInfo( + const entryInfo = formalizeEntryInfo( entry.value, entity.value, excludeAttrs ); - reset(_entryInfo); - setEntryInfo(_entryInfo); + reset(entryInfo); + setInitialized(true); } } }, [willCreate, entity.value, entry.value]); @@ -175,9 +175,9 @@ export const EditEntryPage: FC = ({ /> - {entryInfo && ( + {initialized && entity.value != null && (