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