From cc7ee0116ddda9198e979d14dbcd549b70f88d6d Mon Sep 17 00:00:00 2001 From: meganrm Date: Thu, 26 Mar 2020 15:57:35 -0700 Subject: [PATCH] bug fixes and DRY request code --- src/state/networks/logic.js | 101 +++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 47 deletions(-) diff --git a/src/state/networks/logic.js b/src/state/networks/logic.js index 0a71cd4..c9ac5e8 100644 --- a/src/state/networks/logic.js +++ b/src/state/networks/logic.js @@ -9,8 +9,15 @@ import { import { REQUEST_NETWORKS, } from "./reducers"; -import { GENERAL, REQUEST_SUPPORT, OFFER_SUPPORT, INFORMATION } from "../constants"; -import { setPageOfNetworks } from "./actions"; +import { + GENERAL, + REQUEST_SUPPORT, + OFFER_SUPPORT, + INFORMATION +} from "../constants"; +import { + setPageOfNetworks +} from "./actions"; const fetchNetworks = createLogic({ type: REQUEST_NETWORKS, @@ -19,52 +26,52 @@ const fetchNetworks = createLogic({ const { httpClient, } = deps; - - const requestPage = (token) => { - return httpClient.get(`https://firestore.googleapis.com/v1/projects/townhallproject-86312/databases/(default)/documents/mutual_aid_networks/?pageToken=${token}`) - .then((snapshot) => { - const pageOfNetworks = snapshot.body.documents.map((doc, index) => { - const data = doc.fields; - const unpackedData = mapValues(data, (object) => { - let newValues = values(object)[0]; - if (newValues.values) { - newValues = map(newValues.values, obj => { - return values(obj)[0]; - }); - } - return newValues; - }) - let category; - if (unpackedData.generalForm || (unpackedData.supportRequestForm && unpackedData.supportOfferForm)) { - category = GENERAL; - } else if (unpackedData.supportRequestForm) { - category = REQUEST_SUPPORT; - } else if (unpackedData.supportOfferForm) { - category = OFFER_SUPPORT; - } else { - category = INFORMATION; - } - return { - ...unpackedData, - id: index, - category: category, - } - }); - dispatch(setPageOfNetworks(pageOfNetworks)) - if (snapshot.body.nextPageToken) { - return requestPage(snapshot.body.nextPageToken) - } - done(); - }) - } - - return httpClient.get('https://firestore.googleapis.com/v1/projects/townhallproject-86312/databases/(default)/documents/mutual_aid_networks/') - .then((snapshot) => { - if (snapshot.body.nextPageToken) { - return requestPage(snapshot.body.nextPageToken); + let globalIndex = 0; + const processOnePage = (snapshot) => { + return snapshot.body.documents.map((doc) => { + globalIndex++; + const data = doc.fields; + const unpackedData = mapValues(data, (object) => { + let newValues = values(object)[0]; + if (newValues.values) { + newValues = map(newValues.values, obj => { + return values(obj)[0]; + }); + } + return newValues; + }) + let category; + if (unpackedData.generalForm || (unpackedData.supportRequestForm && unpackedData.supportOfferForm)) { + category = GENERAL; + } else if (unpackedData.supportRequestForm) { + category = REQUEST_SUPPORT; + } else if (unpackedData.supportOfferForm) { + category = OFFER_SUPPORT; + } else { + category = INFORMATION; } - return done(); - }) + return { + ...unpackedData, + id: globalIndex, + category: category, + } + }); + } + + const requestPage = (url) => { + return httpClient.get(url) + .then((snapshot) => { + const pageOfNetworks = processOnePage(snapshot) + dispatch(setPageOfNetworks(pageOfNetworks)) + if (snapshot.body.nextPageToken) { + const url = `https://firestore.googleapis.com/v1/projects/townhallproject-86312/databases/(default)/documents/mutual_aid_networks/?pageToken=${snapshot.body.nextPageToken}` + return requestPage(url) + } + done(); + }) + } + + requestPage('https://firestore.googleapis.com/v1/projects/townhallproject-86312/databases/(default)/documents/mutual_aid_networks/') } })