|
|
|
@ -21,6 +21,7 @@ CheckoutSummary.getInitialProps = async function({ctx: {axios}}){
|
|
|
|
|
export default function CheckoutSummary({order: _order}){
|
|
|
|
|
const user = useUser();
|
|
|
|
|
const [order, updateOrder] = useState(_order)
|
|
|
|
|
const [couponError, setCouponError] = useState(null)
|
|
|
|
|
|
|
|
|
|
const currentTransaction = order
|
|
|
|
|
.transactions.find(transaction => (
|
|
|
|
@ -45,9 +46,22 @@ export default function CheckoutSummary({order: _order}){
|
|
|
|
|
const onCouponSubmit = async ev => {
|
|
|
|
|
if(ev) ev.preventDefault()
|
|
|
|
|
|
|
|
|
|
setCouponError(null)
|
|
|
|
|
|
|
|
|
|
const code = couponRef.current?.value
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const {data: updatedOrder} = await axios.post(`/api/orders/current/coupon`, {code})
|
|
|
|
|
console.log(order, updatedOrder)
|
|
|
|
|
updateOrder(updatedOrder)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err.response.data.errors)
|
|
|
|
|
const message = err?.response?.data?.error?.message
|
|
|
|
|
|| err.response?.data?.errors?.find(e => e.param === 'code')?.msg
|
|
|
|
|
|| 'Unknown error applying coupon'
|
|
|
|
|
|
|
|
|
|
setCouponError(message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For Stripe checkout
|
|
|
|
@ -127,9 +141,9 @@ export default function CheckoutSummary({order: _order}){
|
|
|
|
|
)
|
|
|
|
|
: (
|
|
|
|
|
<div className={styles.horizContainer} style={{maxWidth:'400px', margin: '0 auto'}}>
|
|
|
|
|
<Input inputRef={couponRef} label="" name="coupon" />
|
|
|
|
|
<div style={{maxWidth: '120px', marginLeft: '8px'}}>
|
|
|
|
|
<Button onClick={onCouponSubmit} outline>Save Coupon</Button>
|
|
|
|
|
<Input inputRef={couponRef} label="" name="coupon" onChange={() => setCouponError(null)} error={couponError} />
|
|
|
|
|
<div style={{alignSelf:'flex-start', maxWidth: '120px', marginLeft: '8px'}}>
|
|
|
|
|
<Button onClick={onCouponSubmit} outline>Apply Coupon</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|