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): the logic to update metadata from edit page #1335

Merged
merged 2 commits into from
Dec 13, 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 @@ -115,7 +115,7 @@ const MultiValueField: React.FC<Props> = ({
type="primary"
onClick={() => {
const currentValues = value || [];
const defaultValue = props.type === "date" ? dayjs() : "";
const defaultValue = "";
caichi-t marked this conversation as resolved.
Show resolved Hide resolved
if (Array.isArray(currentValues)) {
onChange?.([...currentValues, defaultValue]);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ import FieldTitle from "../../FieldTitle";
type BoolFieldProps = {
field: Field;
itemGroupId?: string;
onMetaUpdate?: () => void;
disabled: boolean;
};

const BoolField: React.FC<BoolFieldProps> = ({ field, itemGroupId, onMetaUpdate, disabled }) => {
const BoolField: React.FC<BoolFieldProps> = ({ field, itemGroupId, disabled }) => {
return (
<Form.Item
extra={field.description}
name={itemGroupId ? [field.id, itemGroupId] : field.id}
valuePropName="checked"
label={<FieldTitle title={field.title} isUnique={field.unique} isTitle={field.isTitle} />}>
{field.multiple ? (
<MultiValueBooleanField onChange={onMetaUpdate} FieldInput={Switch} disabled={disabled} />
<MultiValueBooleanField FieldInput={Switch} disabled={disabled} />
) : (
<Switch onChange={onMetaUpdate} disabled={disabled} />
<Switch disabled={disabled} />
)}
</Form.Item>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ import FieldTitle from "../../FieldTitle";

type CheckboxFieldProps = {
field: Field;
onMetaUpdate?: () => void;
disabled: boolean;
};

const CheckboxField: React.FC<CheckboxFieldProps> = ({ field, onMetaUpdate, disabled }) => {
const CheckboxField: React.FC<CheckboxFieldProps> = ({ field, disabled }) => {
return (
<Form.Item
extra={field.description}
name={field.id}
valuePropName="checked"
label={<FieldTitle title={field.title} isUnique={field.unique} isTitle={false} />}>
{field.multiple ? (
<MultiValueBooleanField onChange={onMetaUpdate} FieldInput={Checkbox} disabled={disabled} />
<MultiValueBooleanField FieldInput={Checkbox} disabled={disabled} />
) : (
<Checkbox onChange={onMetaUpdate} disabled={disabled} />
<Checkbox disabled={disabled} />
)}
</Form.Item>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import { requiredValidator } from "../utils";
type DateFieldProps = {
field: Field;
itemGroupId?: string;
onMetaUpdate?: () => void;
disabled: boolean;
};

const DateField: React.FC<DateFieldProps> = ({ field, itemGroupId, onMetaUpdate, disabled }) => {
const DateField: React.FC<DateFieldProps> = ({ field, itemGroupId, disabled }) => {
const t = useT();

return (
Expand All @@ -32,14 +31,9 @@ const DateField: React.FC<DateFieldProps> = ({ field, itemGroupId, onMetaUpdate,
name={itemGroupId ? [field.id, itemGroupId] : field.id}
label={<FieldTitle title={field.title} isUnique={field.unique} isTitle={field.isTitle} />}>
{field.multiple ? (
<MultiValueField
onChange={onMetaUpdate}
type="date"
FieldInput={StyledDatePicker}
disabled={disabled}
/>
<MultiValueField type="date" FieldInput={StyledDatePicker} disabled={disabled} />
) : (
<StyledDatePicker onChange={onMetaUpdate} disabled={disabled} />
<StyledDatePicker disabled={disabled} />
)}
</Form.Item>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import FieldTitle from "../../FieldTitle";

type TagFieldProps = {
field: Field;
onMetaUpdate?: () => void;
disabled: boolean;
};

const TagField: React.FC<TagFieldProps> = ({ field, onMetaUpdate, disabled }) => {
const TagField: React.FC<TagFieldProps> = ({ field, disabled }) => {
const t = useT();

return (
Expand All @@ -30,7 +29,6 @@ const TagField: React.FC<TagFieldProps> = ({ field, onMetaUpdate, disabled }) =>
label={<FieldTitle title={field.title} isUnique={field.unique} isTitle={false} />}>
{field.multiple ? (
<StyledMultipleSelect
onChange={onMetaUpdate}
mode="multiple"
tagRender={props => <>{props.label}</>}
allowClear
Expand All @@ -42,7 +40,7 @@ const TagField: React.FC<TagFieldProps> = ({ field, onMetaUpdate, disabled }) =>
))}
</StyledMultipleSelect>
) : (
<Select onChange={onMetaUpdate} allowClear disabled={disabled}>
<Select allowClear disabled={disabled}>
{field.typeProperty?.tags?.map((tag: { id: string; name: string; color: string }) => (
<Select.Option key={tag.name} value={tag.id}>
<TagWrapper>
Expand Down
59 changes: 41 additions & 18 deletions web/src/components/molecules/Content/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ const ContentForm: React.FC<Props> = ({
]);

const handleMetaUpdate = useCallback(async () => {
if (!itemId) return;
try {
const metaFields = await metaFieldsGet();
await onMetaItemUpdate({
Expand All @@ -461,20 +460,48 @@ const ContentForm: React.FC<Props> = ({
} catch (info) {
console.log("Validate Failed:", info);
}
}, [itemId, metaFieldsGet, onMetaItemUpdate, item?.metadata?.id]);
}, [metaFieldsGet, onMetaItemUpdate, item?.metadata?.id]);

const handleMetaValuesChange = useCallback(async () => {
if (itemId) return;
try {
await metaForm.validateFields();
} catch (e) {
if ((e as ValidateErrorEntity).errorFields.length > 0) {
setIsDisabled(true);
return;
const timeout = useRef<ReturnType<typeof setTimeout> | null>(null);

const handleMetaValuesChange = useCallback(
async (changedValues: Record<string, unknown>) => {
if (itemId) {
if (timeout.current) {
clearTimeout(timeout.current);
timeout.current = null;
}
const [key, value] = Object.entries(changedValues)[0];
const initialValue = initialMetaFormValues[key];
if (Array.isArray(value)) {
// use checkIfEmpty
const noEmptyValuesLength = value.filter(
v => !(v === undefined || v === null || v === ""),
).length;
if (
noEmptyValuesLength === value.length ||
(noEmptyValuesLength && !initialValue) ||
(Array.isArray(initialValue) && noEmptyValuesLength !== initialValue.length)
) {
timeout.current = setTimeout(handleMetaUpdate, 800);
}
} else if (value !== initialValue) {
timeout.current = setTimeout(handleMetaUpdate, 800);
}
} else {
try {
await metaForm.validateFields();
} catch (e) {
if ((e as ValidateErrorEntity).errorFields.length > 0) {
setIsDisabled(true);
return;
}
}
setIsDisabled(false);
}
}
setIsDisabled(false);
}, [itemId, metaForm]);
},
[handleMetaUpdate, initialMetaFormValues, itemId, metaForm],
);

const items: MenuProps["items"] = useMemo(() => {
const menuItems = [
Expand Down Expand Up @@ -702,11 +729,7 @@ const ContentForm: React.FC<Props> = ({
const FieldComponent = FIELD_TYPE_COMPONENT_MAP[field.type];
return (
<MetaFormItemWrapper key={field.id}>
<FieldComponent
field={field}
onMetaUpdate={handleMetaUpdate}
disabled={fieldDisabled}
/>
<FieldComponent field={field} disabled={fieldDisabled} />
</MetaFormItemWrapper>
);
})}
Expand Down
Loading