saleor-dashboard/src/discounts/views/VoucherDetails.tsx

702 lines
32 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Button from "@material-ui/core/Button";
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 AssignCategoriesDialog from "@saleor/components/AssignCategoryDialog";
import AssignCollectionDialog from "@saleor/components/AssignCollectionDialog";
import AssignProductDialog from "@saleor/components/AssignProductDialog";
import { WindowTitle } from "@saleor/components/WindowTitle";
import useBulkActions from "@saleor/hooks/useBulkActions";
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
import usePaginator, {
createPaginationState
} from "@saleor/hooks/usePaginator";
import useShop from "@saleor/hooks/useShop";
import { commonMessages, sectionNames } from "@saleor/intl";
2019-06-19 14:40:52 +00:00
import { categoryUrl } from "../../categories/urls";
import { collectionUrl } from "../../collections/urls";
import { DEFAULT_INITIAL_SEARCH_DATA, PAGINATE_BY } from "../../config";
import SearchCategories from "../../containers/SearchCategories";
import SearchCollections from "../../containers/SearchCollections";
import SearchProducts from "../../containers/SearchProducts";
2019-08-09 11:14:35 +00:00
import { decimal, getMutationState, joinDateTime, maybe } from "../../misc";
2019-06-19 14:40:52 +00:00
import { productUrl } from "../../products/urls";
import {
DiscountValueTypeEnum,
2019-08-09 11:14:35 +00:00
VoucherTypeEnum
2019-06-19 14:40:52 +00:00
} from "../../types/globalTypes";
import DiscountCountrySelectDialog from "../components/DiscountCountrySelectDialog";
import VoucherDetailsPage, {
VoucherDetailsPageTab
} from "../components/VoucherDetailsPage";
import {
TypedVoucherCataloguesAdd,
TypedVoucherCataloguesRemove,
TypedVoucherDelete,
TypedVoucherUpdate
} from "../mutations";
import { TypedVoucherDetails } from "../queries";
2019-08-09 11:14:35 +00:00
import { RequirementsPicker } from "../types";
2019-06-19 14:40:52 +00:00
import { VoucherCataloguesAdd } from "../types/VoucherCataloguesAdd";
import { VoucherCataloguesRemove } from "../types/VoucherCataloguesRemove";
import { VoucherDelete } from "../types/VoucherDelete";
import { VoucherUpdate } from "../types/VoucherUpdate";
import {
voucherListUrl,
voucherUrl,
VoucherUrlDialog,
VoucherUrlQueryParams
} from "../urls";
interface VoucherDetailsProps {
id: string;
params: VoucherUrlQueryParams;
}
export const VoucherDetails: React.FC<VoucherDetailsProps> = ({
2019-06-19 14:40:52 +00:00
id,
params
}) => {
const navigate = useNavigator();
const paginate = usePaginator();
const notify = useNotifier();
const shop = useShop();
const { isSelected, listElements, reset, toggle, toggleAll } = useBulkActions(
params.ids
);
const intl = useIntl();
2019-06-19 14:40:52 +00:00
const paginationState = createPaginationState(PAGINATE_BY, params);
const changeTab = (tab: VoucherDetailsPageTab) => {
reset();
navigate(
voucherUrl(id, {
activeTab: tab
})
);
};
const handleVoucherDelete = (data: VoucherDelete) => {
if (data.voucherDelete.errors.length === 0) {
notify({
text: intl.formatMessage({
defaultMessage: "Deleted voucher"
2019-06-19 14:40:52 +00:00
})
});
navigate(voucherListUrl(), true);
}
};
const handleVoucherUpdate = (data: VoucherUpdate) => {
if (data.voucherUpdate.errors.length === 0) {
closeModal();
notify({
text: intl.formatMessage(commonMessages.savedChanges)
2019-06-19 14:40:52 +00:00
});
}
};
const closeModal = () =>
navigate(
voucherUrl(id, {
...params,
action: undefined
}),
true
);
const openModal = (action: VoucherUrlDialog, ids?: string[]) =>
navigate(
voucherUrl(id, {
...params,
action,
ids
})
);
const handleCatalogueAdd = (data: VoucherCataloguesAdd) => {
if (data.voucherCataloguesAdd.errors.length === 0) {
closeModal();
}
};
const handleCatalogueRemove = (data: VoucherCataloguesRemove) => {
if (data.voucherCataloguesRemove.errors.length === 0) {
closeModal();
reset();
}
};
const canOpenBulkActionDialog = maybe(() => params.ids.length > 0);
2019-06-19 14:40:52 +00:00
return (
<TypedVoucherCataloguesRemove onCompleted={handleCatalogueRemove}>
{(voucherCataloguesRemove, voucherCataloguesRemoveOpts) => (
<TypedVoucherCataloguesAdd onCompleted={handleCatalogueAdd}>
{(voucherCataloguesAdd, voucherCataloguesAddOpts) => (
<TypedVoucherUpdate onCompleted={handleVoucherUpdate}>
{(voucherUpdate, voucherUpdateOpts) => (
<TypedVoucherDelete onCompleted={handleVoucherDelete}>
{(voucherDelete, voucherDeleteOpts) => (
<TypedVoucherDetails
displayLoader
variables={{ id, ...paginationState }}
>
{({ data, loading }) => {
const tabPageInfo =
params.activeTab === VoucherDetailsPageTab.categories
? maybe(() => data.voucher.categories.pageInfo)
: params.activeTab ===
VoucherDetailsPageTab.collections
? maybe(() => data.voucher.collections.pageInfo)
: maybe(() => data.voucher.products.pageInfo);
const formTransitionState = getMutationState(
voucherUpdateOpts.called,
voucherUpdateOpts.loading,
maybe(
() => voucherUpdateOpts.data.voucherUpdate.errors
)
);
const assignTransitionState = getMutationState(
voucherCataloguesAddOpts.called,
voucherCataloguesAddOpts.loading,
maybe(
() =>
voucherCataloguesAddOpts.data.voucherCataloguesAdd
.errors
)
);
const unassignTransitionState = getMutationState(
voucherCataloguesRemoveOpts.called,
voucherCataloguesRemoveOpts.loading,
maybe(
() =>
voucherCataloguesRemoveOpts.data
.voucherCataloguesRemove.errors
)
);
const removeTransitionState = getMutationState(
voucherDeleteOpts.called,
voucherDeleteOpts.loading,
maybe(
() => voucherDeleteOpts.data.voucherDelete.errors
)
);
const handleCategoriesUnassign = (ids: string[]) =>
voucherCataloguesRemove({
variables: {
...paginationState,
id,
input: {
categories: ids
}
}
});
const handleCollectionsUnassign = (ids: string[]) =>
voucherCataloguesRemove({
variables: {
...paginationState,
id,
input: {
collections: ids
}
}
});
const handleProductsUnassign = (ids: string[]) =>
voucherCataloguesRemove({
variables: {
...paginationState,
id,
input: {
products: ids
}
}
});
const {
loadNextPage,
loadPreviousPage,
pageInfo
} = paginate(tabPageInfo, paginationState, params);
return (
<>
<WindowTitle
title={intl.formatMessage(sectionNames.vouchers)}
/>
2019-06-19 14:40:52 +00:00
<VoucherDetailsPage
defaultCurrency={maybe(
() => shop.defaultCurrency
)}
voucher={maybe(() => data.voucher)}
disabled={
loading || voucherCataloguesRemoveOpts.loading
}
errors={maybe(
() =>
voucherUpdateOpts.data.voucherUpdate.errors
)}
pageInfo={pageInfo}
onNextPage={loadNextPage}
onPreviousPage={loadPreviousPage}
onCategoryAssign={() =>
openModal("assign-category")
}
onCategoryClick={id => () =>
navigate(categoryUrl(id))}
onCollectionAssign={() =>
openModal("assign-collection")
}
onCollectionUnassign={collectionId =>
voucherCataloguesRemove({
variables: {
...paginationState,
id,
input: {
collections: [collectionId]
}
}
})
}
onCountryAssign={() =>
openModal("assign-country")
}
onCountryUnassign={countryCode =>
voucherUpdate({
variables: {
...paginationState,
id,
input: {
countries: data.voucher.countries
.filter(
country =>
country.code !== countryCode
)
.map(country => country.code)
}
}
})
}
onCategoryUnassign={categoryId =>
voucherCataloguesRemove({
variables: {
...paginationState,
id,
input: {
categories: [categoryId]
}
}
})
}
onCollectionClick={id => () =>
navigate(collectionUrl(id))}
onProductAssign={() =>
openModal("assign-product")
}
onProductUnassign={productId =>
voucherCataloguesRemove({
variables: {
...paginationState,
id,
input: {
products: [productId]
}
}
})
}
onProductClick={id => () =>
navigate(productUrl(id))}
activeTab={params.activeTab}
onBack={() => navigate(voucherListUrl())}
onTabClick={changeTab}
onSubmit={formData =>
voucherUpdate({
variables: {
id,
input: {
2019-08-09 11:14:35 +00:00
applyOncePerCustomer:
formData.applyOncePerCustomer,
applyOncePerOrder:
formData.applyOncePerOrder,
discountValue:
formData.discountType.toString() ===
"SHIPPING"
? 100
: decimal(formData.value),
discountValueType:
formData.discountType.toString() ===
"SHIPPING"
? DiscountValueTypeEnum.PERCENTAGE
: formData.discountType,
endDate: formData.hasEndDate
? joinDateTime(
formData.endDate,
formData.endTime
)
: null,
minAmountSpent:
formData.requirementsPicker !==
RequirementsPicker.ORDER
? 0
: parseFloat(formData.minAmountSpent),
minCheckoutItemsQuantity:
formData.requirementsPicker !==
RequirementsPicker.ITEM
? 0
: parseFloat(
formData.minCheckoutItemsQuantity
),
startDate: joinDateTime(
formData.startDate,
formData.startTime
2019-06-19 14:40:52 +00:00
),
2019-08-09 11:14:35 +00:00
type:
formData.discountType.toString() ===
"SHIPPING"
? VoucherTypeEnum.SHIPPING
: formData.type,
2019-09-10 16:51:38 +00:00
usageLimit: formData.hasUsageLimit
? parseInt(formData.usageLimit, 10)
: 0
2019-06-19 14:40:52 +00:00
}
}
})
}
onRemove={() => openModal("remove")}
saveButtonBarState={formTransitionState}
categoryListToolbar={
<Button
color="primary"
onClick={() =>
openModal("unassign-category", listElements)
}
>
<FormattedMessage
defaultMessage="Unassign"
description="unassign category from voucher, button"
id="voucherDetailsUnassignCategory"
/>
2019-06-19 14:40:52 +00:00
</Button>
}
collectionListToolbar={
<Button
color="primary"
onClick={() =>
openModal(
"unassign-collection",
listElements
)
}
>
<FormattedMessage
defaultMessage="Unassign"
description="unassign collection from voucher, button"
id="voucherDetailsUnassignCollection"
/>
2019-06-19 14:40:52 +00:00
</Button>
}
productListToolbar={
<Button
color="primary"
onClick={() =>
openModal("unassign-product", listElements)
}
>
<FormattedMessage
defaultMessage="Unassign"
description="unassign product from voucher, button"
id="voucherDetailsUnassignProduct"
/>
2019-06-19 14:40:52 +00:00
</Button>
}
isChecked={isSelected}
selected={listElements.length}
toggle={toggle}
toggleAll={toggleAll}
/>
<SearchCategories
variables={DEFAULT_INITIAL_SEARCH_DATA}
>
{({
search: searchCategories,
result: searchCategoriesOpts
}) => (
<AssignCategoriesDialog
categories={maybe(() =>
2019-10-15 12:17:35 +00:00
searchCategoriesOpts.data.search.edges
2019-06-19 14:40:52 +00:00
.map(edge => edge.node)
.filter(
suggestedCategory =>
suggestedCategory.id
)
)}
confirmButtonState={assignTransitionState}
open={params.action === "assign-category"}
onFetch={searchCategories}
loading={searchCategoriesOpts.loading}
onClose={closeModal}
2019-08-09 11:14:35 +00:00
onSubmit={categories =>
2019-06-19 14:40:52 +00:00
voucherCataloguesAdd({
variables: {
...paginationState,
id,
input: {
2019-08-09 11:14:35 +00:00
categories: categories.map(
2019-06-19 14:40:52 +00:00
product => product.id
)
}
}
})
}
/>
)}
</SearchCategories>
<SearchCollections
variables={DEFAULT_INITIAL_SEARCH_DATA}
>
{({
search: searchCollections,
result: searchCollectionsOpts
}) => (
<AssignCollectionDialog
collections={maybe(() =>
2019-10-15 12:17:35 +00:00
searchCollectionsOpts.data.search.edges
2019-06-19 14:40:52 +00:00
.map(edge => edge.node)
.filter(
suggestedCategory =>
suggestedCategory.id
)
)}
confirmButtonState={assignTransitionState}
open={params.action === "assign-collection"}
onFetch={searchCollections}
loading={searchCollectionsOpts.loading}
onClose={closeModal}
2019-08-09 11:14:35 +00:00
onSubmit={collections =>
2019-06-19 14:40:52 +00:00
voucherCataloguesAdd({
variables: {
...paginationState,
id,
input: {
2019-08-09 11:14:35 +00:00
collections: collections.map(
2019-06-19 14:40:52 +00:00
product => product.id
)
}
}
})
}
/>
)}
</SearchCollections>
<DiscountCountrySelectDialog
confirmButtonState={formTransitionState}
countries={maybe(() => shop.countries, [])}
onClose={() => navigate(voucherUrl(id))}
onConfirm={formData =>
voucherUpdate({
variables: {
id,
input: {
countries: formData.countries
}
}
})
}
open={params.action === "assign-country"}
initial={maybe(
() =>
data.voucher.countries.map(
country => country.code
),
[]
)}
/>
<SearchProducts
variables={DEFAULT_INITIAL_SEARCH_DATA}
>
{({
search: searchProducts,
result: searchProductsOpts
}) => (
<AssignProductDialog
confirmButtonState={assignTransitionState}
open={params.action === "assign-product"}
onFetch={searchProducts}
loading={searchProductsOpts.loading}
onClose={closeModal}
2019-08-09 11:14:35 +00:00
onSubmit={products =>
2019-06-19 14:40:52 +00:00
voucherCataloguesAdd({
variables: {
...paginationState,
id,
input: {
2019-08-09 11:14:35 +00:00
products: products.map(
2019-06-19 14:40:52 +00:00
product => product.id
)
}
}
})
}
products={maybe(() =>
2019-10-15 12:17:35 +00:00
searchProductsOpts.data.search.edges
2019-06-19 14:40:52 +00:00
.map(edge => edge.node)
.filter(
suggestedProduct => suggestedProduct.id
)
)}
/>
)}
</SearchProducts>
<ActionDialog
open={
params.action === "unassign-category" &&
canOpenBulkActionDialog
}
title={intl.formatMessage({
defaultMessage:
"Unassign Categories From Voucher",
description: "dialog header"
})}
2019-06-19 14:40:52 +00:00
confirmButtonState={unassignTransitionState}
onClose={closeModal}
onConfirm={() =>
handleCategoriesUnassign(params.ids)
}
>
{canOpenBulkActionDialog && (
<DialogContentText>
<FormattedMessage
defaultMessage="Are you sure you want to unassign {counter, plural,
one {this category}
other {{displayQuantity} categories}
}?"
description="dialog content"
values={{
counter: params.ids.length,
displayQuantity: (
<strong>{params.ids.length}</strong>
2019-06-19 14:40:52 +00:00
)
}}
/>
</DialogContentText>
)}
2019-06-19 14:40:52 +00:00
</ActionDialog>
<ActionDialog
open={
params.action === "unassign-collection" &&
canOpenBulkActionDialog
}
title={intl.formatMessage({
defaultMessage:
"Unassign Collections From Voucher",
description: "dialog header"
})}
2019-06-19 14:40:52 +00:00
confirmButtonState={unassignTransitionState}
onClose={closeModal}
onConfirm={() =>
handleCollectionsUnassign(params.ids)
}
>
{canOpenBulkActionDialog && (
<DialogContentText>
<FormattedMessage
defaultMessage="Are you sure you want to unassign {counter, plural,
one {this collection}
other {{displayQuantity} collections}
}?"
description="dialog content"
values={{
counter: params.ids.length,
displayQuantity: (
<strong>{params.ids.length}</strong>
2019-06-19 14:40:52 +00:00
)
}}
/>
</DialogContentText>
)}
2019-06-19 14:40:52 +00:00
</ActionDialog>
<ActionDialog
open={
params.action === "unassign-product" &&
canOpenBulkActionDialog
}
title={intl.formatMessage({
defaultMessage:
"Unassign Products From Voucher",
description: "dialog header"
})}
2019-06-19 14:40:52 +00:00
confirmButtonState={unassignTransitionState}
onClose={closeModal}
onConfirm={() =>
handleProductsUnassign(params.ids)
}
>
{canOpenBulkActionDialog && (
<DialogContentText>
<FormattedMessage
defaultMessage="Are you sure you want to unassign {counter, plural,
one {this product}
other {{displayQuantity} products}
}?"
description="dialog content"
values={{
counter: params.ids.length,
displayQuantity: (
<strong>{params.ids.length}</strong>
2019-06-19 14:40:52 +00:00
)
}}
/>
</DialogContentText>
)}
2019-06-19 14:40:52 +00:00
</ActionDialog>
<ActionDialog
open={params.action === "remove"}
title={intl.formatMessage({
defaultMessage: "Delete Voucher",
description: "dialog header"
})}
2019-06-19 14:40:52 +00:00
confirmButtonState={removeTransitionState}
onClose={closeModal}
variant="delete"
onConfirm={() =>
voucherDelete({
variables: { id }
})
}
>
<DialogContentText>
<FormattedMessage
defaultMessage="Are you sure you want to delete {voucherCode}?"
description="dialog content"
values={{
voucherCode: (
<strong>
{maybe(() => data.voucher.code, "...")}
</strong>
)
}}
/>
</DialogContentText>
2019-06-19 14:40:52 +00:00
</ActionDialog>
</>
);
}}
</TypedVoucherDetails>
)}
</TypedVoucherDelete>
)}
</TypedVoucherUpdate>
)}
</TypedVoucherCataloguesAdd>
)}
</TypedVoucherCataloguesRemove>
);
};
export default VoucherDetails;