Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web): update the item with a new group field #1037

Merged
merged 8 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions web/src/components/molecules/Common/Form/GroupItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
const t = useT();

const fields = useMemo(() => group?.schema.fields, [group?.schema.fields]);
const itemGroupId = useMemo(() => value, [value]);
const itemGroupId = useMemo(() => value ?? "", [value]);

Check warning on line 121 in web/src/components/molecules/Common/Form/GroupItem/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Common/Form/GroupItem/index.tsx#L121

Added line #L121 was not covered by tests

return (
<Collapse collapsible="header" defaultActiveKey={["1"]} style={{ width: 500 }}>
Expand Down Expand Up @@ -154,7 +154,7 @@
message: t("Please input field!"),
},
]}
name={[field.id, itemGroupId ?? ""]}
name={[field.id, itemGroupId]}

Check warning on line 157 in web/src/components/molecules/Common/Form/GroupItem/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Common/Form/GroupItem/index.tsx#L157

Added line #L157 was not covered by tests
label={
<FieldTitle title={field.title} isUnique={field.unique} isTitle={field.isTitle} />
}>
Expand All @@ -179,7 +179,7 @@
message: t("Please input field!"),
},
]}
name={[field.id, itemGroupId ?? ""]}
name={[field.id, itemGroupId]}

Check warning on line 182 in web/src/components/molecules/Common/Form/GroupItem/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Common/Form/GroupItem/index.tsx#L182

Added line #L182 was not covered by tests
label={
<FieldTitle title={field.title} isUnique={field.unique} isTitle={field.isTitle} />
}>
Expand All @@ -202,7 +202,7 @@
message: t("Please input field!"),
},
]}
name={[field.id, itemGroupId ?? ""]}
name={[field.id, itemGroupId]}

Check warning on line 205 in web/src/components/molecules/Common/Form/GroupItem/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Common/Form/GroupItem/index.tsx#L205

Added line #L205 was not covered by tests
label={
<FieldTitle title={field.title} isUnique={field.unique} isTitle={field.isTitle} />
}>
Expand Down Expand Up @@ -231,7 +231,7 @@
message: t("Please input field!"),
},
]}
name={[field.id, itemGroupId ?? ""]}
name={[field.id, itemGroupId]}

Check warning on line 234 in web/src/components/molecules/Common/Form/GroupItem/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Common/Form/GroupItem/index.tsx#L234

Added line #L234 was not covered by tests
label={
<FieldTitle title={field.title} isUnique={field.unique} isTitle={field.isTitle} />
}>
Expand Down Expand Up @@ -288,7 +288,7 @@
<StyledFormItem
key={field.id}
extra={field.description}
name={[field.id, itemGroupId ?? ""]}
name={[field.id, itemGroupId]}

Check warning on line 291 in web/src/components/molecules/Common/Form/GroupItem/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Common/Form/GroupItem/index.tsx#L291

Added line #L291 was not covered by tests
label={
<FieldTitle title={field.title} isUnique={field.unique} isTitle={field.isTitle} />
}
Expand All @@ -314,7 +314,7 @@
<StyledFormItem
key={field.id}
extra={field.description}
name={[field.id, itemGroupId ?? ""]}
name={[field.id, itemGroupId]}

Check warning on line 317 in web/src/components/molecules/Common/Form/GroupItem/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Common/Form/GroupItem/index.tsx#L317

Added line #L317 was not covered by tests
label={
<FieldTitle title={field.title} isUnique={field.unique} isTitle={field.isTitle} />
}
Expand All @@ -334,7 +334,7 @@
<StyledFormItem
key={field.id}
extra={field.description}
name={[field.id, itemGroupId ?? ""]}
name={[field.id, itemGroupId]}

Check warning on line 337 in web/src/components/molecules/Common/Form/GroupItem/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Common/Form/GroupItem/index.tsx#L337

Added line #L337 was not covered by tests
valuePropName="checked"
label={
<FieldTitle title={field.title} isUnique={field.unique} isTitle={field.isTitle} />
Expand All @@ -345,7 +345,7 @@
<StyledFormItem
key={field.id}
extra={field.description}
name={[field.id, itemGroupId ?? ""]}
name={[field.id, itemGroupId]}

Check warning on line 348 in web/src/components/molecules/Common/Form/GroupItem/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Common/Form/GroupItem/index.tsx#L348

Added line #L348 was not covered by tests
label={<FieldTitle title={field.title} isUnique={field.unique} isTitle={false} />}>
<ReferenceFormItem
key={field.id}
Expand All @@ -364,7 +364,7 @@
<StyledFormItem
key={field.id}
extra={field.description}
name={[field.id, itemGroupId ?? ""]}
name={[field.id, itemGroupId]}

Check warning on line 367 in web/src/components/molecules/Common/Form/GroupItem/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Common/Form/GroupItem/index.tsx#L367

Added line #L367 was not covered by tests
label={
<FieldTitle title={field.title} isUnique={field.unique} isTitle={field.isTitle} />
}
Expand Down Expand Up @@ -411,7 +411,7 @@
message: t("Please input field!"),
},
]}
name={[field.id, itemGroupId ?? ""]}
name={[field.id, itemGroupId]}

Check warning on line 414 in web/src/components/molecules/Common/Form/GroupItem/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Common/Form/GroupItem/index.tsx#L414

Added line #L414 was not covered by tests
label={
<FieldTitle title={field.title} isUnique={field.unique} isTitle={field.isTitle} />
}>
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/molecules/Content/Details/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
setUploadType: (type: UploadType) => void;
onItemCreate: (data: {
schemaId: string;
metaSchemaId: string;
metaSchemaId?: string;

Check warning on line 74 in web/src/components/molecules/Content/Details/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Details/index.tsx#L74

Added line #L74 was not covered by tests
fields: ItemField[];
metaFields: ItemField[];
}) => Promise<void>;
Expand Down
144 changes: 89 additions & 55 deletions web/src/components/molecules/Content/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@
import LinkItemRequestModal from "@reearth-cms/components/molecules/Content/LinkItemRequestModal/LinkItemRequestModal";
import PublishItemModal from "@reearth-cms/components/molecules/Content/PublishItemModal";
import RequestCreationModal from "@reearth-cms/components/molecules/Content/RequestCreationModal";
import { Item, FormItem, ItemField } from "@reearth-cms/components/molecules/Content/types";
import {
Item,
FormItem,
ItemField,
ItemValue,
} from "@reearth-cms/components/molecules/Content/types";

Check warning on line 43 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L38-L43

Added lines #L38 - L43 were not covered by tests
import { Request, RequestState } from "@reearth-cms/components/molecules/Request/types";
import { FieldType, Group, Model } from "@reearth-cms/components/molecules/Schema/types";
import { FieldType, Group, Model, Field } from "@reearth-cms/components/molecules/Schema/types";

Check warning on line 45 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L45

Added line #L45 was not covered by tests
import { Member } from "@reearth-cms/components/molecules/Workspace/types";
import {
AssetSortType,
Expand Down Expand Up @@ -96,7 +101,7 @@
setUploadType: (type: UploadType) => void;
onItemCreate: (data: {
schemaId: string;
metaSchemaId: string;
metaSchemaId?: string;

Check warning on line 104 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L104

Added line #L104 was not covered by tests
fields: ItemField[];
metaFields: ItemField[];
}) => Promise<void>;
Expand Down Expand Up @@ -201,10 +206,32 @@
currentLocation.pathname !== nextLocation.pathname && changedKeys.current.size > 0,
);

const checkIfSingleGroupField = useCallback(
(key: string, value: any) => {
return (
initialFormValues[key] &&
typeof value === "object" &&
!Array.isArray(value) &&
value !== null
);
},
[initialFormValues],
);

Check warning on line 220 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L209-L220

Added lines #L209 - L220 were not covered by tests
const handleValuesChange = useCallback(
(changedValues: any) => {
const [key, value] = Object.entries(changedValues)[0];
if (
if (checkIfSingleGroupField(key, value)) {
const [groupFieldKey, groupFieldValue] = Object.entries(initialFormValues[key])[0];
const changedFieldValue = (value as any)[groupFieldKey];
if (changedFieldValue && groupFieldValue !== null) {
if (JSON.stringify(changedFieldValue) === JSON.stringify(groupFieldValue)) {
changedKeys.current.delete(key);
} else {
changedKeys.current.add(key);
}
}
} else if (

Check warning on line 234 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L224-L234

Added lines #L224 - L234 were not covered by tests
(!value && !initialFormValues[key]) ||
JSON.stringify(value) === JSON.stringify(initialFormValues[key])
) {
Expand All @@ -215,7 +242,7 @@
changedKeys.current.add(key);
}
},
[initialFormValues],
[checkIfSingleGroupField, initialFormValues],

Check warning on line 245 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L245

Added line #L245 was not covered by tests
);

useEffect(() => {
Expand Down Expand Up @@ -287,75 +314,81 @@
[formItemsData],
);

const inputValueGet = useCallback((value: ItemValue, multiple: boolean) => {
if (multiple) {
if (Array.isArray(value)) {
return value.map(v => (moment.isMoment(v) ? transformMomentToString(v) : v));
} else {
return [];
}
} else {
return moment.isMoment(value) ? transformMomentToString(value) : value ?? "";
}
}, []);

Check warning on line 328 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L317-L328

Added lines #L317 - L328 were not covered by tests
const handleSubmit = useCallback(async () => {
try {
const modelFieldTypes = new Map(
(model?.schema.fields || []).map(field => [field.id, field.type]),
);
const modelFields = new Map((model?.schema.fields || []).map(field => [field.id, field]));

Check warning on line 331 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L331

Added line #L331 was not covered by tests
const groupIdsInCurrentModel = new Set();
model?.schema.fields?.forEach(field => {
if (field.type === "Group") groupIdsInCurrentModel.add(field.typeProperty?.groupId);
});
const groupFieldTypes = new Map();
const groupFields = new Map<string, Field>();

Check warning on line 336 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L336

Added line #L336 was not covered by tests
groups
?.filter(group => groupIdsInCurrentModel.has(group.id))
.forEach(group => {
group?.schema.fields?.forEach(field => groupFieldTypes.set(field.id, field.type));
group?.schema.fields?.forEach(field => groupFields.set(field.id, field));

Check warning on line 340 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L340

Added line #L340 was not covered by tests
});
const values = await form.validateFields();
const metaValues = await metaForm.validateFields();
const fields: {
schemaFieldId: string;
itemGroupId?: string;
type: FieldType;
value: string;
}[] = [];
const metaFields: { schemaFieldId: string; type: FieldType; value: string }[] = [];
// TODO: improve performance
const fields: ItemField[] = [];

Check warning on line 343 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L343

Added line #L343 was not covered by tests
for (const [key, value] of Object.entries(values)) {
const isGroup =
typeof value === "object" && !Array.isArray(value) && !moment.isMoment(value);
// group fields
if (value && isGroup) {
for (const [key1, value1] of Object.entries(value)) {
const type1 = groupFieldTypes.get(key) || "";
fields.push({
value: (moment.isMoment(value1)
? transformMomentToString(value1)
: value1 ?? "") as string,
schemaFieldId: key,
itemGroupId: key1,
type: type1 as FieldType,
});
const modelField = modelFields.get(key);
if (modelField) {
fields.push({
value: inputValueGet(value as ItemValue, modelField.multiple),
schemaFieldId: key,
type: modelField.type,
});
} else if (typeof value === "object" && value !== null) {
for (const [groupFieldKey, groupFieldValue] of Object.entries(value)) {
const groupField = groupFields.get(key);
if (groupField) {
fields.push({
value: inputValueGet(groupFieldValue, groupField.multiple),
schemaFieldId: key,
itemGroupId: groupFieldKey,
type: groupField.type,
});
}

Check warning on line 362 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L345-L362

Added lines #L345 - L362 were not covered by tests
}
continue;
}
// model fields
const type = modelFieldTypes.get(key) || "";
fields.push({
value: (moment.isMoment(value) ? transformMomentToString(value) : value ?? "") as string,
schemaFieldId: key,
type: type as FieldType,
});
}

const metaValues = await metaForm.validateFields();
const metaFields: ItemField[] = [];

Check warning on line 368 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L366-L368

Added lines #L366 - L368 were not covered by tests
for (const [key, value] of Object.entries(metaValues)) {
metaFields.push({
value: (moment.isMoment(value) ? transformMomentToString(value) : value ?? "") as string,
schemaFieldId: key,
type: model?.metadataSchema?.fields?.find(field => field.id === key)?.type as FieldType,
});
const type = model?.metadataSchema?.fields?.find(field => field.id === key)?.type;
if (type) {
metaFields.push({
value: moment.isMoment(value) ? transformMomentToString(value) : value ?? "",
schemaFieldId: key,
type,
});
}

Check warning on line 377 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L370-L377

Added lines #L370 - L377 were not covered by tests
}

Check warning on line 379 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L379

Added line #L379 was not covered by tests
changedKeys.current.clear();
if (!itemId) {
await onItemCreate?.({
schemaId: model?.schema.id as string,
metaSchemaId: model?.metadataSchema?.id as string,
metaFields,

if (itemId) {
await onItemUpdate?.({
itemId: itemId,

Check warning on line 384 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L381-L384

Added lines #L381 - L384 were not covered by tests
fields,
});
} else {
await onItemUpdate?.({
itemId: itemId as string,
} else if (model?.schema.id) {
await onItemCreate?.({
schemaId: model?.schema.id,
metaSchemaId: model?.metadataSchema?.id,
metaFields,

Check warning on line 391 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L387-L391

Added lines #L387 - L391 were not covered by tests
fields,
});
}
Expand All @@ -371,8 +404,9 @@
form,
metaForm,
itemId,
onItemCreate,
inputValueGet,

Check warning on line 407 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L407

Added line #L407 was not covered by tests
onItemUpdate,
onItemCreate,

Check warning on line 409 in web/src/components/molecules/Content/Form/index.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/components/molecules/Content/Form/index.tsx#L409

Added line #L409 was not covered by tests
]);

const handleMetaUpdate = useCallback(async () => {
Expand Down
Loading
Loading