handle purchasing a JamTrack
handle adding to cart of redeemable vs paid jamtracks
This commit is contained in:
parent
3245024925
commit
5d87d1a358
|
|
@ -8,34 +8,60 @@ import { useShoppingCart } from '../../hooks/useShoppingCart';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import JKTooltip from '../common/JKTooltip';
|
import JKTooltip from '../common/JKTooltip';
|
||||||
|
import { placeOrder, updateUser } from '../../helpers/rest';
|
||||||
|
|
||||||
const JKJamTrackPurchaseButton = ({ jamTrack }) => {
|
const JKJamTrackPurchaseButton = ({ jamTrack }) => {
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const { currentUser } = useAuth();
|
const { currentUser } = useAuth();
|
||||||
const { addCartItem } = useShoppingCart();
|
const { addCartItem, getCartItems, hasOnlyFreeItemsInShoppingCart } = useShoppingCart();
|
||||||
const { t } = useTranslation('jamtracks');
|
const { t } = useTranslation('jamtracks');
|
||||||
|
|
||||||
|
|
||||||
const addToCart = async () => {
|
const addToCart = async () => {
|
||||||
|
if (!currentUser) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const options = {
|
const options = {
|
||||||
id: jamTrack.id,
|
id: jamTrack.id,
|
||||||
variant: 'full'
|
variant: 'full'
|
||||||
};
|
};
|
||||||
if (await addCartItem(options)) {
|
|
||||||
|
try {
|
||||||
|
const resp = await addCartItem(options);
|
||||||
|
console.log('resp1', resp);
|
||||||
|
if(resp.fast_reedem){ //if this is a free jamtrack
|
||||||
|
//get shopping cart items and see if all are free
|
||||||
|
if(!hasOnlyFreeItemsInShoppingCart()){
|
||||||
|
history.push('/jamtracks');
|
||||||
|
}else{
|
||||||
|
const purchadeResp = await placeOrder();
|
||||||
|
console.log('resp2', purchadeResp);
|
||||||
|
if(purchadeResp.ok){
|
||||||
|
const userResp = await updateUser(currentUser.id);
|
||||||
|
console.log('resp3', userResp);
|
||||||
|
if(userResp.ok){
|
||||||
|
history.push('/checkout/success?free=yes&jamtrackId=' + jamTrack.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}else{ //if this is a paid jamtrack
|
||||||
toast.success(t('search.list.add_success_alert'));
|
toast.success(t('search.list.add_success_alert'));
|
||||||
history.push('/shopping-cart');
|
history.push('/shopping-cart');
|
||||||
} else {
|
|
||||||
console.log('Add to Cart Error');
|
|
||||||
toast.error(t('search.list.add_error_alert'));
|
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
toast.error(t('search.list.add_error_alert'));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{jamTrack.purchaed ? (
|
{jamTrack.purchased ? (
|
||||||
<Button color="light" size="sm" className="mr-1">
|
|
||||||
{t('search.list.purchased')}
|
<span>{t('search.list.purchased')}</span>
|
||||||
</Button>
|
|
||||||
) : jamTrack.allow_free && currentUser && currentUser.show_free_jamtrack ? (
|
) : jamTrack.allow_free && currentUser && currentUser.show_free_jamtrack ? (
|
||||||
<>
|
<>
|
||||||
<Button color="primary" onClick={addToCart} size="sm" className="mr-1">
|
<Button color="primary" onClick={addToCart} size="sm" className="mr-1">
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,58 @@
|
||||||
import React from 'react'
|
import React, { useEffect, useState } from 'react';
|
||||||
import { Card, CardBody } from 'reactstrap'
|
import { Card, CardBody } from 'reactstrap';
|
||||||
import FalconCardHeader from '../common/FalconCardHeader'
|
import FalconCardHeader from '../common/FalconCardHeader';
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from 'react-i18next';
|
||||||
import { Link } from 'react-router-dom'
|
import { Link, useLocation } from 'react-router-dom';
|
||||||
|
import { getJamTrack } from '../../helpers/rest';
|
||||||
|
|
||||||
const JKCheckoutSuccess = () => {
|
const JKCheckoutSuccess = () => {
|
||||||
const {t} = useTranslation('checkoutSuccess')
|
const { t } = useTranslation('checkoutSuccess');
|
||||||
|
const search = useLocation().search;
|
||||||
|
const [free, setFree] = useState(false);
|
||||||
|
const [jamtrack, setJamtrack] = useState(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!search) return;
|
||||||
|
const params = new URLSearchParams(search);
|
||||||
|
setFree(params.get('free') === 'yes');
|
||||||
|
const jamTrackId = params.get('jamtrackId');
|
||||||
|
if (jamTrackId) {
|
||||||
|
getJamTrack({ id: jamTrackId }).then(resp => {
|
||||||
|
if (resp.ok) {
|
||||||
|
resp.json().then(data => {
|
||||||
|
setJamtrack(data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [search]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card>
|
<Card>
|
||||||
<FalconCardHeader title={t('page_title')} titleClass="font-weight-semi-bold" />
|
<FalconCardHeader title={t('page_title')} titleClass="font-weight-semi-bold" />
|
||||||
<CardBody className="pt-0 text-center mt-4">
|
<CardBody className="pt-0 text-center mt-5 mb-5">
|
||||||
<p className="text-muted">Thank you for your order! We'll send you an order confirmation email shortly.</p>
|
{free ? (
|
||||||
|
<>
|
||||||
<p>
|
<p>
|
||||||
|
Thank you for getting your first JamTrack
|
||||||
|
{jamtrack && (
|
||||||
|
<>
|
||||||
|
"{jamtrack.name}" free! Click the button below to start playing with your JamTrack in your
|
||||||
|
browser. <br />
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
|
{jamtrack && (
|
||||||
|
<Link to={`/jamtracks/${jamtrack.id}`} className="btn btn-primary">
|
||||||
|
{t('open_jamtrack')}
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<p>
|
||||||
|
Thank you for your order! We'll send you an order confirmation email shortly.
|
||||||
|
<br />
|
||||||
Click the button below to start using your new JamTracks.
|
Click the button below to start using your new JamTracks.
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
|
|
@ -19,14 +60,21 @@ const JKCheckoutSuccess = () => {
|
||||||
{t('my_jamtracks')}
|
{t('my_jamtracks')}
|
||||||
</Link>
|
</Link>
|
||||||
</p>
|
</p>
|
||||||
<div>
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="mt-6">
|
||||||
<p>
|
<p>
|
||||||
You can also play with your JamTracks in the <a href="https://www.jamkazam.com/downloads" target='_blank'>JamKazam desktop app</a>, available for Windows and Mac.
|
You can also play with your JamTracks in the{' '}
|
||||||
|
<a href="https://www.jamkazam.com/downloads" target="_blank">
|
||||||
|
JamKazam desktop app
|
||||||
|
</a>
|
||||||
|
, available for Windows and Mac.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</CardBody>
|
</CardBody>
|
||||||
</Card>
|
</Card>
|
||||||
)
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export default JKCheckoutSuccess
|
export default JKCheckoutSuccess;
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ export const useShoppingCart = () => {
|
||||||
const resp = await addJamtrackToShoppingCart(options);
|
const resp = await addJamtrackToShoppingCart(options);
|
||||||
const data = await resp.json();
|
const data = await resp.json();
|
||||||
setShoppingCart([...shoppingCart, data]);
|
setShoppingCart([...shoppingCart, data]);
|
||||||
return true;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -57,7 +57,11 @@ export const useShoppingCart = () => {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const hasOnlyFreeItemsInShoppingCart = () => {
|
||||||
|
return shoppingCart.length === 0 || shoppingCart.every(item => item.product_info.free);
|
||||||
|
}
|
||||||
|
|
||||||
return{
|
return{
|
||||||
shoppingCart, error, loading, removeCartItem, addCartItem, cartTotal
|
shoppingCart, error, loading, removeCartItem, addCartItem, cartTotal, hasOnlyFreeItemsInShoppingCart
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
{
|
{
|
||||||
"page_title": "Thank You!",
|
"page_title": "Thank You!",
|
||||||
"my_jamtracks": "My JamTracks"
|
"my_jamtracks": "My JamTracks",
|
||||||
|
"open_jamtrack": "Open JamTrack"
|
||||||
}
|
}
|
||||||
|
|
@ -29,12 +29,14 @@ child(:jam_track_tracks => :tracks) {
|
||||||
|
|
||||||
node do |jam_track|
|
node do |jam_track|
|
||||||
right = jam_track.right_for_user(current_user)
|
right = jam_track.right_for_user(current_user)
|
||||||
|
if right
|
||||||
{
|
{
|
||||||
jam_track_right_id: right.id,
|
jam_track_right_id: right.id,
|
||||||
purchased_at: right.created_at.to_i,
|
purchased_at: right.created_at.to_i,
|
||||||
last_mixdown_id: right.last_mixdown_id,
|
last_mixdown_id: right.last_mixdown_id,
|
||||||
last_stem_id: right.last_stem_id
|
last_stem_id: right.last_stem_id
|
||||||
}
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
node :mixdowns do |jam_track|
|
node :mixdowns do |jam_track|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue