Skip to content

Instantly share code, notes, and snippets.

@hoorayimhelping
Created June 19, 2016 19:33
Show Gist options
  • Select an option

  • Save hoorayimhelping/a80d908631ea2337b5064cac6b14f94c to your computer and use it in GitHub Desktop.

Select an option

Save hoorayimhelping/a80d908631ea2337b5064cac6b14f94c to your computer and use it in GitHub Desktop.
diff --git a/package.json b/package.json
index db836d6..dfe2fca 100644
--- a/package.json
+++ b/package.json
@@ -60,7 +60,7 @@
"node-sass": "^3.4.2",
"parse-link-header": "^0.4.1",
"purecss": "^0.6.0",
- "qh-common": "quartethealth/qh-common#v2.8.2",
+ "qh-common": "quartethealth/qh-common#v2.8.4",
"react": "^0.14.1",
"react-addons-css-transition-group": "^0.14.7",
"react-dom": "^0.14.1",
diff --git a/server.js b/server.js
index 8c34b06..944bec7 100755
--- a/server.js
+++ b/server.js
@@ -30,6 +30,7 @@ import { getAppointments } from './src/api/appointment';
import { searchUsers } from './src/api/users';
import { getNoteByServiceRequestId, addNoteToServiceRequest } from './src/api/service-request-note'
import { beforeEveryRequest, defaultErrorHandler } from './src/middleware/';
+import { createRequestId } from 'qh-common/server/middleware/request';
import {
APP_SECRET,
@@ -83,6 +84,7 @@ app.use(cookieSession({
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ extended: true }));
+app.use(createRequestId);
app.use(beforeEveryRequest);
app.get('/status', (req, res) => res.sendStatus(200));
diff --git a/src/actions/application.js b/src/actions/application.js
index a676820..0c07eee 100755
--- a/src/actions/application.js
+++ b/src/actions/application.js
@@ -2,9 +2,10 @@ import * as constants from '../constants';
import { Automaton } from 'qh-common/utils';
import AdminRequest, { request } from '../utils/client-request';
import Immutable from 'immutable';
-import { REQUEST_LOAD_SIZE, MAX_WAIT_SERVICE_LAUNCH } from '../settings';
+import { REQUEST_LOAD_SIZE, MAX_WAIT_SERVICE_LAUNCH, NUM_SERVICE_REQUESTS_LOAD } from '../settings';
import { Map } from 'immutable';
import { logFrontend } from '../utils/log-front-end';
+import { wait } from '../utils/timing';
const adminRequest = new AdminRequest();
@@ -110,21 +111,19 @@ export const getReferralsByIds = (serviceRequestIds, request = adminRequest) =>
};
export const loadPCPServiceRequests = (offset = 0, request = adminRequest) => {
- async function load(dispatch, offset = 0) {
+ const load = async (dispatch, offset = 0) => {
const res = await request.get(
constants.BFF_ENDPOINT_SERVICE_REQUEST, {
limit: REQUEST_LOAD_SIZE,
offset: offset
// default ordering is -created
}
- ).catch(err => {
- return { err };
- });
+ );
if (res && res.ok) {
const parsedRes = JSON.parse(res.text);
if (offset > parsedRes.count) {
- return { ok: true };
+ return Promise.resolve(parsedRes.count);
}
const results = Immutable.fromJS(parsedRes);
@@ -133,31 +132,25 @@ export const loadPCPServiceRequests = (offset = 0, request = adminRequest) => {
serviceRequests: results
});
- return { ok: true };
+ return Promise.resolve(results.size);
}
};
return async dispatch => {
- let i = 0;
// meaning this is the first load of the page
- if (!offset || offset === 0) {
- while (i < 5) {
- let offset = i * REQUEST_LOAD_SIZE;
- window.setTimeout(async function() {
- let n = 0;
- while (n < 3) {
- const res = await load(dispatch, offset);
- if (res && res.ok) {
- break;
- }
- n++;
- }
- }, i * 800);
- i++;
- }
- } else {
- load(dispatch, offset);
+ if (offset && offset !== 0) {
+ return load(dispatch, offset);
+ }
+
+ let resultsCount = null;
+ let i = 0;
+ do {
+ let offset = i * REQUEST_LOAD_SIZE;
+ await wait(500);
+ resultsCount = await load(dispatch, offset);
+ i++;
}
+ while (resultsCount !== 0);
};
};
diff --git a/src/middleware/index.js b/src/middleware/index.js
index ac4f8b0..defbbea 100644
--- a/src/middleware/index.js
+++ b/src/middleware/index.js
@@ -1,7 +1,7 @@
import { logger } from '../utils/log';
export const beforeEveryRequest = (req, res, next) => {
- logger.info({ req, res });
+ logger.info({ req });
next();
};
diff --git a/src/utils/server-request.js b/src/utils/server-request.js
index 8b7f98f..9437a08 100644
--- a/src/utils/server-request.js
+++ b/src/utils/server-request.js
@@ -1,7 +1,8 @@
import { COLLAB_AUTH_NAME, COLLAB_AUTH_PASSWORD } from '../settings';
export { USER_ID_HEADER, REQUEST_ID_HEADER } from '../settings';
-import request, { defaultHeadersFromReq } from 'qh-common/server/utils/server-request';
+import request from 'qh-common/server/utils/server-request';
+import { defaultHeadersFromReq } from 'qh-common/server/utils/express-request';
for (let method of ['get', 'post', 'put', 'patch', 'del']) {
let oldMethod = request[method];
diff --git a/src/utils/timing.js b/src/utils/timing.js
new file mode 100644
index 0000000..ce63757
--- /dev/null
+++ b/src/utils/timing.js
@@ -0,0 +1,5 @@
+export const wait = waitFor => {
+ return new Promise(fullfil => {
+ setTimeout(() => fullfil(), waitFor);
+ });
+};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment