Add metadata editor to attribute creator
This commit is contained in:
parent
4391e79a90
commit
ca78ecc19f
2 changed files with 48 additions and 27 deletions
|
@ -82,9 +82,9 @@ const AttributePage: React.FC<AttributePageProps> = ({
|
||||||
filterableInDashboard: true,
|
filterableInDashboard: true,
|
||||||
filterableInStorefront: true,
|
filterableInStorefront: true,
|
||||||
inputType: AttributeInputTypeEnum.DROPDOWN,
|
inputType: AttributeInputTypeEnum.DROPDOWN,
|
||||||
metadata: undefined,
|
metadata: [],
|
||||||
name: "",
|
name: "",
|
||||||
privateMetadata: undefined,
|
privateMetadata: [],
|
||||||
slug: "",
|
slug: "",
|
||||||
storefrontSearchPosition: "",
|
storefrontSearchPosition: "",
|
||||||
valueRequired: true,
|
valueRequired: true,
|
||||||
|
@ -119,8 +119,14 @@ const AttributePage: React.FC<AttributePageProps> = ({
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = (data: AttributePageFormData) => {
|
const handleSubmit = (data: AttributePageFormData) => {
|
||||||
const metadata = isMetadataModified ? data.metadata : undefined;
|
const metadata = !attribute
|
||||||
const privateMetadata = isPrivateMetadataModified
|
? data.metadata
|
||||||
|
: isMetadataModified
|
||||||
|
? data.metadata
|
||||||
|
: undefined;
|
||||||
|
const privateMetadata = !attribute
|
||||||
|
? data.privateMetadata
|
||||||
|
: isPrivateMetadataModified
|
||||||
? data.privateMetadata
|
? data.privateMetadata
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
|
@ -170,12 +176,8 @@ const AttributePage: React.FC<AttributePageProps> = ({
|
||||||
onValueReorder={onValueReorder}
|
onValueReorder={onValueReorder}
|
||||||
onValueUpdate={onValueUpdate}
|
onValueUpdate={onValueUpdate}
|
||||||
/>
|
/>
|
||||||
{!!attribute && (
|
<CardSpacer />
|
||||||
<>
|
<Metadata data={data} onChange={changeMetadata} />
|
||||||
<CardSpacer />
|
|
||||||
<Metadata data={data} onChange={changeMetadata} />
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<AttributeProperties
|
<AttributeProperties
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { getStringOrPlaceholder } from "@saleor/misc";
|
||||||
import { ReorderEvent } from "@saleor/types";
|
import { ReorderEvent } from "@saleor/types";
|
||||||
import { ProductErrorCode } from "@saleor/types/globalTypes";
|
import { ProductErrorCode } from "@saleor/types/globalTypes";
|
||||||
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
|
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
|
||||||
|
import createMetadataCreateHandler from "@saleor/utils/handlers/metadataCreateHandler";
|
||||||
import {
|
import {
|
||||||
add,
|
add,
|
||||||
isSelected,
|
isSelected,
|
||||||
|
@ -12,11 +13,17 @@ import {
|
||||||
remove,
|
remove,
|
||||||
updateAtIndex
|
updateAtIndex
|
||||||
} from "@saleor/utils/lists";
|
} from "@saleor/utils/lists";
|
||||||
|
import {
|
||||||
|
useMetadataUpdate,
|
||||||
|
usePrivateMetadataUpdate
|
||||||
|
} from "@saleor/utils/metadata/updateMetadata";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { useIntl } from "react-intl";
|
import { useIntl } from "react-intl";
|
||||||
import slugify from "slugify";
|
import slugify from "slugify";
|
||||||
|
|
||||||
import AttributePage from "../../components/AttributePage";
|
import AttributePage, {
|
||||||
|
AttributePageFormData
|
||||||
|
} from "../../components/AttributePage";
|
||||||
import AttributeValueDeleteDialog from "../../components/AttributeValueDeleteDialog";
|
import AttributeValueDeleteDialog from "../../components/AttributeValueDeleteDialog";
|
||||||
import AttributeValueEditDialog, {
|
import AttributeValueEditDialog, {
|
||||||
AttributeValueEditDialogFormData
|
AttributeValueEditDialogFormData
|
||||||
|
@ -72,6 +79,8 @@ const AttributeDetails: React.FC<AttributeDetailsProps> = ({ params }) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
const [updateMetadata] = useMetadataUpdate({});
|
||||||
|
const [updatePrivateMetadata] = usePrivateMetadataUpdate({});
|
||||||
|
|
||||||
const id = params.id ? parseInt(params.id, 0) : undefined;
|
const id = params.id ? parseInt(params.id, 0) : undefined;
|
||||||
|
|
||||||
|
@ -105,6 +114,31 @@ const AttributeDetails: React.FC<AttributeDetailsProps> = ({ params }) => {
|
||||||
const handleValueReorder = ({ newIndex, oldIndex }: ReorderEvent) =>
|
const handleValueReorder = ({ newIndex, oldIndex }: ReorderEvent) =>
|
||||||
setValues(move(values[oldIndex], values, areValuesEqual, newIndex));
|
setValues(move(values[oldIndex], values, areValuesEqual, newIndex));
|
||||||
|
|
||||||
|
const handleCreate = async (data: AttributePageFormData) => {
|
||||||
|
const input = {
|
||||||
|
...data,
|
||||||
|
metadata: undefined,
|
||||||
|
privateMetadata: undefined,
|
||||||
|
storefrontSearchPosition: parseInt(data.storefrontSearchPosition, 0),
|
||||||
|
values: values.map(value => ({
|
||||||
|
name: value.name
|
||||||
|
}))
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await attributeCreate({
|
||||||
|
variables: {
|
||||||
|
input
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return result.data.attributeCreate?.attribute?.id || null;
|
||||||
|
};
|
||||||
|
const handleSubmit = createMetadataCreateHandler(
|
||||||
|
handleCreate,
|
||||||
|
updateMetadata,
|
||||||
|
updatePrivateMetadata
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<AttributePage
|
<AttributePage
|
||||||
|
@ -113,22 +147,7 @@ const AttributeDetails: React.FC<AttributeDetailsProps> = ({ params }) => {
|
||||||
errors={attributeCreateOpts.data?.attributeCreate.errors || []}
|
errors={attributeCreateOpts.data?.attributeCreate.errors || []}
|
||||||
onBack={() => navigate(attributeListUrl())}
|
onBack={() => navigate(attributeListUrl())}
|
||||||
onDelete={undefined}
|
onDelete={undefined}
|
||||||
onSubmit={input =>
|
onSubmit={handleSubmit}
|
||||||
attributeCreate({
|
|
||||||
variables: {
|
|
||||||
input: {
|
|
||||||
...input,
|
|
||||||
storefrontSearchPosition: parseInt(
|
|
||||||
input.storefrontSearchPosition,
|
|
||||||
0
|
|
||||||
),
|
|
||||||
values: values.map(value => ({
|
|
||||||
name: value.name
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
onValueAdd={() => openModal("add-value")}
|
onValueAdd={() => openModal("add-value")}
|
||||||
onValueDelete={id =>
|
onValueDelete={id =>
|
||||||
openModal("remove-value", {
|
openModal("remove-value", {
|
||||||
|
|
Loading…
Reference in a new issue