import DialogContentText from "@material-ui/core/DialogContentText"; import React from "react"; 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 useShop from "@saleor/hooks/useShop"; import i18n from "../../i18n"; import { getMutationState, maybe } from "../../misc"; import CustomerAddressDialog from "../components/CustomerAddressDialog"; import CustomerAddressListPage from "../components/CustomerAddressListPage"; import { TypedCreateCustomerAddressMutation, TypedRemoveCustomerAddressMutation, TypedSetCustomerDefaultAddressMutation, TypedUpdateCustomerAddressMutation } from "../mutations"; import { TypedCustomerAddressesQuery } from "../queries"; import { CreateCustomerAddress } from "../types/CreateCustomerAddress"; import { RemoveCustomerAddress } from "../types/RemoveCustomerAddress"; import { SetCustomerDefaultAddress } from "../types/SetCustomerDefaultAddress"; import { UpdateCustomerAddress } from "../types/UpdateCustomerAddress"; import { customerAddressesUrl, CustomerAddressesUrlDialog, CustomerAddressesUrlQueryParams, customerUrl } from "../urls"; interface CustomerAddressesProps { id: string; params: CustomerAddressesUrlQueryParams; } const CustomerAddresses: React.FC = ({ id, params }) => { const navigate = useNavigator(); const notify = useNotifier(); const shop = useShop(); const closeModal = () => navigate(customerAddressesUrl(id), true); const openModal = (action: CustomerAddressesUrlDialog, addressId?: string) => navigate(customerAddressesUrl(id, { action, id: addressId })); const handleSetAddressAsDefault = (data: SetCustomerDefaultAddress) => { if (data.addressSetDefault.errors.length === 0) { closeModal(); notify({ text: i18n.t("Set address as default", { context: "notification" }) }); } }; const handleAddressCreate = (data: CreateCustomerAddress) => { if (data.addressCreate.errors.length === 0) { closeModal(); } }; const handleAddressUpdate = (data: UpdateCustomerAddress) => { if (data.addressUpdate.errors.length === 0) { closeModal(); notify({ text: i18n.t("Updated address", { context: "notification" }) }); } }; const handleAddressRemove = (data: RemoveCustomerAddress) => { if (data.addressDelete.errors.length === 0) { closeModal(); notify({ text: i18n.t("Removed address", { context: "notification" }) }); } }; return ( {setCustomerDefaultAddress => ( {(createCustomerAddress, createCustomerAddressOpts) => ( {(updateCustomerAddress, updateCustomerAddressOpts) => ( {(removeCustomerAddress, removeCustomerAddressOpts) => ( {customerData => { const createAddressTransitionState = getMutationState( createCustomerAddressOpts.called, createCustomerAddressOpts.loading, maybe( () => createCustomerAddressOpts.data.addressCreate .errors, [] ) ); const updateAddressTransitionState = getMutationState( updateCustomerAddressOpts.called, updateCustomerAddressOpts.loading, maybe( () => updateCustomerAddressOpts.data.addressUpdate .errors, [] ) ); const removeAddressTransitionState = getMutationState( removeCustomerAddressOpts.called, removeCustomerAddressOpts.loading, maybe( () => removeCustomerAddressOpts.data.addressDelete .errors, [] ) ); const countryChoices = maybe( () => shop.countries.map(country => ({ code: country.code, label: country.country })), [] ); return ( <> customerData.data.user.email)} /> customerData.data.user)} disabled={customerData.loading} onAdd={() => openModal("add")} onBack={() => navigate(customerUrl(id))} onEdit={addressId => openModal("edit", addressId)} onRemove={addressId => openModal("remove", addressId) } onSetAsDefault={(addressId, type) => setCustomerDefaultAddress({ variables: { addressId, type, userId: id } }) } /> createCustomerAddressOpts.data.addressCreate .errors, [] )} open={params.action === "add"} variant="create" onClose={closeModal} onConfirm={input => createCustomerAddress({ variables: { id, input } }) } /> customerData.data.user.addresses.find( addr => addr.id === params.id ) )} confirmButtonState={updateAddressTransitionState} countries={countryChoices} errors={maybe( () => updateCustomerAddressOpts.data.addressUpdate .errors, [] )} open={params.action === "edit"} variant="edit" onClose={closeModal} onConfirm={input => updateCustomerAddress({ variables: { id: params.id, input } }) } /> removeCustomerAddress({ variables: { id: params.id } }) } > {i18n.t( "Are you sure you want to remove this address from users address book?" )} ); }} )} )} )} )} ); }; export default CustomerAddresses;