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): toggle ok button on modal for updating field #1252

Merged
merged 5 commits into from
Oct 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@ const GeometryItem: React.FC<Props> = ({
: [GEO_TYPE_MAP[supportedTypes]];
if (convertedTypes.includes(valueJson.type)) {
handleErrorDelete();
} else {
handleErrorAdd();
return;
}
}
throw new Error();
} catch (_) {
return;
handleErrorAdd();
caichi-t marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
handleErrorDelete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ const MultiValueGeometry: React.FC<Props> = ({
return index !== key;
}),
);
errorDelete?.(key);
},
[onChange, value],
[errorDelete, onChange, value],
);

return (
Expand Down
41 changes: 37 additions & 4 deletions web/src/components/molecules/Schema/FieldModal/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import dayjs from "dayjs";
import { useCallback, useEffect, useMemo, useState, useRef } from "react";

import Form from "@reearth-cms/components/atoms/Form";
import { keyAutoFill, keyReplace } from "@reearth-cms/components/molecules/Common/Form/utils";
import {
keyAutoFill,
keyReplace,
emptyConvert,
} from "@reearth-cms/components/molecules/Common/Form/utils";
import {
Field,
FieldModalTabs,
Expand Down Expand Up @@ -118,9 +122,12 @@ export default (
}
}, []);

const changedKeys = useRef(new Set<string>());
const defaultValueRef = useRef<Partial<FormTypes>>();

useEffect(() => {
setMultipleValue(!!selectedField?.multiple);
form.setFieldsValue({
const defaultValue = {
fieldId: selectedField?.id,
title: selectedField?.title,
description: selectedField?.description,
Expand All @@ -139,7 +146,10 @@ export default (
supportedTypes:
selectedField?.typeProperty?.objectSupportedTypes ||
selectedField?.typeProperty?.editorSupportedTypes?.[0],
});
};
form.setFieldsValue(defaultValue);
defaultValueRef.current = defaultValue;
changedKeys.current.clear();
}, [defaultValueGet, form, selectedField]);

const typePropertyGet = useCallback((values: FormTypes) => {
Expand Down Expand Up @@ -230,14 +240,36 @@ export default (
} else {
form
.validateFields()
.then(() => setButtonDisabled(false))
.then(() => setButtonDisabled(changedKeys.current.size === 0))
.catch(() => setButtonDisabled(true));
}
} else {
setButtonDisabled(true);
}
}, [form, values]);

const handleValuesChange = useCallback(async (changedValues: Record<string, unknown>) => {
const [key, value] = Object.entries(changedValues)[0];
let changedValue = value;
let defaultValue = defaultValueRef.current?.[key as keyof FormTypes];
if (Array.isArray(value)) {
changedValue = [...value].sort();
}
if (Array.isArray(defaultValue)) {
defaultValue = [...defaultValue].sort();
}

if (
JSON.stringify(emptyConvert(changedValue)) === JSON.stringify(emptyConvert(defaultValue))
) {
caichi-t marked this conversation as resolved.
Show resolved Hide resolved
changedKeys.current.delete(key);
} else {
changedKeys.current.add(key);
}
},
[],
);
caichi-t marked this conversation as resolved.
Show resolved Hide resolved

const handleNameChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
if (selectedField) return;
Expand Down Expand Up @@ -357,6 +389,7 @@ export default (
multipleValue,
handleMultipleChange,
handleTabChange,
handleValuesChange,
handleNameChange,
handleKeyChange,
handleSubmit,
Expand Down
8 changes: 7 additions & 1 deletion web/src/components/molecules/Schema/FieldModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const FieldModal: React.FC<Props> = ({
multipleValue,
handleMultipleChange,
handleTabChange,
handleValuesChange,
handleNameChange,
handleKeyChange,
handleSubmit,
Expand Down Expand Up @@ -176,7 +177,12 @@ const FieldModal: React.FC<Props> = ({
{t("OK")}
</Button>,
]}>
<Form form={form} layout="vertical" initialValues={initialValues} requiredMark={requiredMark}>
<Form
form={form}
layout="vertical"
initialValues={initialValues}
requiredMark={requiredMark}
onValuesChange={handleValuesChange}>
<Tabs activeKey={activeTab} onChange={handleTabChange}>
<TabPane tab={t("Settings")} key="settings" forceRender>
<Form.Item
Expand Down
6 changes: 3 additions & 3 deletions web/src/components/molecules/Schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export type FieldTypePropertyInput = {
select?: { defaultValue: string; values: string[] };
tag?: {
defaultValue?: string;
tags: { color: string; id?: string; name: string }[];
tags: Tag[];
};
checkbox?: { defaultValue?: boolean };
integer?: { defaultValue: number | ""; min: number | null; max: number | null };
Expand Down Expand Up @@ -162,9 +162,9 @@ export type FormTypes = FormValues & {
values?: string[];
min?: number;
max?: number;
tags?: { color: string; id: string; name: string }[];
tags?: Tag[];
group: string;
supportedTypes?: string[] | string;
supportedTypes?: ObjectSupportedType[] | EditorSupportedType;
caichi-t marked this conversation as resolved.
Show resolved Hide resolved
};

export type Tab = "fields" | "meta-data";
Expand Down
Loading