2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-08-26 13:59:32 +00:00
|
|
|
import { useIntl } from "react-intl";
|
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";
|
|
|
|
import useShop from "@saleor/hooks/useShop";
|
2019-08-26 13:59:32 +00:00
|
|
|
import { sectionNames } from "@saleor/intl";
|
2019-10-24 13:25:26 +00:00
|
|
|
import { decimal, getMutationState, joinDateTime, maybe } from "../../misc";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { DiscountValueTypeEnum, SaleType } from "../../types/globalTypes";
|
|
|
|
import SaleCreatePage from "../components/SaleCreatePage";
|
|
|
|
import { TypedSaleCreate } from "../mutations";
|
|
|
|
import { SaleCreate } from "../types/SaleCreate";
|
|
|
|
import { saleListUrl, saleUrl } from "../urls";
|
|
|
|
|
|
|
|
function discountValueTypeEnum(type: SaleType): DiscountValueTypeEnum {
|
|
|
|
return type.toString() === DiscountValueTypeEnum.FIXED
|
|
|
|
? DiscountValueTypeEnum.FIXED
|
|
|
|
: DiscountValueTypeEnum.PERCENTAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const SaleDetails: React.StatelessComponent = () => {
|
|
|
|
const navigate = useNavigator();
|
|
|
|
const pushMessage = useNotifier();
|
|
|
|
const shop = useShop();
|
2019-08-26 13:59:32 +00:00
|
|
|
const intl = useIntl();
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
const handleSaleCreate = (data: SaleCreate) => {
|
|
|
|
if (data.saleCreate.errors.length === 0) {
|
|
|
|
pushMessage({
|
2019-08-26 13:59:32 +00:00
|
|
|
text: intl.formatMessage({
|
|
|
|
defaultMessage: "Successfully created sale"
|
2019-06-19 14:40:52 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
navigate(saleUrl(data.saleCreate.sale.id), true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TypedSaleCreate onCompleted={handleSaleCreate}>
|
|
|
|
{(saleCreate, saleCreateOpts) => {
|
|
|
|
const formTransitionState = getMutationState(
|
|
|
|
saleCreateOpts.called,
|
|
|
|
saleCreateOpts.loading,
|
|
|
|
maybe(() => saleCreateOpts.data.saleCreate.errors)
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2019-08-26 13:59:32 +00:00
|
|
|
<WindowTitle title={intl.formatMessage(sectionNames.sales)} />
|
2019-06-19 14:40:52 +00:00
|
|
|
<SaleCreatePage
|
|
|
|
defaultCurrency={maybe(() => shop.defaultCurrency)}
|
|
|
|
disabled={saleCreateOpts.loading}
|
|
|
|
errors={maybe(() => saleCreateOpts.data.saleCreate.errors)}
|
|
|
|
onBack={() => navigate(saleListUrl())}
|
|
|
|
onSubmit={formData =>
|
|
|
|
saleCreate({
|
|
|
|
variables: {
|
|
|
|
input: {
|
2019-10-24 13:25:26 +00:00
|
|
|
endDate: formData.hasEndDate
|
|
|
|
? joinDateTime(formData.endDate, formData.endTime)
|
|
|
|
: null,
|
2019-06-19 14:40:52 +00:00
|
|
|
name: formData.name,
|
2019-10-24 13:25:26 +00:00
|
|
|
startDate: joinDateTime(
|
|
|
|
formData.startDate,
|
|
|
|
formData.startTime
|
|
|
|
),
|
2019-06-19 14:40:52 +00:00
|
|
|
type: discountValueTypeEnum(formData.type),
|
|
|
|
value: decimal(formData.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
saveButtonBarState={formTransitionState}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedSaleCreate>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default SaleDetails;
|