aboutsummaryrefslogtreecommitdiff
path: root/src/utils/users.ts
diff options
context:
space:
mode:
authorAshm Walia <40498934+ashmgarv@users.noreply.github.com>2020-12-04 08:50:24 -0800
committerGitHub <noreply@github.com>2020-12-04 11:50:24 -0500
commit0fd892ad288f2e1eaaa4fdf5e1fd6f15dbd45860 (patch)
treed7d53d94c6c4026ac9b325508ebce4706d412ac4 /src/utils/users.ts
parentf620102190629e0b6f180d3ce056d850b1db5aaa (diff)
[TMA - 398 AND TMA-430] Replace Providers with Redux Store (#125)
* First * WIP * Thunk * Some more comments * sc * recent searches and follounfollow * Edit profile dummy * Block / unblock and some cleanup * Replace auth provider * Sc * Delete AP after rebase * Discover users * Cleanup * More cleanup * Replace profile provider * Fixed build failure * Fixed a bug reported * Prevent app crash when backend server is down
Diffstat (limited to 'src/utils/users.ts')
-rw-r--r--src/utils/users.ts97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/utils/users.ts b/src/utils/users.ts
new file mode 100644
index 00000000..30b9d77b
--- /dev/null
+++ b/src/utils/users.ts
@@ -0,0 +1,97 @@
+import {RootState} from './../store/rootReducer';
+import AsyncStorage from '@react-native-community/async-storage';
+import {AppDispatch} from './../store/configureStore';
+import {UserType, ScreenType} from './../types/types';
+import {INTEGRATED_SOCIAL_LIST} from '../constants';
+import {
+ loadAllSocials,
+ loadBlockedList,
+ loadFollowData,
+ loadRecentlySearched,
+ loadUserData,
+ loadUserMoments,
+} from '../store/actions';
+import {NO_SOCIAL_ACCOUNTS} from '../store/initialStates';
+import {loadSocialPosts} from '../services';
+import {userLoggedIn} from '../store/reducers';
+
+const loadData = async (dispatch: AppDispatch, user: UserType) => {
+ await Promise.all([
+ dispatch(loadUserData(user)),
+ dispatch(loadFollowData(user.userId)),
+ dispatch(loadUserMoments(user.userId)),
+ dispatch(loadAllSocials(user.userId)),
+ dispatch(loadBlockedList(user.userId)),
+ dispatch(loadRecentlySearched()),
+ ]);
+};
+
+/**
+ * This tries to log the user in present with the AsyncStorage if user.userId is empty
+ * Else it tries to login the user passed in
+ * @param dispatch This is the dispatch object from the redux store
+ * @param user The user if at all any
+ */
+export const userLogin = async (dispatch: AppDispatch, user: UserType) => {
+ try {
+ let localUser = {...user};
+ if (!user.userId) {
+ const [id, username, token] = await Promise.all([
+ AsyncStorage.getItem('userId'),
+ AsyncStorage.getItem('username'),
+ AsyncStorage.getItem('token'),
+ ]);
+ if (id && username && token) {
+ localUser = {...localUser, userId: id, username: username};
+ } else {
+ return;
+ }
+ }
+ await loadData(dispatch, localUser);
+ } catch (error) {
+ console.log(error);
+ }
+};
+
+/**
+ * This function checks if the userX slice of our store contains the given user for the provided Screen
+ */
+export const userXInStore = (
+ state: RootState,
+ screen: ScreenType,
+ userId: string,
+) => {
+ const userX = state.userX[screen];
+ return userId in userX && userX[userId].user.userId;
+};
+
+/**
+ * Abstracted the code to laod all socials out.
+ * @param userId userId for whom socials should be fetched
+ */
+export const loadAllSocialsForUser = async (userId: string) => {
+ let socials = NO_SOCIAL_ACCOUNTS;
+ try {
+ let socialNeedsUpdate = INTEGRATED_SOCIAL_LIST;
+ for (let socialType of socialNeedsUpdate) {
+ const social = await loadSocialPosts(userId, socialType);
+ socials = {...socials, [socialType]: social};
+ }
+ return socials;
+ } catch (error) {
+ console.log(error);
+ }
+};
+
+/**
+ * Push the user out of system if token is not present in async storage
+ * @param dispatch
+ */
+export const getTokenOrLogout = async (dispatch: Function): Promise<string> => {
+ const token = await AsyncStorage.getItem('token');
+ if (!token) {
+ dispatch({type: userLoggedIn.type, payload: {userId: '', username: ''}});
+ return '';
+ }
+ return token;
+};