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): updating metadata added later from edit page #1125

Merged
merged 4 commits into from
Apr 15, 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
41 changes: 41 additions & 0 deletions web/e2e/project/item/metadata/update.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { closeNotification } from "@reearth-cms/e2e/common/notification";
import { handleFieldForm } from "@reearth-cms/e2e/project/utils/field";
import { createModel } from "@reearth-cms/e2e/project/utils/model";
import { createProject, deleteProject } from "@reearth-cms/e2e/project/utils/project";
import { expect, test } from "@reearth-cms/e2e/utils";

test.beforeEach(async ({ reearth, page }) => {
await reearth.goto("/", { waitUntil: "domcontentloaded" });
await createProject(page);
await createModel(page);
await page.locator("li").filter({ hasText: "Text" }).locator("div").first().click();
await handleFieldForm(page, "text");
await page.getByText("Content").click();
await page.getByRole("button", { name: "plus New Item" }).click();
await page.getByRole("button", { name: "Save" }).click();
await closeNotification(page);
await page.getByText("Schema").click();
await page.getByRole("tab", { name: "Meta Data" }).click();
await page.locator("li").filter({ hasText: "Boolean" }).locator("div").first().click();
await handleFieldForm(page, "boolean");
await page.getByText("Content").click();
});

test.afterEach(async ({ page }) => {
await deleteProject(page);
});

test("Updating metadata added later from table has succeeded", async ({ page }) => {
await page.getByRole("switch").click();
await closeNotification(page);
await page.getByRole("link", { name: "edit", exact: true }).click();
await expect(page.getByLabel("boolean")).toHaveAttribute("aria-checked", "true");
});

test("Updating metadata added later from edit page has succeeded", async ({ page }) => {
await page.getByRole("link", { name: "edit", exact: true }).click();
await page.getByLabel("boolean").click();
await closeNotification(page);
await page.getByLabel("Back").click();
await expect(page.getByRole("switch")).toHaveAttribute("aria-checked", "true");
});
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 @@ -78,7 +78,7 @@ type Props = {
metaFields: ItemField[];
}) => Promise<void>;
onItemUpdate: (data: { itemId: string; fields: ItemField[] }) => Promise<void>;
onMetaItemUpdate: (data: { metaItemId: string; metaFields: ItemField[] }) => Promise<void>;
onMetaItemUpdate: (data: { metaItemId?: string; metaFields: ItemField[] }) => Promise<void>;
onBack: (modelId?: string) => void;
onAssetsCreate: (files: UploadFile[]) => Promise<(Asset | undefined)[]>;
onAssetCreateFromUrl: (url: string, autoUnzip: boolean) => Promise<Asset | undefined>;
Expand Down
8 changes: 4 additions & 4 deletions web/src/components/molecules/Content/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ interface Props {
metaFields: ItemField[];
}) => Promise<void>;
onItemUpdate: (data: { itemId: string; fields: ItemField[] }) => Promise<void>;
onMetaItemUpdate: (data: { metaItemId: string; metaFields: ItemField[] }) => Promise<void>;
onMetaItemUpdate: (data: { metaItemId?: string; metaFields: ItemField[] }) => Promise<void>;
onBack: (modelId?: string) => void;
onAssetsCreate: (files: UploadFile[]) => Promise<(Asset | undefined)[]>;
onAssetCreateFromUrl: (url: string, autoUnzip: boolean) => Promise<Asset | undefined>;
Expand Down Expand Up @@ -403,7 +403,7 @@ const ContentForm: React.FC<Props> = ({
}, [model, form, metaForm, itemId, onGroupGet, inputValueGet, onItemUpdate, onItemCreate]);

const handleMetaUpdate = useCallback(async () => {
if (!itemId || !item?.metadata?.id) return;
if (!itemId) return;
try {
const metaValues = await metaForm.validateFields();
const metaFields: { schemaFieldId: string; type: FieldType; value: string }[] = [];
Expand All @@ -414,8 +414,8 @@ const ContentForm: React.FC<Props> = ({
type: model?.metadataSchema?.fields?.find(field => field.id === key)?.type as FieldType,
});
}
await onMetaItemUpdate?.({
metaItemId: item.metadata.id,
await onMetaItemUpdate({
metaItemId: item?.metadata?.id,
metaFields,
});
} catch (info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,21 +289,58 @@ export default () => {
);

const handleMetaItemUpdate = useCallback(
async ({ metaItemId, metaFields }: { metaItemId: string; metaFields: ItemField[] }) => {
const item = await updateItem({
variables: {
itemId: metaItemId,
fields: metaFields as ItemFieldInput[],
version: currentItem?.metadata?.version ?? "",
},
});
if (item.errors || !item.data?.updateItem) {
async ({ metaItemId, metaFields }: { metaItemId?: string; metaFields: ItemField[] }) => {
if (metaItemId) {
const item = await updateItem({
variables: {
itemId: metaItemId,
fields: metaFields as ItemFieldInput[],
version: currentItem?.metadata?.version ?? "",
},
});
if (item.errors || !item.data?.updateItem) {
Notification.error({ message: t("Failed to update item.") });
return;
}
} else if (
currentItem &&
currentItem.fields &&
currentModel?.id &&
currentModel.metadataSchema.id
) {
const metaItem = await createItem({
variables: {
modelId: currentModel.id,
schemaId: currentModel.metadataSchema.id,
fields: metaFields as ItemFieldInput[],
},
});
if (metaItem.errors || !metaItem.data?.createItem) {
Notification.error({ message: t("Failed to update item.") });
return;
}
const item = await updateItem({
variables: {
itemId: currentItem.id,
fields: currentItem.fields.map(field => ({
...field,
value: field.value ?? "",
})) as ItemFieldInput[],
metadataId: metaItem?.data.createItem.item.id,
version: currentItem.version,
},
});
if (item.errors || !item.data?.updateItem) {
Notification.error({ message: t("Failed to update item.") });
return;
}
} else {
Notification.error({ message: t("Failed to update item.") });
return;
}
Notification.success({ message: t("Successfully updated Item!") });
},
[updateItem, currentItem?.metadata?.version, t],
[createItem, currentItem, currentModel?.id, currentModel?.metadataSchema.id, t, updateItem],
);

const dateConvert = useCallback((value?: ItemValue) => {
Expand Down
Loading