File size: 2,350 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { getDefaultPaymentMethodStep } from '../../src/components/default-steps';
import {
	CheckoutStepGroup,
	CheckoutStepBody,
	CheckoutStep,
	CheckoutFormSubmit,
} from '../../src/public-api';
import CheckoutOrderSummary from '../utils/checkout-order-summary';

export interface LineItem {
	type: string;
	id: string;
	label: string;
	amount: number;
}

function OrderReviewLineItem( { item }: { item: LineItem } ) {
	const itemSpanId = `checkout-line-item-${ item.id }`;
	return (
		<div>
			<span id={ itemSpanId }>{ item.label }</span>
			<span aria-labelledby={ itemSpanId }>{ item.amount }</span>
		</div>
	);
}

function OrderReviewLineItems( { items }: { items: LineItem[] } ) {
	return (
		<div>
			{ items.map( ( item ) => (
				<OrderReviewLineItem key={ item.id } item={ item } />
			) ) }
		</div>
	);
}

function OrderReviewTotal( { total }: { total: LineItem } ) {
	return (
		<div>
			<OrderReviewLineItem item={ total } />
		</div>
	);
}

function CheckoutReviewOrder( { items }: { items: LineItem[] } ) {
	const total = items.reduce( ( sum, item ) => sum + item.amount, 0 );

	return (
		<div className="checkout-review-order">
			<div>
				<OrderReviewLineItems items={ items } />
			</div>
			<div>
				<OrderReviewTotal total={ { id: 'total', type: 'total', label: 'Total', amount: total } } />
			</div>
		</div>
	);
}

export function DefaultCheckoutSteps( { items }: { items: LineItem[] } ) {
	const paymentMethodStep = getDefaultPaymentMethodStep();
	return (
		<CheckoutStepGroup>
			<CheckoutStepBody
				activeStepContent={ <CheckoutOrderSummary items={ items } /> }
				titleContent="Order summary"
				isStepActive={ false }
				isStepComplete
				stepNumber={ 1 }
				stepId="order-summary-step"
			/>
			<CheckoutStep
				stepId="review-order-step"
				className="checkout__review-order-step"
				isCompleteCallback={ () => true }
				activeStepContent={ <CheckoutReviewOrder items={ items } /> }
				titleContent="Review your order"
			/>
			<CheckoutStep
				stepId="payment-method-step"
				isCompleteCallback={ () => true }
				activeStepContent={ paymentMethodStep.activeStepContent }
				completeStepContent={ paymentMethodStep.completeStepContent }
				titleContent={ paymentMethodStep.titleContent }
				className={ paymentMethodStep.className }
			/>
			<CheckoutFormSubmit />
		</CheckoutStepGroup>
	);
}