Skip to content

Commit

Permalink
formatted javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
sammdu committed Dec 13, 2021
1 parent 917b989 commit 32e5104
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 126 deletions.
89 changes: 50 additions & 39 deletions js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ limitations under the License.
*/
var api = {
// The URL root of the API backend
API_URL: "https://api.autodirect.tech",
API_URL: 'https://api.autodirect.tech',

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

Expand All @@ -30,14 +30,13 @@ var api = {

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

search: async function(searchQueryStr) {
search: async function (searchQueryStr) {
// send the search query to the backend API
const response = await fetch(this.API_URL + '/search' + searchQueryStr);

Expand All @@ -46,98 +45,110 @@ var api = {

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

getClaimedOffers: async function(userID) {
getClaimedOffers: async function (userID) {
// send the claimed offers query to the backend API
const response = await fetch(this.API_URL + '/getClaimedOffers?user_id=' + userID);
const response = await fetch(
this.API_URL + '/getClaimedOffers?user_id=' + userID
);

// retrieve the returned cars/offers
const results = await response.json();

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

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

// retrieve the offer details
const results = await response.json();

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

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

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

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

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

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

// retrieve the new offer details
const results = await response.json();

if (response.status == 200) {
return results;
}
else if (response.status == 406) {
} else if (response.status == 406) {
return {
'error': 'New loan principal does not result in a loan offer!'
'error': 'New loan principal does not result in a loan offer!',
};
}
else {
} else {
console.log(response);
throw "HTTP status: " + response.status;
throw 'HTTP status: ' + response.status;
}
}
},
};
70 changes: 45 additions & 25 deletions js/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/


/*
Always called when the details page is loaded.
*/
Expand Down Expand Up @@ -52,13 +51,12 @@ function goBack() {
*/
function highlightOffer(offerID) {
// give the offer a "selected" style
const offerEntry = document.querySelector('li[name="' + offerID +'"]');
offerEntry.className += " offer-selected";
const offerEntry = document.querySelector('li[name="' + offerID + '"]');
offerEntry.className += ' offer-selected';
// scroll the offer entry into view (if not already in-view)
offerEntry.scrollIntoView(false);
}


/*
Offer details operations
*/
Expand All @@ -77,12 +75,17 @@ async function getOfferDetails(userID, offerID) {

// display the offer details
renderOfferDetails(
details['brand'], details['model'], details['year'], details['kms'],
details['price'], details['loan_amount'], details['interest_rate'],
details['term_mo'], details['total_sum']
)
}
catch (e) {
details['brand'],
details['model'],
details['year'],
details['kms'],
details['price'],
details['loan_amount'],
details['interest_rate'],
details['term_mo'],
details['total_sum']
);
} catch (e) {
console.log(e);
}
}
Expand All @@ -91,13 +94,24 @@ async function getOfferDetails(userID, offerID) {
Present a given offer's details
*/
function renderOfferDetails(
make, model, year, kms, price, principal, interest_rate, loan_term, total_sum
make,
model,
year,
kms,
price,
principal,
interest_rate,
loan_term,
total_sum
) {
// get render target
let offerDetailsContainer = document.getElementById('offerDetailsContainer');
let offerDetailsContainer = document.getElementById(
'offerDetailsContainer'
);

// get mustache template
const tmpl_offerDetails = document.getElementById('tmpl_LoanDetails').innerHTML;
const tmpl_offerDetails =
document.getElementById('tmpl_LoanDetails').innerHTML;

// render loan offer info
const offerDetailsData = {
Expand All @@ -110,9 +124,12 @@ function renderOfferDetails(
interest_rate: Math.round(interest_rate * 100) / 100,
payment_mo: Math.round((total_sum / loan_term) * 100) / 100,
loan_term: loan_term,
total_sum: total_sum
total_sum: total_sum,
};
const offerDetailsRendered = Mustache.render(tmpl_offerDetails, offerDetailsData);
const offerDetailsRendered = Mustache.render(
tmpl_offerDetails,
offerDetailsData
);

// append loan offer element to offers container
offerDetailsContainer.innerHTML = offerDetailsRendered;
Expand All @@ -124,7 +141,7 @@ function renderOfferDetails(
async function submitNewPrincipal() {
// retrieve new principal info
const formNewPrincipal = document.getElementById('form-update-principal');
const newPrincipal = (new FormData(formNewPrincipal)).get('loan-principal');
const newPrincipal = new FormData(formNewPrincipal).get('loan-principal');

// attempt to update the specified offer's loan amount and fetch its details from the
// backend API
Expand All @@ -141,21 +158,24 @@ async function submitNewPrincipal() {
// make changes
if ('error' in details) {
alert(details['error']);
}
else {
} else {
// display the offer details
renderOfferDetails(
details['brand'], details['model'], details['year'], details['kms'],
details['price'], details['loan_amount'], details['interest_rate'],
details['term_mo'], details['total_sum']
)
details['brand'],
details['model'],
details['year'],
details['kms'],
details['price'],
details['loan_amount'],
details['interest_rate'],
details['term_mo'],
details['total_sum']
);
}
}
catch (e) {
} catch (e) {
console.log(e);
}
}


// always call this function on page load
onPageLoad();
Loading

0 comments on commit 32e5104

Please sign in to comment.