Skip to content

Commit

Permalink
Move disclosures to fetch-logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wpears committed Nov 6, 2023
1 parent c5000fe commit d1186de
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
import { promiseRequest } from '../utils/promise-request.js';
import financialView from '../views/financial-view.js';

/**
* getApi - Make an API request to the endpoint specified with parameters specified
* @param {string} url - URL of API endpoint
* @returns {object} Promise
*/
function getApi(url) {
return new Promise(function (resolve, reject) {
promiseRequest('GET', url)
.then(function (resp) {
resolve(resp);
})
.catch(function (error) {
reject(new Error(error));
});
});
}

const getApiValues = {
values: {},
constants: function () {
const urlBase = document.querySelector('main').getAttribute('data-context');
const url =
'/' + urlBase + '/understanding-your-financial-aid-offer/api/constants/';
return getApi(url);
return fetch(url);
},

expenses: function () {
const urlBase = document.querySelector('main').getAttribute('data-context');
const url =
'/' + urlBase + '/understanding-your-financial-aid-offer/api/expenses/';

return getApi(url);
return fetch(url);
},

fetchSchoolData: function (iped) {
Expand All @@ -44,15 +26,9 @@ const getApiValues = {
iped +
'/';

return new Promise(function (resolve ) {
promiseRequest('GET', url)
.then(function (resp) {
resolve(resp);
})
.catch(function (error) {
financialView.missingData('school');
new Error(error);
});
return fetch(url).catch(function (error) {
financialView.missingData('school');
return new Error(error);
});
},

Expand All @@ -77,26 +53,9 @@ const getApiValues = {
pid +
'/';

// $.ajax({
// url: url,
// dataType: 'json',
// success: function (resp) {
// return resp;
// },
// error: function (/* req, status, err */) {
// financialView.missingData('program');
// },
// });

return new Promise(function (resolve ) {
promiseRequest('GET', url)
.then(function (resp) {
resolve(resp);
})
.catch(function () {
financialView.missingData('school');
// reject(new Error(error));
});
return fetch(url).catch(function (error) {
financialView.missingData('school');
return new Error(error);
});
},

Expand All @@ -114,14 +73,14 @@ const getApiValues = {
url += '_' + pid + '/';
}

return getApi(url);
return fetch(url);
},

schoolData: function (iped, pid) {
return Promise.all([
this.fetchSchoolData(iped),
this.fetchProgramData(iped, pid),
this.fetchNationalData(iped, pid),
this.fetchSchoolData(iped).then((v) => v.json()),
this.fetchProgramData(iped, pid).then((v) => v.json()),
this.fetchNationalData(iped, pid).then((v) => v.json()),
]);
},

Expand All @@ -139,7 +98,10 @@ const getApiValues = {
});
return deferred;
}
return Promise.all([this.constants(), this.expenses()]);
return Promise.all([
this.constants().then((v) => v.json()),
this.expenses().then((v) => v.json()),
]);
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import $ from './utils/dollar-sign.js';
import fetch from './dispatchers/get-api-values.js';
import getApiValues from './dispatchers/get-api-values.js';
import verifyOffer from './dispatchers/post-verify.js';
import financialModel from './models/financial-model.js';
import schoolModel from './models/school-model.js';
Expand Down Expand Up @@ -39,9 +39,8 @@ const app = {
urlValues: {},
init: function () {
// jquery promise to delay full model creation until ajax resolves
fetch.initialData().then((resp) => {
const constants = JSON.parse(resp[0].responseText);
const expenses = JSON.parse(resp[1].responseText);
getApiValues.initialData().then((resp) => {
const [constants, expenses] = resp;
financialModel.init(constants[0]);
financialView.init();
if (location.href.indexOf('about-this-tool') === -1) {
Expand All @@ -51,12 +50,10 @@ const app = {
if (getUrlOfferExists()) {
// Check for URL offer data
this.urlValues = getUrlValues();
fetch
getApiValues
.schoolData(this.urlValues.collegeID, this.urlValues.programID)
.then((respArr) => {
const schoolData = JSON.parse(respArr[0].responseText);
const programData = JSON.parse(respArr[1].responseText);
const nationalData = JSON.parse(respArr[1].responseText);
const [schoolData, programData, nationalData] = respArr;
const data = {};
Object.assign(data, schoolData, programData, nationalData);
const schoolValues = schoolModel.init(
Expand Down

This file was deleted.

0 comments on commit d1186de

Please sign in to comment.