This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
/
Page.tsx
117 lines (108 loc) · 3.01 KB
/
Page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import "./scss/index.scss";
import * as React from "react";
import { useAlert } from "react-alert";
import { Link } from "react-router-dom";
import { useUserDetails } from "@sdk/react";
import { CheckoutContextInterface } from "../../checkout/context";
import { baseUrl as checkoutUrl } from "../../checkout/routes";
import { Button, CartTable, EmptyCart, Loader } from "../../components";
import { CartInterface } from "../../components/CartProvider/context";
import {
extractCartLines,
extractCheckoutLines,
getTotal
} from "../../components/CartProvider/utils";
import { OverlayContextInterface } from "../../components/Overlay/context";
import { getShop_shop } from "../../components/ShopProvider/types/getShop";
import { maybe } from "../../core/utils";
import { checkoutLoginUrl } from "../../routes";
import { TypedProductVariantsQuery } from "../Product/queries";
interface PageProps {
checkout: CheckoutContextInterface;
overlay: OverlayContextInterface;
cart: CartInterface;
shop: getShop_shop;
}
const Page: React.FC<PageProps> = ({
shop: { geolocalization, defaultCountry },
checkout: {
checkout,
loading: checkoutLoading,
syncWithCart,
syncUserCheckout,
},
cart: {
lines,
remove,
add,
errors,
clearErrors,
subtract,
loading: cartLoading,
changeQuantity,
},
}) => {
const alert = useAlert();
const { data: user } = useUserDetails();
const hasErrors: boolean | null = maybe(() => !!errors.length);
const isLoading =
(!checkout && checkoutLoading) || syncWithCart || syncUserCheckout;
React.useEffect(() => {
if (hasErrors) {
alert.show(
{
content: errors.map(err => err.message).join(", "),
title: "Error",
},
{ type: "error" }
);
clearErrors();
}
}, [hasErrors]);
if (isLoading) {
return <Loader full />;
}
if (!lines.length) {
return <EmptyCart />;
}
const productTableProps = {
add,
changeQuantity,
invalid: maybe(() => !!errors.length, false),
processing: cartLoading,
remove,
subtract,
};
const locale = maybe(() => geolocalization.country.code, defaultCountry.code);
return (
<>
{checkout ? (
<CartTable
{...productTableProps}
lines={extractCheckoutLines(checkout.lines)}
subtotal={checkout.subtotalPrice.gross.localized}
/>
) : (
<TypedProductVariantsQuery
variables={{
ids: lines.map(line => line.variantId),
}}
>
{({ data }) => (
<CartTable
{...productTableProps}
lines={extractCartLines(data, lines, locale)}
subtotal={getTotal(data, lines, locale)}
/>
)}
</TypedProductVariantsQuery>
)}
<div className="cart-page__checkout-action">
<Link to={user ? checkoutUrl : checkoutLoginUrl}>
<Button disabled={cartLoading}>Proceed to Checkout</Button>
</Link>
</div>
</>
);
};
export default Page;