Merge pull request #244 from mirumee/fix/minor-bugs

Fix minor bugs
This commit is contained in:
Dominik Żegleń 2019-11-07 14:09:54 +01:00 committed by GitHub
commit 45a56bb175
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 114 additions and 30 deletions

View file

@ -4,7 +4,7 @@ All notable, unreleased changes to this project will be documented in this file.
## [Unreleased] ## [Unreleased]
.. - Fix minor bugs - #244 by @dominik-zeglen
## 2.0.0 ## 2.0.0

View file

@ -4,7 +4,6 @@ import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent"; import DialogContent from "@material-ui/core/DialogContent";
import DialogTitle from "@material-ui/core/DialogTitle"; import DialogTitle from "@material-ui/core/DialogTitle";
import { createStyles, withStyles, WithStyles } from "@material-ui/core/styles"; import { createStyles, withStyles, WithStyles } from "@material-ui/core/styles";
import React from "react"; import React from "react";
import { FormattedMessage } from "react-intl"; import { FormattedMessage } from "react-intl";
@ -13,10 +12,13 @@ import ConfirmButton, {
ConfirmButtonTransitionState ConfirmButtonTransitionState
} from "@saleor/components/ConfirmButton"; } from "@saleor/components/ConfirmButton";
import Form from "@saleor/components/Form"; import Form from "@saleor/components/Form";
import useAddressValidation from "@saleor/hooks/useAddressValidation";
import useModalDialogErrors from "@saleor/hooks/useModalDialogErrors";
import useStateFromProps from "@saleor/hooks/useStateFromProps"; import useStateFromProps from "@saleor/hooks/useStateFromProps";
import { buttonMessages } from "@saleor/intl"; import { buttonMessages } from "@saleor/intl";
import { maybe } from "@saleor/misc"; import { maybe } from "@saleor/misc";
import { UserError } from "@saleor/types"; import { UserError } from "@saleor/types";
import { AddressInput } from "@saleor/types/globalTypes";
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler"; import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
import { AddressTypeInput } from "../../types"; import { AddressTypeInput } from "../../types";
import { CustomerAddresses_user_addresses } from "../../types/CustomerAddresses"; import { CustomerAddresses_user_addresses } from "../../types/CustomerAddresses";
@ -32,7 +34,7 @@ export interface CustomerAddressDialogProps {
open: boolean; open: boolean;
variant: "create" | "edit"; variant: "create" | "edit";
onClose: () => void; onClose: () => void;
onConfirm: (data: AddressTypeInput) => void; onConfirm: (data: AddressInput) => void;
} }
const styles = createStyles({ const styles = createStyles({
@ -56,6 +58,15 @@ const CustomerAddressDialog = withStyles(styles, {})(
const [countryDisplayName, setCountryDisplayName] = useStateFromProps( const [countryDisplayName, setCountryDisplayName] = useStateFromProps(
maybe(() => address.country.country, "") maybe(() => address.country.country, "")
); );
const {
errors: validationErrors,
submit: handleSubmit
} = useAddressValidation(onConfirm);
const dialogErrors = useModalDialogErrors(
[...errors, ...validationErrors],
open
);
const initialForm: AddressTypeInput = { const initialForm: AddressTypeInput = {
city: maybe(() => address.city, ""), city: maybe(() => address.city, ""),
cityArea: maybe(() => address.cityArea, ""), cityArea: maybe(() => address.cityArea, ""),
@ -87,8 +98,12 @@ const CustomerAddressDialog = withStyles(styles, {})(
fullWidth fullWidth
maxWidth="sm" maxWidth="sm"
> >
<Form initial={initialForm} errors={errors} onSubmit={onConfirm}> <Form
{({ change, data, errors }) => { initial={initialForm}
errors={dialogErrors}
onSubmit={handleSubmit}
>
{({ change, data, errors: formErrors }) => {
const handleCountrySelect = createSingleAutocompleteSelectHandler( const handleCountrySelect = createSingleAutocompleteSelectHandler(
change, change,
setCountryDisplayName, setCountryDisplayName,
@ -115,7 +130,7 @@ const CustomerAddressDialog = withStyles(styles, {})(
countries={countryChoices} countries={countryChoices}
data={data} data={data}
countryDisplayValue={countryDisplayName} countryDisplayValue={countryDisplayName}
errors={errors} errors={formErrors}
onChange={change} onChange={change}
onCountryChange={handleCountrySelect} onCountryChange={handleCountrySelect}
/> />

View file

@ -8,7 +8,7 @@ import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier"; import useNotifier from "@saleor/hooks/useNotifier";
import useShop from "@saleor/hooks/useShop"; import useShop from "@saleor/hooks/useShop";
import { commonMessages } from "@saleor/intl"; import { commonMessages } from "@saleor/intl";
import { getMutationState, maybe, transformFormToAddress } from "../../misc"; import { getMutationState, maybe } from "../../misc";
import CustomerAddressDialog from "../components/CustomerAddressDialog"; import CustomerAddressDialog from "../components/CustomerAddressDialog";
import CustomerAddressListPage from "../components/CustomerAddressListPage"; import CustomerAddressListPage from "../components/CustomerAddressListPage";
import { import {
@ -176,7 +176,7 @@ const CustomerAddresses: React.FC<CustomerAddressesProps> = ({
createCustomerAddress({ createCustomerAddress({
variables: { variables: {
id, id,
input: transformFormToAddress(input) input
} }
}) })
} }
@ -202,7 +202,7 @@ const CustomerAddresses: React.FC<CustomerAddressesProps> = ({
updateCustomerAddress({ updateCustomerAddress({
variables: { variables: {
id: params.id, id: params.id,
input: transformFormToAddress(input) input
} }
}) })
} }

View file

@ -8,6 +8,7 @@ import { maybe, transformFormToAddress } from "../../misc";
import CustomerCreatePage from "../components/CustomerCreatePage"; import CustomerCreatePage from "../components/CustomerCreatePage";
import { TypedCreateCustomerMutation } from "../mutations"; import { TypedCreateCustomerMutation } from "../mutations";
import { TypedCustomerCreateDataQuery } from "../queries"; import { TypedCustomerCreateDataQuery } from "../queries";
import { AddressTypeInput } from "../types";
import { CreateCustomer } from "../types/CreateCustomer"; import { CreateCustomer } from "../types/CreateCustomer";
import { customerListUrl, customerUrl } from "../urls"; import { customerListUrl, customerUrl } from "../urls";
@ -57,6 +58,21 @@ export const CustomerCreate: React.FC<{}> = () => {
} }
onBack={() => navigate(customerListUrl())} onBack={() => navigate(customerListUrl())}
onSubmit={formData => { 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 = { const address = {
city: formData.city, city: formData.city,
cityArea: formData.cityArea, cityArea: formData.cityArea,
@ -73,8 +89,12 @@ export const CustomerCreate: React.FC<{}> = () => {
createCustomer({ createCustomer({
variables: { variables: {
input: { input: {
defaultBillingAddress: transformFormToAddress(address), defaultBillingAddress: areAddressInputFieldsModified
defaultShippingAddress: transformFormToAddress(address), ? transformFormToAddress(address)
: null,
defaultShippingAddress: areAddressInputFieldsModified
? transformFormToAddress(address)
: null,
email: formData.email, email: formData.email,
firstName: formData.customerFirstName, firstName: formData.customerFirstName,
lastName: formData.customerLastName, lastName: formData.customerLastName,

View file

@ -0,0 +1,46 @@
import { useState } from "react";
import { useIntl } from "react-intl";
import { AddressTypeInput } from "@saleor/customers/types";
import { commonMessages } from "@saleor/intl";
import { transformFormToAddress } from "@saleor/misc";
import { UserError } from "@saleor/types";
import { AddressInput } from "@saleor/types/globalTypes";
import { add, remove } from "@saleor/utils/lists";
interface UseAddressValidation {
errors: UserError[];
submit: (data: AddressTypeInput) => void;
}
function useAddressValidation(
onSubmit: (address: AddressInput) => void
): UseAddressValidation {
const intl = useIntl();
const [validationErrors, setValidationErrors] = useState<UserError[]>([]);
const countryRequiredError = {
field: "country",
message: intl.formatMessage(commonMessages.requiredField)
};
return {
errors: validationErrors,
submit: (data: AddressTypeInput) => {
try {
setValidationErrors(
remove(
countryRequiredError,
validationErrors,
(a, b) => a.field === b.field
)
);
onSubmit(transformFormToAddress(data));
} catch {
setValidationErrors(add(countryRequiredError, validationErrors));
}
}
};
}
export default useAddressValidation;

View file

@ -12,12 +12,15 @@ import ConfirmButton, {
ConfirmButtonTransitionState ConfirmButtonTransitionState
} from "@saleor/components/ConfirmButton"; } from "@saleor/components/ConfirmButton";
import Form from "@saleor/components/Form"; import Form from "@saleor/components/Form";
import { AddressTypeInput } from "@saleor/customers/types";
import useAddressValidation from "@saleor/hooks/useAddressValidation";
import useModalDialogErrors from "@saleor/hooks/useModalDialogErrors";
import useStateFromProps from "@saleor/hooks/useStateFromProps"; import useStateFromProps from "@saleor/hooks/useStateFromProps";
import { buttonMessages } from "@saleor/intl"; import { buttonMessages } from "@saleor/intl";
import { maybe } from "@saleor/misc"; import { maybe } from "@saleor/misc";
import { UserError } from "@saleor/types";
import { AddressInput } from "@saleor/types/globalTypes";
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler"; import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
import { AddressTypeInput } from "../../../customers/types";
import { UserError } from "../../../types";
const styles = createStyles({ const styles = createStyles({
overflow: { overflow: {
@ -36,7 +39,7 @@ interface OrderAddressEditDialogProps extends WithStyles<typeof styles> {
label: string; label: string;
}>; }>;
onClose(); onClose();
onConfirm(data: AddressTypeInput); onConfirm(data: AddressInput);
} }
const OrderAddressEditDialog = withStyles(styles, { const OrderAddressEditDialog = withStyles(styles, {
@ -59,6 +62,15 @@ const OrderAddressEditDialog = withStyles(styles, {
() => countries.find(country => address.country === country.code).label () => countries.find(country => address.country === country.code).label
) )
); );
const {
errors: validationErrors,
submit: handleSubmit
} = useAddressValidation(onConfirm);
const dialogErrors = useModalDialogErrors(
[...errors, ...validationErrors],
open
);
const countryChoices = countries.map(country => ({ const countryChoices = countries.map(country => ({
label: country.label, label: country.label,
value: country.code value: country.code
@ -70,7 +82,7 @@ const OrderAddressEditDialog = withStyles(styles, {
open={open} open={open}
classes={{ paper: classes.overflow }} classes={{ paper: classes.overflow }}
> >
<Form initial={address} errors={errors} onSubmit={onConfirm}> <Form initial={address} errors={dialogErrors} onSubmit={handleSubmit}>
{({ change, data, errors, submit }) => { {({ change, data, errors, submit }) => {
const handleCountrySelect = createSingleAutocompleteSelectHandler( const handleCountrySelect = createSingleAutocompleteSelectHandler(
change, change,

View file

@ -36,9 +36,9 @@ const OrderDraftCancelDialog: React.FC<OrderDraftCancelDialogProps> = ({
> >
<DialogContentText> <DialogContentText>
<FormattedMessage <FormattedMessage
defaultMessage="Are you sure you want to delete draft #{number}?" defaultMessage="Are you sure you want to delete draft #{orderNumber}?"
values={{ values={{
orderNumber orderNumber: <strong>{orderNumber}</strong>
}} }}
/> />
</DialogContentText> </DialogContentText>

View file

@ -6,12 +6,7 @@ import useUser from "@saleor/hooks/useUser";
import { DEFAULT_INITIAL_SEARCH_DATA } from "../../../config"; import { DEFAULT_INITIAL_SEARCH_DATA } from "../../../config";
import SearchCustomers from "../../../containers/SearchCustomers"; import SearchCustomers from "../../../containers/SearchCustomers";
import { customerUrl } from "../../../customers/urls"; import { customerUrl } from "../../../customers/urls";
import { import { getMutationState, maybe, transformAddressToForm } from "../../../misc";
getMutationState,
maybe,
transformAddressToForm,
transformFormToAddress
} from "../../../misc";
import { productUrl } from "../../../products/urls"; import { productUrl } from "../../../products/urls";
import { OrderStatus } from "../../../types/globalTypes"; import { OrderStatus } from "../../../types/globalTypes";
import OrderAddressEditDialog from "../../components/OrderAddressEditDialog"; import OrderAddressEditDialog from "../../components/OrderAddressEditDialog";
@ -604,9 +599,7 @@ export const OrderDetails: React.FC<OrderDetailsProps> = ({ id, params }) => {
orderUpdate.mutate({ orderUpdate.mutate({
id, id,
input: { input: {
shippingAddress: transformFormToAddress(
shippingAddress shippingAddress
)
} }
}) })
} }
@ -639,9 +632,7 @@ export const OrderDetails: React.FC<OrderDetailsProps> = ({ id, params }) => {
orderUpdate.mutate({ orderUpdate.mutate({
id, id,
input: { input: {
billingAddress: transformFormToAddress(
billingAddress billingAddress
)
} }
}) })
} }

View file

@ -49,7 +49,7 @@ const TranslationsCategoriesPage: React.FC<TranslationsCategoriesPageProps> = ({
title={intl.formatMessage( title={intl.formatMessage(
{ {
defaultMessage: defaultMessage:
'Translation Category "{categoryNane}" - {languageCode}' 'Translation Category "{categoryName}" - {languageCode}'
}, },
{ {
categoryName: maybe(() => category.name, "..."), categoryName: maybe(() => category.name, "..."),