saleor-dashboard/src/customers/views/CustomerDetails.tsx

168 lines
6 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import DialogContentText from "@material-ui/core/DialogContentText";
2019-08-09 10:26:22 +00:00
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import ActionDialog from "@saleor/components/ActionDialog";
import { WindowTitle } from "@saleor/components/WindowTitle";
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
import { commonMessages } from "@saleor/intl";
2020-02-20 14:18:22 +00:00
import NotFoundPage from "@saleor/components/NotFoundPage";
2019-12-06 17:11:46 +00:00
import { maybe } from "../../misc";
2019-06-19 14:40:52 +00:00
import { orderListUrl, orderUrl } from "../../orders/urls";
import CustomerDetailsPage from "../components/CustomerDetailsPage/CustomerDetailsPage";
import {
TypedRemoveCustomerMutation,
TypedUpdateCustomerMutation
} from "../mutations";
import { TypedCustomerDetailsQuery } from "../queries";
import { RemoveCustomer } from "../types/RemoveCustomer";
import { UpdateCustomer } from "../types/UpdateCustomer";
import {
customerAddressesUrl,
customerListUrl,
customerUrl,
CustomerUrlQueryParams
} from "../urls";
interface CustomerDetailsViewProps {
id: string;
params: CustomerUrlQueryParams;
}
export const CustomerDetailsView: React.FC<CustomerDetailsViewProps> = ({
id,
params
}) => {
2019-06-19 14:40:52 +00:00
const navigate = useNavigator();
const notify = useNotifier();
const intl = useIntl();
2019-06-19 14:40:52 +00:00
const handleCustomerUpdateSuccess = (data: UpdateCustomer) => {
if (data.customerUpdate.errors.length === 0) {
notify({
text: intl.formatMessage(commonMessages.savedChanges)
2019-06-19 14:40:52 +00:00
});
}
};
const handleCustomerRemoveSuccess = (data: RemoveCustomer) => {
if (data.customerDelete.errors.length === 0) {
notify({
text: intl.formatMessage({
defaultMessage: "Customer Removed"
2019-06-19 14:40:52 +00:00
})
});
navigate(customerListUrl());
}
};
2020-02-20 14:18:22 +00:00
const handleBack = () => navigate(customerListUrl());
2019-06-19 14:40:52 +00:00
return (
<TypedRemoveCustomerMutation
variables={{ id }}
onCompleted={handleCustomerRemoveSuccess}
>
{(removeCustomer, removeCustomerOpts) => (
<TypedUpdateCustomerMutation onCompleted={handleCustomerUpdateSuccess}>
{(updateCustomer, updateCustomerOpts) => (
2020-02-20 14:18:22 +00:00
<TypedCustomerDetailsQuery displayLoader variables={{ id }}>
{customerDetails => {
const user = customerDetails.data?.user;
if (user === null) {
return <NotFoundPage onBack={handleBack} />;
}
return (
<>
<WindowTitle
title={maybe(() => customerDetails.data.user.email)}
/>
<CustomerDetailsPage
customer={maybe(() => customerDetails.data.user)}
disabled={
customerDetails.loading ||
updateCustomerOpts.loading ||
removeCustomerOpts.loading
}
2020-03-03 11:20:39 +00:00
errors={
updateCustomerOpts.data?.customerUpdate.errors || []
}
2020-02-20 14:18:22 +00:00
saveButtonBar={updateCustomerOpts.status}
onAddressManageClick={() =>
navigate(customerAddressesUrl(id))
}
onBack={handleBack}
onRowClick={id => navigate(orderUrl(id))}
onSubmit={formData =>
updateCustomer({
variables: {
id,
input: {
email: formData.email,
firstName: formData.firstName,
isActive: formData.isActive,
lastName: formData.lastName,
note: formData.note
}
2019-06-19 14:40:52 +00:00
}
})
2020-02-20 14:18:22 +00:00
}
onDelete={() =>
navigate(
customerUrl(id, {
action: "remove"
})
)
}
onViewAllOrdersClick={() =>
navigate(
orderListUrl({
customer: maybe(
() => customerDetails.data.user.email
)
})
)
}
/>
<ActionDialog
confirmButtonState={removeCustomerOpts.status}
onClose={() => navigate(customerUrl(id), true)}
onConfirm={() => removeCustomer()}
title={intl.formatMessage({
defaultMessage: "Delete Customer",
description: "dialog header"
})}
variant="delete"
open={params.action === "remove"}
>
<DialogContentText>
<FormattedMessage
defaultMessage="Are you sure you want to delete {email}?"
description="delete customer, dialog content"
values={{
email: (
<strong>
{maybe(
() => customerDetails.data.user.email,
"..."
)}
</strong>
)
}}
/>
</DialogContentText>
</ActionDialog>
</>
);
}}
2019-06-19 14:40:52 +00:00
</TypedCustomerDetailsQuery>
)}
</TypedUpdateCustomerMutation>
)}
</TypedRemoveCustomerMutation>
);
};
export default CustomerDetailsView;