Do not submit address if not given

This commit is contained in:
dominik-zeglen 2019-11-06 14:26:59 +01:00
parent 11e6743ad7
commit f2330bd662

View file

@ -8,6 +8,7 @@ import { maybe, transformFormToAddress } from "../../misc";
import CustomerCreatePage from "../components/CustomerCreatePage";
import { TypedCreateCustomerMutation } from "../mutations";
import { TypedCustomerCreateDataQuery } from "../queries";
import { AddressTypeInput } from "../types";
import { CreateCustomer } from "../types/CreateCustomer";
import { customerListUrl, customerUrl } from "../urls";
@ -57,6 +58,21 @@ export const CustomerCreate: React.StatelessComponent<{}> = () => {
}
onBack={() => navigate(customerListUrl())}
onSubmit={formData => {
const areAddressInputFieldsModified = ([
"city",
"companyName",
"country",
"countryArea",
"firstName",
"lastName",
"phone",
"postalCode",
"streetAddress1",
"streetAddress2"
] as Array<keyof AddressTypeInput>)
.map(key => formData[key])
.some(field => field !== "");
const address = {
city: formData.city,
cityArea: formData.cityArea,
@ -73,8 +89,12 @@ export const CustomerCreate: React.StatelessComponent<{}> = () => {
createCustomer({
variables: {
input: {
defaultBillingAddress: transformFormToAddress(address),
defaultShippingAddress: transformFormToAddress(address),
defaultBillingAddress: areAddressInputFieldsModified
? transformFormToAddress(address)
: null,
defaultShippingAddress: areAddressInputFieldsModified
? transformFormToAddress(address)
: null,
email: formData.email,
firstName: formData.customerFirstName,
lastName: formData.customerLastName,