diff --git a/domains/DataCollection/index.js b/domains/DataCollection/index.js index c47a7884f..7c06bb67d 100644 --- a/domains/DataCollection/index.js +++ b/domains/DataCollection/index.js @@ -19,7 +19,7 @@ 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'; import ComingSoonSVG from '../../assets/graphics/static/Adventurer.svg'; @@ -61,8 +61,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 65ddc1d21..5bc0d12d9 100644 --- a/modules/cached-resources/index.js +++ b/modules/cached-resources/index.js @@ -1,4 +1,9 @@ -import { residentQuery, cacheAutofillData } from './read'; +import { + residentQuery, + cacheAutofillData, + customFormsQuery, + getTasksAsync +} from './read'; import { postIdentificationForm, postSupplementaryForm, @@ -9,6 +14,8 @@ import { export { residentQuery, cacheAutofillData, + customFormsQuery, + getTasksAsync, postIdentificationForm, postSupplementaryForm, postHousehold, diff --git a/modules/cached-resources/read.js b/modules/cached-resources/read.js index b1f981ff1..4bf0f0596 100644 --- a/modules/cached-resources/read.js +++ b/modules/cached-resources/read.js @@ -1,7 +1,9 @@ -import { residentIDQuery } from '../../services/parse/crud'; +import { residentIDQuery, customQueryService } from '../../services/parse/crud'; import retrievePuenteAutofillData from '../../services/aws'; -import { storeData, getData } from '../async-storage'; import checkOnlineStatus from '../offline'; +import { getData, storeData } from '../async-storage'; +import checkOnlineStatus from '../offline'; +import getTasks from '../../services/tasky'; async function residentQuery(queryParams) { let records = await residentIDQuery(queryParams); @@ -19,10 +21,58 @@ async function cacheAutofillData(parameter) { } else { return getData('autofill_information')[parameter]; } + }) +} + +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, - cacheAutofillData + cacheAutofillData, + customFormsQuery, + getTasksAsync };