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

158 lines
5.5 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";
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());
}
};
return (
<TypedRemoveCustomerMutation
variables={{ id }}
onCompleted={handleCustomerRemoveSuccess}
>
{(removeCustomer, removeCustomerOpts) => (
<TypedUpdateCustomerMutation onCompleted={handleCustomerUpdateSuccess}>
{(updateCustomer, updateCustomerOpts) => (
<TypedCustomerDetailsQuery
displayLoader
variables={{ id }}
require={["user"]}
>
2019-12-06 17:11:46 +00:00
{customerDetails => (
<>
<WindowTitle
title={maybe(() => customerDetails.data.user.email)}
/>
<CustomerDetailsPage
customer={maybe(() => customerDetails.data.user)}
disabled={
customerDetails.loading ||
updateCustomerOpts.loading ||
removeCustomerOpts.loading
}
errors={maybe(
() => updateCustomerOpts.data.customerUpdate.errors
)}
2019-12-06 17:17:44 +00:00
saveButtonBar={updateCustomerOpts.status}
2019-12-06 17:11:46 +00:00
onAddressManageClick={() =>
navigate(customerAddressesUrl(id))
}
onBack={() => navigate(customerListUrl())}
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
}
2019-12-06 17:11:46 +00:00
}
})
}
onDelete={() =>
navigate(
customerUrl(id, {
action: "remove"
})
)
}
onViewAllOrdersClick={() =>
navigate(
orderListUrl({
email: maybe(() => customerDetails.data.user.email)
2019-06-19 14:40:52 +00:00
})
2019-12-06 17:11:46 +00:00
)
}
/>
<ActionDialog
2019-12-06 17:17:44 +00:00
confirmButtonState={removeCustomerOpts.status}
2019-12-06 17:11:46 +00:00
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;