2019-06-19 14:40:52 +00:00
|
|
|
import DialogContentText from "@material-ui/core/DialogContentText";
|
|
|
|
import ActionDialog from "@saleor/components/ActionDialog";
|
2020-05-14 09:30:32 +00:00
|
|
|
import NotFoundPage from "@saleor/components/NotFoundPage";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
2019-08-23 12:23:59 +00:00
|
|
|
import { commonMessages } from "@saleor/intl";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
|
|
|
import { FormattedMessage, useIntl } from "react-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";
|
2020-10-22 11:33:29 +00:00
|
|
|
import CustomerDetailsPage, {
|
|
|
|
CustomerDetailsPageFormData
|
|
|
|
} from "../components/CustomerDetailsPage/CustomerDetailsPage";
|
2019-06-19 14:40:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
export const CustomerDetailsView: React.FC<CustomerDetailsViewProps> = ({
|
|
|
|
id,
|
|
|
|
params
|
|
|
|
}) => {
|
2019-06-19 14:40:52 +00:00
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
2019-08-23 12:23:59 +00:00
|
|
|
const intl = useIntl();
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
const handleCustomerUpdateSuccess = (data: UpdateCustomer) => {
|
|
|
|
if (data.customerUpdate.errors.length === 0) {
|
|
|
|
notify({
|
2020-07-01 09:39:36 +00:00
|
|
|
status: "success",
|
2019-08-23 12:23:59 +00:00
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const handleCustomerRemoveSuccess = (data: RemoveCustomer) => {
|
|
|
|
if (data.customerDelete.errors.length === 0) {
|
|
|
|
notify({
|
2020-07-01 09:39:36 +00:00
|
|
|
status: "success",
|
2019-08-23 12:23:59 +00:00
|
|
|
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} />;
|
|
|
|
}
|
|
|
|
|
2020-10-22 11:33:29 +00:00
|
|
|
const handleSubmit = async (
|
|
|
|
data: CustomerDetailsPageFormData
|
|
|
|
) => {
|
|
|
|
const result = await updateCustomer({
|
|
|
|
variables: {
|
|
|
|
id,
|
|
|
|
input: {
|
|
|
|
email: data.email,
|
|
|
|
firstName: data.firstName,
|
|
|
|
isActive: data.isActive,
|
|
|
|
lastName: data.lastName,
|
|
|
|
note: data.note
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result.data.customerUpdate.errors;
|
|
|
|
};
|
|
|
|
|
2020-02-20 14:18:22 +00:00
|
|
|
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))}
|
2020-10-22 11:33:29 +00:00
|
|
|
onSubmit={handleSubmit}
|
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;
|