Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move error logic and state to its own component
dominik-zeglen854ddd2bf41a
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/errors.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/errors.tsxnew file mode 100644index 0000000000..85bfb42f37--- /dev/null+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/errors.tsx@@ -0,0 +1,46 @@+import * as React from "react";++import { UserError } from "../../..";++interface ProductTypeUpdateErrorsState {+ addAttributeErrors: UserError[];+ editAttributeErrors: UserError[];+ formErrors: UserError[];+}+interface ProductTypeUpdateErrorsProps {+ children: (+ props: {+ errors: ProductTypeUpdateErrorsState;+ set: {+ addAttributeErrors: (errors: UserError[]) => void;+ editAttributeErrors: (errors: UserError[]) => void;+ formErrors: (errors: UserError[]) => void;+ };+ }+ ) => React.ReactNode;+}++export class ProductTypeUpdateErrors extends React.Component<+ ProductTypeUpdateErrorsProps,+ ProductTypeUpdateErrorsState+> {+ state: ProductTypeUpdateErrorsState = {+ addAttributeErrors: [],+ editAttributeErrors: [],+ formErrors: []+ };++ render() {+ return this.props.children({+ errors: this.state,+ set: {+ addAttributeErrors: (addAttributeErrors: UserError[]) =>+ this.setState({ addAttributeErrors }),+ editAttributeErrors: (editAttributeErrors: UserError[]) =>+ this.setState({ editAttributeErrors }),+ formErrors: (formErrors: UserError[]) => this.setState({ formErrors })+ }+ });+ }+}+export default ProductTypeUpdateErrors;diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsxindex 42c40b2bf0..50ad666147 100644--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx@@ -20,6 +20,7 @@ import { AttributeDelete } from "../../types/AttributeDelete";import { AttributeUpdate } from "../../types/AttributeUpdate";import { ProductTypeDelete } from "../../types/ProductTypeDelete";import { ProductTypeUpdate as ProductTypeUpdateMutation } from "../../types/ProductTypeUpdate";+import { ProductTypeUpdateErrors } from "./errors";import { addAttributeUrl, editAttributeUrl } from "./urls";interface ProductTypeUpdateProps {@@ -33,283 +34,318 @@ export const ProductTypeUpdate: React.StatelessComponent<{pushMessage => (<Navigator>{navigate => (- <TypedProductTypeDetailsQuery variables={{ id }}>- {({ data, loading: dataLoading }) => {- const closeModal = () =>- navigate(productTypeDetailsUrl(encodeURIComponent(id)), true);- const handleAttributeCreateSuccess = (data: AttributeCreate) => {- if (!maybe(() => data.attributeCreate.errors.length)) {- pushMessage({- text: i18n.t("Attribute created", {- context: "notification"- })- });- closeModal();- }- };- const handleAttributeDeleteSuccess = (data: AttributeDelete) => {- if (!maybe(() => data.attributeDelete.errors.length)) {- pushMessage({- text: i18n.t("Attribute deleted", {- context: "notification"- })- });- }- };- const handleAttributeUpdateSuccess = (data: AttributeUpdate) => {- if (!maybe(() => data.attributeUpdate.errors.length)) {- pushMessage({- text: i18n.t("Attribute updated", {- context: "notification"- })- });- closeModal();- }- };- const handleProductTypeDeleteSuccess = (- deleteData: ProductTypeDelete- ) => {- if (deleteData.productTypeDelete.errors.length === 0) {- pushMessage({- text: i18n.t("Product type deleted", {- context: "notification"- })- });- navigate(productTypeListUrl);- }- };- const handleProductTypeUpdateSuccess = (- updateData: ProductTypeUpdateMutation- ) => {- if (updateData.productTypeUpdate.errors.length === 0) {- pushMessage({- text: i18n.t("Product type updated", {- context: "notification"- })- });- }- };-- return (- <ProductTypeOperations- id={id}- onAttributeCreate={handleAttributeCreateSuccess}- onAttributeDelete={handleAttributeDeleteSuccess}- onAttributeUpdate={handleAttributeUpdateSuccess}- onProductTypeDelete={handleProductTypeDeleteSuccess}- onProductTypeUpdate={handleProductTypeUpdateSuccess}- >- {({- attributeCreate,- deleteAttribute,- deleteProductType,- loading: mutationLoading,- updateAttribute,- updateProductType- }) => {- const handleProductTypeDelete = () =>- deleteProductType.mutate({ id });- const handleProductTypeUpdate = (- formData: ProductTypeForm- ) => {- updateProductType.mutate({- id,- input: {- hasVariants: formData.hasVariants,- isShippingRequired: formData.isShippingRequired,- name: formData.name,- productAttributes: formData.productAttributes.map(- choice => choice.value- ),- taxRate: formData.taxRate,- variantAttributes: formData.variantAttributes.map(- choice => choice.value- ),- weight: formData.weight- }+ <ProductTypeUpdateErrors>+ {({ errors, set: setErrors }) => (+ <TypedProductTypeDetailsQuery variables={{ id }}>+ {({ data, loading: dataLoading }) => {+ const closeModal = () => {+ navigate(+ productTypeDetailsUrl(encodeURIComponent(id)),+ true+ );+ setErrors.addAttributeErrors([]);+ setErrors.editAttributeErrors([]);+ };+ const handleAttributeCreateSuccess = (+ data: AttributeCreate+ ) => {+ if (+ !data.attributeCreate.errors ||+ data.attributeCreate.errors.length === 0+ ) {+ pushMessage({+ text: i18n.t("Attribute created", {+ context: "notification"+ })+ });+ closeModal();+ } else if (+ data.attributeCreate.errors !== null &&+ data.attributeCreate.errors.length > 0+ ) {+ setErrors.addAttributeErrors(data.attributeCreate.errors);+ }+ };+ const handleAttributeDeleteSuccess = (+ data: AttributeDelete+ ) => {+ if (!data.attributeDelete.errors) {+ pushMessage({+ text: i18n.t("Attribute deleted", {+ context: "notification"+ })});- };- const handleAttributeCreate = (- data: AttributeForm,- type: AttributeTypeEnum- ) =>- attributeCreate.mutate({- id,- input: {- name: data.name,- values: data.values.map(value => ({- name: value.label- }))- },- type+ }+ };+ const handleAttributeUpdateSuccess = (+ data: AttributeUpdate+ ) => {+ if (+ !data.attributeUpdate.errors ||+ data.attributeUpdate.errors.length === 0+ ) {+ pushMessage({+ text: i18n.t("Attribute updated", {+ context: "notification"+ })});- const handleAttributeDelete = (- id: string,- event: React.MouseEvent<any>- ) => {- event.stopPropagation();- deleteAttribute.mutate({ id });- };- const handleAttributeUpdate = (- id: string,- formData: AttributeForm- ) => {- const attribute = data.productType.variantAttributes- .concat(data.productType.productAttributes)- .filter(attribute => attribute.id === id)[0];- updateAttribute.mutate({- id,- input: {- addValues: formData.values- .filter(- value =>- !attribute.values- .map(value => value.id)- .includes(value.value)- )- .map(value => ({- name: value.label- })),- name: formData.name,- removeValues: attribute.values- .filter(- value =>- !formData.values- .map(value => value.value)- .includes(value.id)- )- .map(value => value.id)- }+ closeModal();+ } else if (+ data.attributeUpdate.errors !== null &&+ data.attributeUpdate.errors.length > 0+ ) {+ setErrors.editAttributeErrors(+ data.attributeUpdate.errors+ );+ }+ };+ const handleProductTypeDeleteSuccess = (+ deleteData: ProductTypeDelete+ ) => {+ if (deleteData.productTypeDelete.errors.length === 0) {+ pushMessage({+ text: i18n.t("Product type deleted", {+ context: "notification"+ })});- };- const loading = mutationLoading || dataLoading;- return (- <>- <ProductTypeDetailsPage- defaultWeightUnit={maybe(- () => data.shop.defaultWeightUnit- )}- disabled={loading}- errors={maybe(- () =>- updateProductType.data.productTypeUpdate.errors- )}- pageTitle={maybe(() => data.productType.name)}- productType={maybe(() => data.productType)}- saveButtonBarState={loading ? "loading" : "default"}- onAttributeAdd={type =>- navigate(- addAttributeUrl(encodeURIComponent(id), type)- )- }- onAttributeDelete={handleAttributeDelete}- onAttributeUpdate={attributeId =>- navigate(- editAttributeUrl(- encodeURIComponent(id),- encodeURIComponent(attributeId)- )- )- }- onBack={() => navigate(productTypeListUrl)}- onDelete={handleProductTypeDelete}- onSubmit={handleProductTypeUpdate}- />- {!dataLoading && (+ navigate(productTypeListUrl);+ }+ };+ const handleProductTypeUpdateSuccess = (+ updateData: ProductTypeUpdateMutation+ ) => {+ if (+ !updateData.productTypeUpdate.errors ||+ updateData.productTypeUpdate.errors.length === 0+ ) {+ pushMessage({+ text: i18n.t("Product type updated", {+ context: "notification"+ })+ });+ } else if (+ updateData.productTypeUpdate.errors !== null &&+ updateData.productTypeUpdate.errors.length > 0+ ) {+ setErrors.formErrors(updateData.productTypeUpdate.errors);+ }+ };++ return (+ <ProductTypeOperations+ id={id}+ onAttributeCreate={handleAttributeCreateSuccess}+ onAttributeDelete={handleAttributeDeleteSuccess}+ onAttributeUpdate={handleAttributeUpdateSuccess}+ onProductTypeDelete={handleProductTypeDeleteSuccess}+ onProductTypeUpdate={handleProductTypeUpdateSuccess}+ >+ {({+ attributeCreate,+ deleteAttribute,+ deleteProductType,+ loading: mutationLoading,+ updateAttribute,+ updateProductType+ }) => {+ const handleProductTypeDelete = () =>+ deleteProductType.mutate({ id });+ const handleProductTypeUpdate = (+ formData: ProductTypeForm+ ) => {+ updateProductType.mutate({+ id,+ input: {+ hasVariants: formData.hasVariants,+ isShippingRequired: formData.isShippingRequired,+ name: formData.name,+ productAttributes: formData.productAttributes.map(+ choice => choice.value+ ),+ taxRate: formData.taxRate,+ variantAttributes: formData.variantAttributes.map(+ choice => choice.value+ ),+ weight: formData.weight+ }+ });+ };+ const handleAttributeCreate = (+ data: AttributeForm,+ type: AttributeTypeEnum+ ) =>+ attributeCreate.mutate({+ id,+ input: {+ name: data.name,+ values: data.values.map(value => ({+ name: value.label+ }))+ },+ type+ });+ const handleAttributeDelete = (+ id: string,+ event: React.MouseEvent<any>+ ) => {+ event.stopPropagation();+ deleteAttribute.mutate({ id });+ };+ const handleAttributeUpdate = (+ id: string,+ formData: AttributeForm+ ) => {+ const attribute = data.productType.variantAttributes+ .concat(data.productType.productAttributes)+ .filter(attribute => attribute.id === id)[0];+ updateAttribute.mutate({+ id,+ input: {+ addValues: formData.values+ .filter(+ value =>+ !attribute.values+ .map(value => value.id)+ .includes(value.value)+ )+ .map(value => ({+ name: value.label+ })),+ name: formData.name,+ removeValues: attribute.values+ .filter(+ value =>+ !formData.values+ .map(value => value.value)+ .includes(value.id)+ )+ .map(value => value.id)+ }+ });+ };+ const loading = mutationLoading || dataLoading;+ return (<>- {Object.keys(AttributeTypeEnum).map(key => (- <Route- exact- path={addAttributeUrl(- encodeURIComponent(id),- AttributeTypeEnum[key]- )}- key={key}- >- {({ match }) => (- <ProductTypeAttributeEditDialog- disabled={attributeCreate.loading}- errors={maybe(- () =>- attributeCreate.data.attributeCreate- .errors- )}- name=""- values={[]}- onClose={closeModal}- onConfirm={data =>- handleAttributeCreate(- data,- AttributeTypeEnum[key]- )- }- opened={!!match}- title={i18n.t("Add Attribute", {- context: "modal title"- })}- />- )}- </Route>- ))}- <Route- exact- path={editAttributeUrl(- encodeURIComponent(id),- ":id"+ <ProductTypeDetailsPage+ defaultWeightUnit={maybe(+ () => data.shop.defaultWeightUnit)}- >- {({ match }) => {- const attribute = maybe(- () =>- data.productType.productAttributes- .concat(- data.productType.variantAttributes- )- .filter(- attribute =>- attribute.id ===- decodeURIComponent(match.params.id)- )[0]- );- return (- <ProductTypeAttributeEditDialog- disabled={updateAttribute.loading}- errors={maybe(- () =>- updateAttribute.data.attributeUpdate- .errors+ disabled={loading}+ errors={errors.formErrors}+ pageTitle={maybe(() => data.productType.name)}+ productType={maybe(() => data.productType)}+ saveButtonBarState={+ loading ? "loading" : "default"+ }+ onAttributeAdd={type =>+ navigate(+ addAttributeUrl(encodeURIComponent(id), type)+ )+ }+ onAttributeDelete={handleAttributeDelete}+ onAttributeUpdate={attributeId =>+ navigate(+ editAttributeUrl(+ encodeURIComponent(id),+ encodeURIComponent(attributeId)+ )+ )+ }+ onBack={() => navigate(productTypeListUrl)}+ onDelete={handleProductTypeDelete}+ onSubmit={handleProductTypeUpdate}+ />+ {!dataLoading && (+ <>+ {Object.keys(AttributeTypeEnum).map(key => (+ <Route+ exact+ path={addAttributeUrl(+ encodeURIComponent(id),+ AttributeTypeEnum[key])}- name={maybe(() => attribute.name)}- values={maybe(() =>- attribute.values.map(value => ({- label: value.name,- value: value.id- }))+ key={key}+ >+ {({ match }) => (+ <ProductTypeAttributeEditDialog+ disabled={attributeCreate.loading}+ errors={errors.addAttributeErrors}+ name=""+ values={[]}+ onClose={closeModal}+ onConfirm={data =>+ handleAttributeCreate(+ data,+ AttributeTypeEnum[key]+ )+ }+ opened={!!match}+ title={i18n.t("Add Attribute", {+ context: "modal title"+ })}+ />)}- onClose={closeModal}- onConfirm={data =>- handleAttributeUpdate(- decodeURIComponent(match.params.id),- data- )- }- opened={!!match}- title={i18n.t("Edit Attribute", {- context: "modal title"- })}- />- );- }}- </Route>+ </Route>+ ))}+ <Route+ exact+ path={editAttributeUrl(+ encodeURIComponent(id),+ ":id"+ )}+ >+ {({ match }) => {+ const attribute = maybe(+ () =>+ data.productType.productAttributes+ .concat(+ data.productType.variantAttributes+ )+ .filter(+ attribute =>+ attribute.id ===+ decodeURIComponent(+ match.params.id+ )+ )[0]+ );+ return (+ <ProductTypeAttributeEditDialog+ disabled={updateAttribute.loading}+ errors={errors.editAttributeErrors}+ name={maybe(() => attribute.name)}+ values={maybe(() =>+ attribute.values.map(value => ({+ label: value.name,+ value: value.id+ }))+ )}+ onClose={closeModal}+ onConfirm={data =>+ handleAttributeUpdate(+ decodeURIComponent(match.params.id),+ data+ )+ }+ opened={!!match}+ title={i18n.t("Edit Attribute", {+ context: "modal title"+ })}+ />+ );+ }}+ </Route>+ </>+ )}</>- )}- </>- );- }}- </ProductTypeOperations>- );- }}- </TypedProductTypeDetailsQuery>+ );+ }}+ </ProductTypeOperations>+ );+ }}+ </TypedProductTypeDetailsQuery>+ )}+ </ProductTypeUpdateErrors>)}</Navigator>)}