Skip to content

Commit

Permalink
claim and unclaim offers work
Browse files Browse the repository at this point in the history
  • Loading branch information
sammdu committed Dec 9, 2021
1 parent 86e6981 commit 7007591
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ <h5>Total cost:</h5>
</button>
</script>
<script type="text/html" id="tmpl_CarOfferBtnAvailable">
<button type="button" class="claim-btn rounded-bottom" name="{{id}}" onclick="claimOffer(this.name);">
<button type="button" class="claim-btn rounded-bottom" name="{{id}}" onclick="toggleClaimOffer(this.name);">
<span class="btn-dark-blue claim-text">Claim this offer</span>
<span class="btn-red bg-heart-empty claim-heart"></span>
</button>
Expand Down
15 changes: 15 additions & 0 deletions js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ var api = {
this.API_URL + '/claimOffer?user_id=' + userID + '&offer_id=' + OfferID
);

if (response.status == 200) {
return;
}
else {
console.log(response);
throw "HTTP status: " + response.status;
}
},

unclaimOffer: async function(userID, OfferID) {
// send the search query to the backend API
const response = await fetch(
this.API_URL + '/unclaimOffer?user_id=' + userID + '&offer_id=' + OfferID
);

if (response.status == 200) {
return;
}
Expand Down
45 changes: 29 additions & 16 deletions js/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ async function onPageLoad() {
// log in the user
await userLogin();
// fetch and display the user's claimed offers, if there is any
const userOffer = await fetchClaimedOffers(fetchQueryParamByKey('user_id'));
if (userOffer !== null) {
const offersDetailsLink = document.getElementById('offersDetailsLink');
offersDetailsLink.style.visibility = 'visible';
}
await updateClaimedOffers(fetchQueryParamByKey('user_id'));
}
}

Expand Down Expand Up @@ -301,11 +297,25 @@ function setCarsOffersLoading() {
Claimed loan offers operations
*/

/*
Update the list of claimed offers and associated display
*/
async function updateClaimedOffers(userID) {
const userOffer = await fetchClaimedOffers(userID);
const offersDetailsLink = document.getElementById('offersDetailsLink');
if (userOffer !== null) {
offersDetailsLink.style.visibility = 'visible';
}
else {
removeAllLoanOffers();
offersDetailsLink.style.visibility = 'hidden';
}
}

/*
Visually toggle the specified claim button between claimed and unclaimed
*/
function toggleButtonClaimed(id) {
let claimButton = document.querySelector('button.claim-btn[name="' + id +'"]');
function toggleButtonClaimed(claimButton) {
for (let part of claimButton.children) {
// modify the text component
if (part.className.includes('claim-text')) {
Expand Down Expand Up @@ -339,22 +349,25 @@ function toggleButtonClaimed(id) {
/*
Claim a given loan offer
*/
async function claimOffer(id) {
async function toggleClaimOffer(id) {
let claimButton = document.querySelector('button.claim-btn[name="' + id +'"]');
const userID = fetchQueryParamByKey('user_id');
// attempt to claim the specified offer at the backend API
try {
// make the API call
await api.claimOffer(userID, id);
// offer is unclaimed, so claim it
if (claimButton.innerHTML.includes("Claim this offer")) {
await api.claimOffer(userID, id);
}
// offer is claimed, so unclaim it
else {
await api.unclaimOffer(userID, id);
}

// display the offer details
toggleButtonClaimed(id);
toggleButtonClaimed(claimButton);

// update the claimed offers widget, fetch and display the user's claimed offers
const userOffer = await fetchClaimedOffers(userID);
if (userOffer !== null) {
const offersDetailsLink = document.getElementById('offersDetailsLink');
offersDetailsLink.style.visibility = 'visible';
}
await updateClaimedOffers(userID);
}
catch (e) {
console.log(e);
Expand Down
4 changes: 2 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ function addOfferToContainer(
Remove all loan offers in the claimed offers container.
*/
function removeAllLoanOffers() {
let carsContainer = document.getElementById('loanOffersContainer');
carsContainer.innerHTML = '';
let loanOffersContainer = document.getElementById('loanOffersContainer');
loanOffersContainer.innerHTML = '';
}

0 comments on commit 7007591

Please sign in to comment.