From 42a415d505ecefdf5bb53ced3a593530a6932d1c Mon Sep 17 00:00:00 2001 From: Joseph McCombs Date: Mon, 7 Dec 2020 21:50:18 -0500 Subject: [PATCH 1/2] feat: api request cached --- domains/DataCollection/index.js | 5 ++- domains/HomeScreen/index.js | 4 +- modules/cached-resources/index.js | 8 +++- modules/cached-resources/read.js | 61 ++++++++++++++++++++++++++++++- package-lock.json | 2 +- 5 files changed, 72 insertions(+), 8 deletions(-) diff --git a/domains/DataCollection/index.js b/domains/DataCollection/index.js index c47a7884f..18ea0ccc1 100644 --- a/domains/DataCollection/index.js +++ b/domains/DataCollection/index.js @@ -20,6 +20,7 @@ import { layout } from '../../modules/theme'; import I18n from '../../modules/i18n'; import { customQueryService } from '../../services/parse/crud'; +import { customFormsQuery } from '../../modules/cached-resources'; import { retrieveSignOutFunction } from '../../services/parse/auth'; import ComingSoonSVG from '../../assets/graphics/static/Adventurer.svg'; @@ -61,8 +62,8 @@ const DataCollection = ({ navigation }) => { }).catch(() => { setSurveyingOrganization(surveyingOrganization || ''); }); - customQueryService(0, 5000, 'FormSpecificationsV2', 'organizations', surveyingOrganization).then((forms) => { - setCustomForms(JSON.parse(JSON.stringify(forms))); + customFormsQuery(surveyingOrganization).then((forms) => { + setCustomForms(forms); }); }, [surveyingUser, surveyingOrganization]); diff --git a/domains/HomeScreen/index.js b/domains/HomeScreen/index.js index 34370d766..ebc05ba56 100644 --- a/domains/HomeScreen/index.js +++ b/domains/HomeScreen/index.js @@ -7,7 +7,7 @@ import { } from 'react-native-paper'; import { ScrollView } from 'react-native-gesture-handler'; -import getTasks from '../../services/tasky'; +import { getTasksAsync } from '../../modules/cached-resources'; import { retrieveSignOutFunction } from '../../services/parse/auth'; import { deleteData } from '../../modules/async-storage'; @@ -23,7 +23,7 @@ const HomeScreen = (props) => { const { navigation } = props; const showTasks = async () => { - await getTasks().then((result) => { + await getTasksAsync().then((result) => { setTasks(result); }); }; diff --git a/modules/cached-resources/index.js b/modules/cached-resources/index.js index ecdad8784..0dd18b0d2 100644 --- a/modules/cached-resources/index.js +++ b/modules/cached-resources/index.js @@ -1,4 +1,8 @@ -import residentQuery from './read'; +import { + residentQuery, + customFormsQuery, + getTasksAsync +} from './read'; import { postIdentificationForm, postSupplementaryForm, @@ -8,6 +12,8 @@ import { export { residentQuery, + customFormsQuery, + getTasksAsync, postIdentificationForm, postSupplementaryForm, postHousehold, diff --git a/modules/cached-resources/read.js b/modules/cached-resources/read.js index fab218d38..501d91379 100644 --- a/modules/cached-resources/read.js +++ b/modules/cached-resources/read.js @@ -1,7 +1,64 @@ -import { residentIDQuery } from '../../services/parse/crud'; +import { residentIDQuery, customQueryService } from '../../services/parse/crud'; +import checkOnlineStatus from '../offline'; +import { getData, storeData } from '../async-storage'; +import getTasks from '../../services/tasky'; -export default async function residentQuery(queryParams) { +async function residentQuery(queryParams) { let records = await residentIDQuery(queryParams); records = JSON.parse(JSON.stringify(records)); return records; } + +function customFormsQuery(surveyingOrganization) { + return new Promise((resolve, reject) => { + checkOnlineStatus().then((online) => { + if (online) { + customQueryService(0, 5000, 'FormSpecificationsV2', 'organizations', surveyingOrganization).then(async (forms) => { + await storeData(forms, 'customForms'); + resolve(JSON.parse(JSON.stringify(forms))); + }, (error) => { + reject(error); + }) + } + else { + getData('customForms').then((forms) => { + resolve(forms); + }, (error) => { + reject(error); + }) + } + }, (error) => { + reject(error); + }) + }) +} + +function getTasksAsync() { + return new Promise((resolve, reject) => { + checkOnlineStatus().then(async (online) => { + if (online) { + await getTasks().then(async (result) => { + await storeData(result, 'tasks'); + resolve(result); + }, (error) => { + reject(error); + }) + } + else { + getData('tasks').then((tasks) => { + resolve(tasks); + }, (error) => { + reject(error); + }) + } + }, (error) => { + reject(error); + }) + }) +} + +export { + residentQuery, + customFormsQuery, + getTasksAsync +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 0b3ecbb48..423e5470c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "puente-reactnative", - "version": "8.6.2", + "version": "8.8.1", "lockfileVersion": 1, "requires": true, "dependencies": { From 7873265e1c9ad1eaf79d3cfee6bddb547d1255f4 Mon Sep 17 00:00:00 2001 From: Joseph McCombs Date: Mon, 7 Dec 2020 22:57:40 -0500 Subject: [PATCH 2/2] chore: linting --- domains/DataCollection/index.js | 1 - modules/cached-resources/read.js | 24 +++++++++++------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/domains/DataCollection/index.js b/domains/DataCollection/index.js index 18ea0ccc1..7c06bb67d 100644 --- a/domains/DataCollection/index.js +++ b/domains/DataCollection/index.js @@ -19,7 +19,6 @@ import { deleteData, getData } from '../../modules/async-storage'; import { layout } from '../../modules/theme'; import I18n from '../../modules/i18n'; -import { customQueryService } from '../../services/parse/crud'; import { customFormsQuery } from '../../modules/cached-resources'; import { retrieveSignOutFunction } from '../../services/parse/auth'; diff --git a/modules/cached-resources/read.js b/modules/cached-resources/read.js index 501d91379..e7657788b 100644 --- a/modules/cached-resources/read.js +++ b/modules/cached-resources/read.js @@ -18,19 +18,18 @@ function customFormsQuery(surveyingOrganization) { resolve(JSON.parse(JSON.stringify(forms))); }, (error) => { reject(error); - }) - } - else { + }); + } else { getData('customForms').then((forms) => { resolve(forms); }, (error) => { reject(error); - }) + }); } }, (error) => { reject(error); - }) - }) + }); + }); } function getTasksAsync() { @@ -42,23 +41,22 @@ function getTasksAsync() { resolve(result); }, (error) => { reject(error); - }) - } - else { + }); + } else { getData('tasks').then((tasks) => { resolve(tasks); }, (error) => { reject(error); - }) + }); } }, (error) => { reject(error); - }) - }) + }); + }); } export { residentQuery, customFormsQuery, getTasksAsync -} \ No newline at end of file +};