Skip to content

Commit

Permalink
fix: async currentUser logic updated
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-mccombs committed Jan 23, 2021
1 parent e6d5bb7 commit 806b1b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
24 changes: 8 additions & 16 deletions domains/Auth/PinCode/GetPinCode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import FormInput from '../../../../components/FormikFields/FormInput';
import { storeData, getData, deleteData } from '../../../../modules/async-storage';
import { retrieveSignInFunction, retrieveCurrentUserAsyncFunction } from '../../../../services/parse/auth';
import I18n from '../../../../modules/i18n';
import { populateCache } from '../../../../modules/cached-resources'

const GetPinCode = ({ navigation }) => {
const [failedAttempts, setFailedAttempts] = useState(1);
Expand All @@ -20,18 +21,9 @@ const GetPinCode = ({ navigation }) => {
getData('credentials')
.then((userCreds) => {
retrieveSignInFunction(userCreds.username, userCreds.password)
.then(() => {
const currentUser = retrieveCurrentUserAsyncFunction();
getData('currentUser').then((user) => {
if (user !== currentUser) {
storeData(currentUser, 'currentUser');
}
});
getData('organization').then((organization) => {
if (organization !== currentUser.organization) {
storeData(currentUser.organization, 'organization');
}
});
.then((currentUser) => {
console.log("Pin coder")
populateCache(currentUser);
});
navigation.navigate('Root');
}, () => {
Expand Down Expand Up @@ -70,10 +62,10 @@ const GetPinCode = ({ navigation }) => {
{formikProps.isSubmitting ? (
<ActivityIndicator />
) : (
<Button onPress={formikProps.handleSubmit}>
<Text>{I18n.t('global.submit')}</Text>
</Button>
)}
<Button onPress={formikProps.handleSubmit}>
<Text>{I18n.t('global.submit')}</Text>
</Button>
)}
</>
)}
</Formik>
Expand Down
23 changes: 18 additions & 5 deletions modules/cached-resources/populate-cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cacheAutofillData } from './read';
import { retrieveCurrentUserAsyncFunction } from '../../services/parse/auth';
import { storeData } from '../async-storage';
import { getData, storeData } from '../async-storage';

export default function populateCache(user) {
// communities called since we need a paramter, all data would be cached in the
Expand All @@ -10,16 +10,29 @@ export default function populateCache(user) {

})
.then(async () => {
const currentUserAsync = await getData('currentUser');
const currentOrgAsync = await getData('organization')
// store information after sign up/sign in
if (user) {
await storeData(user.get('organization'), 'organization');
await storeData(user, 'currentUser');
if (user !== currentUserAsync) {
await storeData(user, 'currentUser');
}
if (user.get('organization') !== currentOrgAsync) {
await storeData(user.get('organization'), 'organization');
}
} else {
// fail safe in case no user is passed in for some reason
await retrieveCurrentUserAsyncFunction()
.then(async (currentUser) => {
await storeData(currentUser.organization, 'organization');
await storeData(currentUser, 'currentUser');
if (currentUser !== null && currentUser !== undefined) {
if (currentUser !== currentUserAsync) {
console.log("stored yser")
await storeData(currentUser, 'currentUser');
}
if (currentUser.organization !== currentOrgAsync) {
await storeData(currentUser.organization, 'organization');
}
}
});
}
});
Expand Down

0 comments on commit 806b1b3

Please sign in to comment.