Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add attribute creation

dominik-zeglen8526d41653b2
diff --git a/saleor/static/dashboard-next/attributes/index.tsx b/saleor/static/dashboard-next/attributes/index.tsx
index cedfe155d7..03798e10ba 100644
--- a/saleor/static/dashboard-next/attributes/index.tsx
+++ b/saleor/static/dashboard-next/attributes/index.tsx
@@ -6,11 +6,13 @@ import { WindowTitle } from "../components/WindowTitle";
import i18n from "../i18n";
import {
attributeAddPath,
+ AttributeAddUrlQueryParams,
attributeListPath,
AttributeListUrlQueryParams,
attributePath,
AttributeUrlQueryParams
} from "./urls";
+import AttributeCreateComponent from "./views/AttributeCreate";
import AttributeDetailsComponent from "./views/AttributeDetails";
import AttributeListComponent from "./views/AttributeList";
@@ -20,6 +22,12 @@ const AttributeList: React.FC<RouteComponentProps<{}>> = ({ location }) => {
return <AttributeListComponent params={params} />;
};
+const AttributeCreate: React.FC<RouteComponentProps<{}>> = ({ location }) => {
+ const qs = parseQs(location.search.substr(1));
+ const params: AttributeAddUrlQueryParams = qs;
+ return <AttributeCreateComponent params={params} />;
+};
+
const AttributeDetails: React.FC<RouteComponentProps<{ id: string }>> = ({
location,
match
@@ -34,6 +42,7 @@ export const AttributeSection: React.FC = () => (
<WindowTitle title={i18n.t("Attributes")} />
<Switch>
<Route exact path={attributeListPath} component={AttributeList} />
+ <Route exact path={attributeAddPath} component={AttributeCreate} />
<Route path={attributePath(":id")} component={AttributeDetails} />
</Switch>
</>
diff --git a/saleor/static/dashboard-next/attributes/mutations.ts b/saleor/static/dashboard-next/attributes/mutations.ts
index 5d2197d618..a142458f37 100644
--- a/saleor/static/dashboard-next/attributes/mutations.ts
+++ b/saleor/static/dashboard-next/attributes/mutations.ts
@@ -6,6 +6,10 @@ import {
AttributeBulkDelete,
AttributeBulkDeleteVariables
} from "./types/AttributeBulkDelete";
+import {
+ AttributeCreate,
+ AttributeCreateVariables
+} from "./types/AttributeCreate";
import {
AttributeDelete,
AttributeDeleteVariables
@@ -132,3 +136,22 @@ export const AttributeValueCreateMutation = TypedMutation<
AttributeValueCreate,
AttributeValueCreateVariables
>(attributeValueCreateMutation);
+
+export const attributeCreateMutation = gql`
+ ${attributeDetailsFragment}
+ mutation AttributeCreate($input: AttributeCreateInput!) {
+ attributeCreate(input: $input) {
+ errors {
+ field
+ message
+ }
+ attribute {
+ ...AttributeDetailsFragment
+ }
+ }
+ }
+`;
+export const AttributeCreateMutation = TypedMutation<
+ AttributeCreate,
+ AttributeCreateVariables
+>(attributeCreateMutation);
diff --git a/saleor/static/dashboard-next/attributes/types/AttributeCreate.ts b/saleor/static/dashboard-next/attributes/types/AttributeCreate.ts
new file mode 100644
index 0000000000..f6edb72fb8
--- /dev/null
+++ b/saleor/static/dashboard-next/attributes/types/AttributeCreate.ts
@@ -0,0 +1,47 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+import { AttributeCreateInput, AttributeValueType } from "./../../types/globalTypes";
+
+// ====================================================
+// GraphQL mutation operation: AttributeCreate
+// ====================================================
+
+export interface AttributeCreate_attributeCreate_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface AttributeCreate_attributeCreate_attribute_values {
+ __typename: "AttributeValue";
+ id: string;
+ name: string | null;
+ slug: string | null;
+ sortOrder: number | null;
+ type: AttributeValueType | null;
+ value: string | null;
+}
+
+export interface AttributeCreate_attributeCreate_attribute {
+ __typename: "Attribute";
+ id: string;
+ name: string | null;
+ slug: string | null;
+ values: (AttributeCreate_attributeCreate_attribute_values | null)[] | null;
+}
+
+export interface AttributeCreate_attributeCreate {
+ __typename: "AttributeCreate";
+ errors: AttributeCreate_attributeCreate_errors[] | null;
+ attribute: AttributeCreate_attributeCreate_attribute | null;
+}
+
+export interface AttributeCreate {
+ attributeCreate: AttributeCreate_attributeCreate | null;
+}
+
+export interface AttributeCreateVariables {
+ input: AttributeCreateInput;
+}
diff --git a/saleor/static/dashboard-next/attributes/urls.ts b/saleor/static/dashboard-next/attributes/urls.ts
index 4fb0364a07..fb22388a24 100644
--- a/saleor/static/dashboard-next/attributes/urls.ts
+++ b/saleor/static/dashboard-next/attributes/urls.ts
@@ -18,7 +18,8 @@ export type AttributeAddUrlDialog =
| "edit-value"
| "remove-value"
| "remove-values";
-export type AttributeAddUrlQueryParams = Dialog<AttributeAddUrlDialog>;
+export type AttributeAddUrlQueryParams = Dialog<AttributeAddUrlDialog> &
+ SingleAction;
export const attributeAddPath = urlJoin(attributeSection, "add");
export const attributeAddUrl = (params?: AttributeAddUrlQueryParams) =>
attributeAddPath + "?" + stringifyQs(params);
diff --git a/saleor/static/dashboard-next/attributes/views/AttributeCreate/AttributeCreate.tsx b/saleor/static/dashboard-next/attributes/views/AttributeCreate/AttributeCreate.tsx
index e69de29bb2..e049da3644 100644
--- a/saleor/static/dashboard-next/attributes/views/AttributeCreate/AttributeCreate.tsx
+++ b/saleor/static/dashboard-next/attributes/views/AttributeCreate/AttributeCreate.tsx
@@ -0,0 +1,154 @@
+import * as React from "react";
+
+import useNavigator from "@saleor/hooks/useNavigator";
+import useNotifier from "@saleor/hooks/useNotifier";
+import i18n from "@saleor/i18n";
+import { getMutationState, maybe } from "@saleor/misc";
+import AttributePage from "../../components/AttributePage";
+import AttributeValueDeleteDialog from "../../components/AttributeValueDeleteDialog";
+import AttributeValueEditDialog, {
+ AttributeValueEditDialogFormData
+} from "../../components/AttributeValueEditDialog";
+import { AttributeCreateMutation } from "../../mutations";
+import { AttributeCreate } from "../../types/AttributeCreate";
+import {
+ attributeAddUrl,
+ AttributeAddUrlDialog,
+ AttributeAddUrlQueryParams,
+ attributeListUrl,
+ attributeUrl
+} from "../../urls";
+
+interface AttributeDetailsProps {
+ params: AttributeAddUrlQueryParams;
+}
+
+const AttributeDetails: React.FC<AttributeDetailsProps> = ({ params }) => {
+ const navigate = useNavigator();
+ const notify = useNotifier();
+
+ const [values, setValues] = React.useState<
+ AttributeValueEditDialogFormData[]
+ >([]);
+
+ const id = params.id ? parseInt(params.id, 0) : undefined;
+
+ const closeModal = () =>
+ navigate(
+ attributeAddUrl({
+ ...params,
+ action: undefined,
+ id: undefined
+ }),
+ true
+ );
+
+ const openModal = (action: AttributeAddUrlDialog, valueId?: string) =>
+ navigate(
+ attributeAddUrl({
+ ...params,
+ action,
+ id: valueId
+ })
+ );
+
+ const handleValueDelete = () => {
+ setValues([...values.slice(0, id), ...values.slice(id + 1)]);
+ notify({ text: i18n.t("Value removed") });
+ closeModal();
+ };
+ const handleCreate = (data: AttributeCreate) => {
+ if (data.attributeCreate.errors.length === 0) {
+ notify({ text: i18n.t("Successfully created attribute") });
+ navigate(attributeUrl(data.attributeCreate.attribute.id));
+ }
+ };
+ const handleValueUpdate = (input: AttributeValueEditDialogFormData) => {
+ setValues([...values.slice(0, id), input, ...values.slice(id + 1)]);
+ notify({ text: i18n.t("Saved changes") });
+ closeModal();
+ };
+ const handleValueCreate = (input: AttributeValueEditDialogFormData) => {
+ setValues([...values, input]);
+ notify({ text: i18n.t("Added new value") });
+ closeModal();
+ };
+
+ return (
+ <AttributeCreateMutation onCompleted={handleCreate}>
+ {(attributeCreate, attributeCreateOpts) => {
+ const createTransitionState = getMutationState(
+ attributeCreateOpts.called,
+ attributeCreateOpts.loading,
+ maybe(() => attributeCreateOpts.data.attributeCreate.errors)
+ );
+
+ return (
+ <>
+ <AttributePage
+ attribute={null}
+ disabled={false}
+ onBack={() => navigate(attributeListUrl())}
+ onDelete={undefined}
+ onSubmit={input =>
+ attributeCreate({
+ variables: {
+ input: {
+ ...input,
+ values: values.map(value => ({
+ name: value.name,
+ value: value.slug
+ }))
+ }
+ }
+ })
+ }
+ onValueAdd={() => openModal("add-value")}
+ onValueDelete={id => openModal("remove-value", id)}
+ onValueUpdate={id => openModal("edit-value", id)}
+ saveButtonBarState={createTransitionState}
+ values={values.map((value, valueIndex) => ({
+ __typename: "AttributeValue",
+ id: valueIndex.toString(),
+ sortOrder: null,
+ type: null,
+ value: null,
+ ...value
+ }))}
+ />
+ <AttributeValueEditDialog
+ attributeValue={null}
+ confirmButtonState="default"
+ disabled={false}
+ open={params.action === "add-value"}
+ onClose={closeModal}
+ onSubmit={handleValueCreate}
+ />
+ {values.length && (
+ <>
+ <AttributeValueDeleteDialog
+ open={params.action === "remove-value"}
+ name={maybe(() => values[id].name)}
+ confirmButtonState="default"
+ onClose={closeModal}
+ onConfirm={handleValueDelete}
+ />
+ <AttributeValueEditDialog
+ attributeValue={maybe(() => values[params.id])}
+ confirmButtonState="default"
+ disabled={false}
+ open={params.action === "edit-value"}
+ onClose={closeModal}
+ onSubmit={handleValueUpdate}
+ />
+ </>
+ )}
+ </>
+ );
+ }}
+ </AttributeCreateMutation>
+ );
+};
+AttributeDetails.displayName = "AttributeDetails";
+
+export default AttributeDetails;
diff --git a/saleor/static/dashboard-next/productTypes/mutations.ts b/saleor/static/dashboard-next/productTypes/mutations.ts
index 22235ee361..9898e0644d 100644
--- a/saleor/static/dashboard-next/productTypes/mutations.ts
+++ b/saleor/static/dashboard-next/productTypes/mutations.ts
@@ -1,19 +1,7 @@
import gql from "graphql-tag";
import { TypedMutation } from "../mutations";
-import { attributeFragment, productTypeDetailsFragment } from "./queries";
-import {
- AttributeCreate,
- AttributeCreateVariables
-} from "./types/AttributeCreate";
-import {
- AttributeDelete,
- AttributeDeleteVariables
-} from "./types/AttributeDelete";
-import {
- AttributeUpdate,
- AttributeUpdateVariables
-} from "./types/AttributeUpdate";
+import { productTypeDetailsFragment } from "./queries";
import {
ProductTypeBulkDelete,
ProductTypeBulkDeleteVariables
@@ -101,9 +89,3 @@ export const TypedProductTypeCreateMutation = TypedMutation<
ProductTypeCreate,
ProductTypeCreateVariables
>(productTypeCreateMutation);
-
-export const attributeCreateMutation = "";
-export const TypedAttributeCreateMutation = TypedMutation<
- AttributeCreate,
- AttributeCreateVariables
->(attributeCreateMutation);
diff --git a/saleor/static/dashboard-next/types/globalTypes.ts b/saleor/static/dashboard-next/types/globalTypes.ts
index cc70a45498..a8a51baa26 100644
--- a/saleor/static/dashboard-next/types/globalTypes.ts
+++ b/saleor/static/dashboard-next/types/globalTypes.ts
@@ -239,6 +239,12 @@ export interface AddressInput {
phone?: string | null;
}
+export interface AttributeCreateInput {
+ name: string;
+ slug?: string | null;
+ values?: (AttributeValueCreateInput | null)[] | null;
+}
+
export interface AttributeInput {
slug: string;
value: string;