-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move disclosures to fetch-logic #8008
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
|
@@ -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('noSchool'); | ||
return new Error(error); | ||
}); | ||
}, | ||
|
||
|
@@ -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('noProgram'); | ||
return new Error(error); | ||
}); | ||
}, | ||
|
||
|
@@ -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()), | ||
]); | ||
}, | ||
|
||
|
@@ -130,16 +89,14 @@ const getApiValues = { | |
const warning = document | ||
.querySelector('[data-warning]') | ||
.getAttribute('data-warning'); | ||
if (warning !== '' && typeof warning !== 'undefined') { | ||
if (warning && !window.Cypress) { | ||
financialView.missingData(warning); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this code know about Cypress? It seems like whatever the need is here should be offloaded to the test logic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this is kinda gross, the problem is though that the data isn't available in the test db, so this warning code will always trigger, even though the actual http requests are stubbed. Putting this logic here allows Cypress to skip the warning code (which we want) and, in normal use of the app, allows the warning conditional to trigger when we need it. I think maybe the best solution would be rearchitecting how the warning logic works/how the warning is propagated from the backend to the frontend, but that seemed out of scope. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'd be happy to fast-follow that because this is pretty gross. 😁 |
||
const deferred = {}; | ||
deferred.promise = new Promise((resolve, reject) => { | ||
deferred.resolve = resolve; | ||
deferred.reject = reject; | ||
}); | ||
return deferred; | ||
return Promise.resolve(); | ||
} | ||
return Promise.all([this.constants(), this.expenses()]); | ||
return Promise.all([ | ||
this.constants().then((v) => v.json()), | ||
this.expenses().then((v) => v.json()), | ||
]); | ||
}, | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/** | ||
Check warning on line 1 in cfgov/unprocessed/apps/paying-for-college/js/disclosures/utils/dollar-sign.js GitHub Actions / frontend
|
||
* | ||
* @param selector | ||
Check warning on line 3 in cfgov/unprocessed/apps/paying-for-college/js/disclosures/utils/dollar-sign.js GitHub Actions / frontend
Check warning on line 3 in cfgov/unprocessed/apps/paying-for-college/js/disclosures/utils/dollar-sign.js GitHub Actions / frontend
|
||
*/ | ||
function Query(selector) { | ||
this.elements = []; | ||
|
@@ -17,17 +17,11 @@ | |
if (typeof selector === 'string' && selector !== '') { | ||
this.elements = document.querySelectorAll(selector); | ||
} | ||
|
||
// if ( this.elements.length > 1 ) { | ||
// return this.elements; | ||
// } else { | ||
// return this.elements[0]; | ||
// } | ||
} | ||
|
||
Query.prototype.attr = function (name, value) { | ||
if (typeof value === 'undefined') { | ||
return this.elements[0].getAttribute(name); | ||
return this.elements.length ? this.elements[0].getAttribute(name) : null; | ||
} else { | ||
this.elements.forEach((elem) => { | ||
elem.setAttribute(name, value); | ||
|
@@ -36,13 +30,13 @@ | |
}; | ||
|
||
Query.prototype.cloner = function () { | ||
return this.elements[0].cloneNode(true); | ||
return this.elements.length ? this.elements[0].cloneNode(true) : null; | ||
}; | ||
|
||
Query.prototype.text = function (value) { | ||
// getter | ||
if (typeof value === 'undefined') { | ||
return this.elements[0].textContent; | ||
return this.elements.length ? this.elements[0].textContent : null; | ||
} | ||
//setter | ||
else { | ||
|
@@ -55,7 +49,7 @@ | |
Query.prototype.val = function (value) { | ||
// getter | ||
if (typeof value === 'undefined' && this.elements.length > 0) { | ||
return this.elements[0].value; | ||
return this.elements.length ? this.elements[0].value : null; | ||
} | ||
//setter | ||
else { | ||
|
@@ -146,19 +140,11 @@ | |
}; | ||
|
||
Query.prototype.height = function () { | ||
if (this.elements.length > 0) { | ||
return this.elements[0].offsetHeight; | ||
} else { | ||
return null; | ||
} | ||
return this.elements.length ? this.elements[0].offsetHeight : null; | ||
}; | ||
|
||
Query.prototype.top = function () { | ||
if (this.elements.length > 0) { | ||
return this.elements[0].offsetTop; | ||
} else { | ||
return null; | ||
} | ||
return this.elements.length ? this.elements[0].offsetTop : null; | ||
}; | ||
|
||
Query.prototype.listen = function (eventType, callback) { | ||
|
@@ -168,7 +154,7 @@ | |
}; | ||
|
||
Query.prototype.tagName = function () { | ||
return this.elements[0].tagName; | ||
return this.elements.length ? this.elements[0].tagName : null; | ||
}; | ||
|
||
Query.prototype.addClass = function (classNames) { | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of all the conditionals, would it make sense to have something like this at the top:
And then
{{ school_primary_alias }}
in the spots where it is used?If you do keep the conditions, perhaps format them the same as
{% if school %}
, instead of a mix of that and{% if school%}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I just did it with a mass replace in vim ^^;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 79c3f37