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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
import style from '../styles/summary.module.css';
import React, { useEffect, useState, useMemo } from 'react';
import formatCurrency from '~/libs/formatCurrency';
import clsxm from '~/libs/clsxm';
import { Skeleton, Box, useColorModeValue, Text } from '@chakra-ui/react';
type Props = {
total?: number;
discount?: number;
subtotal?: number;
tax?: number;
shipping?: number;
grandTotal?: number;
isLoaded: boolean;
products?: any[]; // Added to detect changes in selected products
};
const CartSummary = ({
total,
discount,
subtotal,
tax,
shipping,
grandTotal,
isLoaded = false,
products = [],
}: Props) => {
const PPN: number = process.env.NEXT_PUBLIC_PPN
? parseFloat(process.env.NEXT_PUBLIC_PPN)
: 0;
const [isMounted, setIsMounted] = useState(false);
// Local state to store calculated values
const [summaryValues, setSummaryValues] = useState({
subtotal: 0,
discount: 0,
total: 0,
tax: 0,
shipping: 0,
grandTotal: 0,
});
// This fixes hydration issues by ensuring the component only renders fully after mounting
useEffect(() => {
setIsMounted(true);
}, []);
// Calculate summary based on products whenever products change
useMemo(() => {
if (!products || products.length === 0) return;
// Only count selected products
const selectedProducts = products.filter((product) => product.selected);
// Calculate values based on selected products
let calculatedSubtotal = 0;
let calculatedDiscount = 0;
selectedProducts.forEach((product) => {
// Get raw price and discount from product
const productBasePrice = product.price?.price || 0;
const productQty = product.quantity || 1;
const productDiscountedPrice =
product.price?.price_discount || productBasePrice;
const productDiscount = productBasePrice - productDiscountedPrice;
calculatedSubtotal += productBasePrice * productQty;
calculatedDiscount += productDiscount * productQty;
});
const calculatedTotal = calculatedSubtotal - calculatedDiscount;
const calculatedTax = calculatedTotal * (PPN - 1);
const calculatedShipping = shipping || 0;
const calculatedGrandTotal =
calculatedTotal + calculatedTax + calculatedShipping;
// If calculated values are different from props, use calculated ones
const shouldUpdateValues =
Math.abs((subtotal || 0) - calculatedSubtotal) > 0.01 ||
Math.abs((discount || 0) - calculatedDiscount) > 0.01 ||
Math.abs((total || 0) - calculatedTotal) > 0.01 ||
Math.abs((tax || 0) - calculatedTax) > 0.01 ||
Math.abs((grandTotal || 0) - calculatedGrandTotal) > 0.01;
if (shouldUpdateValues && isLoaded) {
setSummaryValues({
subtotal: calculatedSubtotal,
discount: calculatedDiscount,
total: calculatedTotal,
tax: calculatedTax,
shipping: calculatedShipping,
grandTotal: calculatedGrandTotal,
});
} else if (isLoaded) {
// Use values from props when available
setSummaryValues({
subtotal: subtotal || 0,
discount: discount || 0,
total: total || 0,
tax: tax || 0,
shipping: shipping || 0,
grandTotal: grandTotal || 0,
});
}
}, [
products,
isLoaded,
subtotal,
discount,
total,
tax,
shipping,
grandTotal,
PPN,
]);
// Update local values whenever props change
useEffect(() => {
if (isLoaded) {
setSummaryValues({
subtotal: subtotal || 0,
discount: discount || 0,
total: total || 0,
tax: tax || 0,
shipping: shipping || 0,
grandTotal: grandTotal || 0,
});
}
}, [isLoaded, subtotal, discount, total, tax, shipping, grandTotal]);
if (!isMounted) {
return (
<Box p={4} borderWidth='1px' borderRadius='lg' boxShadow='sm'>
<Text fontSize='lg' fontWeight='medium' mb={4}>
Ringkasan Pesanan
</Text>
{Array(6)
.fill(0)
.map((_, index) => (
<Skeleton key={index} height='24px' my={2} />
))}
</Box>
);
}
// Use local state for rendering to ensure responsiveness
const {
subtotal: displaySubtotal,
discount: displayDiscount,
total: displayTotal,
tax: displayTax,
shipping: displayShipping,
grandTotal: displayGrandTotal,
} = summaryValues;
return (
<div className={style.summaryContainer}>
<Text fontSize='lg' fontWeight='medium' mb={4}>
Ringkasan Pesanan
</Text>
<div className='flex flex-col gap-y-3'>
<Skeleton isLoaded={isLoaded} className={style.line}>
<span className={style.label}>Total Belanja</span>
<span className={style.value}>
Rp {formatCurrency(displaySubtotal)}
</span>
</Skeleton>
<Skeleton isLoaded={isLoaded} className={style.line}>
<span className={style.label}>Total Diskon</span>
<span className={clsxm(style.value, style.discount)}>
- Rp {formatCurrency(displayDiscount)}
</span>
</Skeleton>
<div className={style.divider} />
<Skeleton isLoaded={isLoaded} className={style.line}>
<span className={style.label}>Subtotal</span>
<span className={style.value}>Rp {formatCurrency(displayTotal)}</span>
</Skeleton>
<Skeleton isLoaded={isLoaded} className={style.line}>
<span className={style.label}>
Tax {((PPN - 1) * 100).toFixed(0)}%
</span>
<span className={style.value}>Rp {formatCurrency(displayTax)}</span>
</Skeleton>
<Skeleton isLoaded={isLoaded} className={style.line}>
<span className={style.label}>Biaya Kirim</span>
<span className={style.value}>
Rp {formatCurrency(displayShipping)}
</span>
</Skeleton>
<div className={style.divider} />
<Skeleton isLoaded={isLoaded} className={style.line}>
<span className={clsxm(style.label, style.grandTotal)}>
Grand Total
</span>
<span className={style.value}>
Rp {formatCurrency(grandTotal || 0)}
</span>
</Skeleton>
</div>
</div>
);
};
export default CartSummary;
|