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): field key unique check for fields in group #1308

Merged
merged 2 commits into from
Nov 14, 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
4 changes: 2 additions & 2 deletions web/e2e/project/item/fields/group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test("Group field creating and updating has succeeded", async ({ page }) => {
await page.getByLabel("Settings").locator("#key").fill("group1");
await page.getByLabel("Settings").locator("#description").click();
await page.getByLabel("Settings").locator("#description").fill("group1 description");
await page.getByLabel("Select Group").click();
await page.locator(".ant-select-selector").click();
await page.getByText("e2e group name #e2e-group-key").click();
await expect(page.getByLabel("Settings")).toContainText("e2e group name #e2e-group-key");
await page.getByRole("tab", { name: "Validation" }).click();
Expand Down Expand Up @@ -171,7 +171,7 @@ test("Group field editing has succeeded", async ({ page }) => {
await page.getByLabel("Settings").locator("#key").fill("group1");
await page.getByLabel("Settings").locator("#description").click();
await page.getByLabel("Settings").locator("#description").fill("group1 description");
await page.getByLabel("Select Group").click();
await page.locator(".ant-select-selector").click();
await page.getByText("e2e group name #e2e-group-key").click();
await expect(page.getByLabel("Settings")).toContainText("e2e group name #e2e-group-key");
await page.getByRole("tab", { name: "Validation" }).click();
Expand Down
2 changes: 1 addition & 1 deletion web/e2e/project/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ test("Group creating from adding field has succeeded", async ({ page }) => {
await page.getByText("e2e model name").click();
await page.locator("li").getByText("Group", { exact: true }).click();
await expect(page.getByText("Create Group Field")).toBeVisible();
await page.getByLabel("Select Group").click();
await page.locator(".ant-select-selector").click();
caichi-t marked this conversation as resolved.
Show resolved Hide resolved
await expect(page.getByText("e2e group name #e2e-group-key")).toBeVisible();
await page.getByRole("button", { name: "Cancel" }).click();
});
Expand Down
3 changes: 3 additions & 0 deletions web/src/components/molecules/Schema/FieldModal/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export default (
if (form.getFieldValue("title") && form.getFieldValue("key")) {
if (
form.getFieldValue("values")?.length === 0 ||
form.getFieldValue("group")?.length === 0 ||
form.getFieldValue("supportedTypes")?.length === 0 ||
form.getFieldValue("tags")?.length === 0
) {
Expand Down Expand Up @@ -370,6 +371,8 @@ export default (
if (open && !selectedField) {
if (selectedType === "Select") {
form.setFieldValue("values", []);
} else if (selectedType === "Group") {
form.setFieldValue("group", "");
caichi-t marked this conversation as resolved.
Show resolved Hide resolved
} else if (selectedType === "GeometryObject") {
form.setFieldValue("supportedTypes", []);
} else if (selectedType === "GeometryEditor") {
Expand Down
44 changes: 24 additions & 20 deletions web/src/components/organisms/Project/Schema/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
FieldType,
Group,
SelectedSchemaType,
Schema,
MetaDataSchema,
} from "@reearth-cms/components/molecules/Schema/types";
import type { FormValues, ModelFormValues } from "@reearth-cms/components/molecules/Schema/types";
import { fromGraphQLModel } from "@reearth-cms/components/organisms/DataConverters/model";
Expand Down Expand Up @@ -49,6 +51,10 @@ export default () => {
const hasUpdateRight = useMemo(() => !!userRights?.schema.update, [userRights?.schema.update]);
const hasDeleteRight = useMemo(() => !!userRights?.schema.delete, [userRights?.schema.delete]);

const [modelModalShown, setModelModalShown] = useState(false);
const [modelDeletionModalShown, setModelDeletionModalShown] = useState(false);
const [groupModalShown, setGroupModalShown] = useState(false);
const [groupDeletionModalShown, setGroupDeletionModalShown] = useState(false);
const [fieldModalShown, setFieldModalShown] = useState(false);
const [isMeta, setIsMeta] = useState(false);
const [selectedField, setSelectedField] = useState<Field | null>(null);
Expand Down Expand Up @@ -114,6 +120,13 @@ export default () => {
[group],
);

const isGroup = useMemo(
() => groupModalShown || selectedSchemaType === "group",
[groupModalShown, selectedSchemaType],
);

const data = useMemo(() => (isGroup ? group : currentModel), [currentModel, group, isGroup]);

useEffect(() => {
if (!schemaId && currentModel) {
navigate(`/workspace/${workspaceId}/project/${projectId}/schema/${currentModel.id}`);
Expand All @@ -135,23 +148,27 @@ export default () => {
);

const keyUniqueCheck = useCallback(
(key: string, fieldId?: string, model?: Model) => {
const schema = isMeta ? model?.metadataSchema : model?.schema;
(key: string, fieldId?: string, schema?: Schema | MetaDataSchema) => {
const sameKeyField = schema?.fields?.find(field => field.key === key);
return !sameKeyField || sameKeyField.id === fieldId;
},
[isMeta],
[],
);

const handleFieldKeyUnique = useCallback(
(key: string) => keyUniqueCheck(key, selectedField?.id, currentModel),
[keyUniqueCheck, selectedField?.id, currentModel],
(key: string) =>
keyUniqueCheck(key, selectedField?.id, isMeta ? currentModel?.metadataSchema : data?.schema),
[keyUniqueCheck, selectedField?.id, isMeta, currentModel?.metadataSchema, data?.schema],
);

const handleCorrespondingFieldKeyUnique = useCallback(
(key: string) =>
keyUniqueCheck(key, selectedField?.typeProperty?.correspondingField?.id, referencedModel),
[keyUniqueCheck, selectedField?.typeProperty?.correspondingField?.id, referencedModel],
keyUniqueCheck(
key,
selectedField?.typeProperty?.correspondingField?.id,
referencedModel?.schema,
),
[keyUniqueCheck, referencedModel?.schema, selectedField?.typeProperty?.correspondingField?.id],
);

const [createNewField, { loading: fieldCreationLoading }] = useCreateFieldMutation({
Expand Down Expand Up @@ -291,9 +308,6 @@ export default () => {
);

// group hooks
const [groupModalShown, setGroupModalShown] = useState(false);
const [groupDeletionModalShown, setGroupDeletionModalShown] = useState(false);

const handleGroupModalOpen = useCallback(() => setGroupModalShown(true), []);
const handleGroupModalClose = useCallback(() => setGroupModalShown(false), []);
const handleGroupDeletionModalOpen = useCallback(
Expand Down Expand Up @@ -436,9 +450,6 @@ export default () => {
);

// model hooks
const [modelModalShown, setModelModalShown] = useState(false);
const [modelDeletionModalShown, setModelDeletionModalShown] = useState(false);

const [CheckModelKeyAvailability] = useCheckModelKeyAvailabilityLazyQuery({
fetchPolicy: "no-cache",
});
Expand Down Expand Up @@ -510,13 +521,6 @@ export default () => {
[updateNewModel, handleModelModalClose, t],
);

const isGroup = useMemo(
() => groupModalShown || selectedSchemaType === "group",
[groupModalShown, selectedSchemaType],
);

const data = useMemo(() => (isGroup ? group : currentModel), [currentModel, group, isGroup]);

const handleKeyCheck = useCallback(
async (key: string, ignoredKey?: string) => {
if (!projectId || !key) return false;
Expand Down
Loading