Skip to content

Commit

Permalink
bug fixes and DRY request code
Browse files Browse the repository at this point in the history
  • Loading branch information
meganrm committed Mar 26, 2020
1 parent 05ed9f3 commit cc7ee01
Showing 1 changed file with 54 additions and 47 deletions.
101 changes: 54 additions & 47 deletions src/state/networks/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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/')
}
})

Expand Down

0 comments on commit cc7ee01

Please sign in to comment.