Created
April 28, 2020 20:47
-
-
Save jordangarcia/af95ddb5cf35f67a43a270fcad6c9519 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable prefer-promise-reject-errors */ | |
/* eslint-disable compat/compat */ | |
import $ from 'jquery'; | |
import lumin from '../constants/lumin'; | |
const contentType = { 'Content-Type': 'application/json' }; | |
export const ajaxRequest = function ( | |
endpoint = '', | |
method = 'get', | |
data = {}, | |
headers = {}, | |
) { | |
return $.ajax({ | |
url: `${window.api_base_url}/${endpoint}`, | |
method, | |
data, | |
dataType: 'json', | |
headers: { | |
'X-Requested-With': 'XMLHttpRequest', | |
Accept: 'applications/x.skinco.v1+json', | |
Authorization: `Bearer ${lumin.user.token}`, | |
...headers, | |
}, | |
}); | |
}; | |
export const handleError = function (error, callback) { | |
// TODO: Check messsage is unauthenticated also | |
if (error && error.status === 500) { | |
window.location.assign('/?redirect=login'); | |
} else if (error) { | |
if (typeof callback === 'function') { | |
callback(); | |
} | |
} | |
}; | |
export const request = { | |
getAccountCredit(){ | |
return ajaxRequest('api/account/balance'); | |
}, | |
login(payload) { | |
return ajaxRequest('api/account/magic-link', 'post', payload); | |
}, | |
getDiscountAmount(couponCode, currency = lumin.user.currency) { | |
return ajaxRequest( | |
`api/coupons/code/${couponCode}/${currency.toString().toUpperCase()}` | |
); | |
}, | |
checkout: { | |
initiateCheckout(data) { | |
return ajaxRequest( | |
'api/checkout', | |
'post', | |
JSON.stringify(data), | |
contentType | |
); | |
}, | |
updateEmail(orderId, email, checkoutToken) { | |
return ajaxRequest( | |
`api/checkout/orders/${orderId}/email`, | |
'post', | |
JSON.stringify({ email }), | |
{ | |
...contentType, | |
'Checkout-Token': checkoutToken, | |
} | |
); | |
}, | |
updateShipping(orderId, shippingAddress, checkoutToken) { | |
return ajaxRequest( | |
`api/checkout/orders/${orderId}/shipping-address`, | |
'post', | |
JSON.stringify(shippingAddress), | |
{ | |
...contentType, | |
'Checkout-Token': checkoutToken, | |
} | |
); | |
}, | |
updateBilling(orderId, billingAddress, checkoutToken) { | |
return ajaxRequest( | |
`api/checkout/orders/${orderId}/billing-address`, | |
'post', | |
JSON.stringify(billingAddress), | |
{ | |
...contentType, | |
'Checkout-Token': checkoutToken, | |
} | |
); | |
}, | |
completeCheckout(orderId, stripeToken, checkoutToken) { | |
return ajaxRequest( | |
`api/checkout/orders/${orderId}/submit`, | |
'post', | |
JSON.stringify({stripe_token: stripeToken}), | |
{ | |
...contentType, | |
'Checkout-Token': checkoutToken, | |
} | |
); | |
}, | |
getOrder(orderId, checkoutToken) { | |
return ajaxRequest( | |
`api/checkout/orders/${orderId}`, | |
'get', | |
'', | |
{ | |
'Checkout-Token': checkoutToken, | |
} | |
); | |
}, | |
}, | |
subscription: { | |
getLastSubscription() { | |
return ajaxRequest('api/account/last-subscription'); | |
}, | |
getAllSubscriptions() { | |
return ajaxRequest('api/subscriptions'); | |
}, | |
updateAddress(subscriptionId, addressData) { | |
return ajaxRequest( | |
`api/subscriptions/${subscriptionId}/address/update`, | |
'post', | |
addressData | |
); | |
}, | |
updatePayment(subscriptionId, token) { | |
return ajaxRequest( | |
`api/subscriptions/${subscriptionId}/billing`, | |
'post', | |
{ payment_token: token } | |
); | |
}, | |
delayNextOrder(subscriptionId, newDate) { | |
return ajaxRequest(`api/subscriptions/${subscriptionId}/delay`, 'post', { | |
next_order_date: newDate.toISOString(), | |
}); | |
}, | |
cancelSubscription(subscriptionId, data) { | |
return ajaxRequest( | |
`api/subscriptions/${subscriptionId}/cancel`, | |
'post', | |
data | |
); | |
}, | |
checkEarlyCancellation(subscriptionId) { | |
return ajaxRequest( | |
`api/subscriptions/${subscriptionId}/cancel/alert`, | |
'post' | |
); | |
}, | |
updateSubscription(subscriptionId, messageBody) { | |
return ajaxRequest( | |
`api/account/updateSubscription/${subscriptionId}`, | |
'post', | |
{ message_body: messageBody } | |
); | |
}, | |
applyCoupon(subscriptionId, coupon) { | |
return ajaxRequest( | |
`api/subscriptions/${subscriptionId}/coupons/${coupon}`, | |
'post', | |
{ coupon } | |
); | |
}, | |
}, | |
kv: { | |
getAll() { | |
return ajaxRequest( | |
'api/user/kv', | |
).catch((e) => { | |
window.TrackJS && window.TrackJS.track('kv.getAll:', e); | |
}); | |
}, | |
getByKey(key) { | |
return ajaxRequest( | |
`api/user/kv/${key}`, | |
'get', | |
).catch(() => { | |
return undefined; | |
}); | |
}, | |
set({ key, value }) { | |
return ajaxRequest( | |
`api/user/kv/${key}`, | |
'put', | |
{ key, value }, | |
).catch((e) => { | |
window.TrackJS && window.TrackJS.track('kv.set:', e); | |
}); | |
}, | |
deleteByKey(key) { | |
return ajaxRequest( | |
`api/user/kv/${key}`, | |
'delete', | |
).catch((e) => { | |
window.TrackJS && window.TrackJS.track('kv.deleteByKey:', e); | |
}); | |
} | |
}, | |
}; | |
export const RequestQueue = function (queue = []) { | |
this.addRequest = function (requestInstance) { | |
let result = { then() {}, catch() {}, finally() {} }; | |
if ( | |
Array.isArray(queue) && | |
typeof requestInstance === 'object' && | |
requestInstance.abort | |
) { | |
queue.push(requestInstance); | |
result = requestInstance; | |
} | |
return result; | |
}; | |
this.clearRequests = function () { | |
queue.forEach((currentRequest) => { | |
currentRequest.abort(); | |
}); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment