From d1186de98ab3797f2fbb764341414cf838d5082c Mon Sep 17 00:00:00 2001 From: Wyatt Pearsall Date: Mon, 6 Nov 2023 16:17:08 -0500 Subject: [PATCH] Move disclosures to fetch-logic --- .../disclosures/dispatchers/get-api-values.js | 70 +++++-------------- .../js/disclosures/index.js | 13 ++-- .../js/disclosures/utils/promise-request.js | 30 -------- 3 files changed, 21 insertions(+), 92 deletions(-) delete mode 100644 cfgov/unprocessed/apps/paying-for-college/js/disclosures/utils/promise-request.js diff --git a/cfgov/unprocessed/apps/paying-for-college/js/disclosures/dispatchers/get-api-values.js b/cfgov/unprocessed/apps/paying-for-college/js/disclosures/dispatchers/get-api-values.js index e3152d0593f..7653f106856 100755 --- a/cfgov/unprocessed/apps/paying-for-college/js/disclosures/dispatchers/get-api-values.js +++ b/cfgov/unprocessed/apps/paying-for-college/js/disclosures/dispatchers/get-api-values.js @@ -1,30 +1,12 @@ -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 () { @@ -32,7 +14,7 @@ const getApiValues = { 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('school'); + 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('school'); + 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()), ]); }, @@ -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()), + ]); }, }; diff --git a/cfgov/unprocessed/apps/paying-for-college/js/disclosures/index.js b/cfgov/unprocessed/apps/paying-for-college/js/disclosures/index.js index 0309f25a7c5..6d66de20523 100755 --- a/cfgov/unprocessed/apps/paying-for-college/js/disclosures/index.js +++ b/cfgov/unprocessed/apps/paying-for-college/js/disclosures/index.js @@ -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'; @@ -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) { @@ -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( diff --git a/cfgov/unprocessed/apps/paying-for-college/js/disclosures/utils/promise-request.js b/cfgov/unprocessed/apps/paying-for-college/js/disclosures/utils/promise-request.js deleted file mode 100644 index 5a9d4bcdd3d..00000000000 --- a/cfgov/unprocessed/apps/paying-for-college/js/disclosures/utils/promise-request.js +++ /dev/null @@ -1,30 +0,0 @@ -/* Promise requests are better. */ - -/** - * promiseRequest - A handy function for returning XHR Promises - * @param {string} method - The method, ex. POST or GET - * @param {string} url - The url to be requested - * @returns {object} Promise of the XHR request - */ -function promiseRequest(method, url) { - const xhr = new XMLHttpRequest(); - - return new Promise(function (resolve, reject) { - // Completed xhr - xhr.onreadystatechange = function () { - // Do not run unless xhr is complete - if (xhr.readyState !== 4) return; - if (xhr.status >= 200 && xhr.status < 300) { - resolve(xhr); - } else { - reject(new Error(xhr.status + ', ' + xhr.statusText)); - } - }; - - xhr.open(method, url, true); - - xhr.send(); - }); -} - -export { promiseRequest };