Skip to content

Commit

Permalink
added defaultTeamId to be saved in asyncStorage (#1839)
Browse files Browse the repository at this point in the history
* added defaultTeamId to be saved in asyncStorage, so next time when user logins it memorizes the team

* added functionality when there are multi tenants

* storing the userId and tenantId in asyncStorage too, in order to check default team

* removed unnecessary else block
  • Loading branch information
desperado1802 authored Nov 21, 2023
1 parent 48b7ed1 commit b754552
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 168 deletions.
14 changes: 13 additions & 1 deletion apps/mobile/app/screens/LoginScreen/Components/PassCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { EMAIL_REGEX } from '../../../helpers/regex';
import UserTenants from './UserTenants';
import { IWorkspace, VerificationResponse } from '../../../services/interfaces/IAuthentication';
import { CodeInputField } from '../../../components/CodeField';
import AsyncStorage from '@react-native-async-storage/async-storage';

interface Props {
isLoading: boolean;
Expand All @@ -29,6 +30,11 @@ interface Props {
signInWorkspace: () => unknown;
setIsWorkspaceScreen: React.Dispatch<React.SetStateAction<boolean>>;
}
export type defaultUserInfoType = {
defaultUserId: string;
defaultUserTenantId: string;
};

const { width } = Dimensions.get('window');
const PassCode: FC<Props> = observer(
({
Expand All @@ -55,8 +61,12 @@ const PassCode: FC<Props> = observer(
step2: false,
step3: false
});
const [selectedWorkspace, setSelectedWorkspace] = useState<number>(0);
const [selectedWorkspace, setSelectedWorkspace] = useState<number>(null);
const [workspaceData, setWorkspaceData] = useState(null);
const [defaultUserInfo, setDefaultUserInfo] = useState<defaultUserInfoType>({
defaultUserId: '',
defaultUserTenantId: ''
});

const onNextStep = async () => {
if (step === 'Email') {
Expand Down Expand Up @@ -100,6 +110,7 @@ const PassCode: FC<Props> = observer(
}, 1000);
}
if (step === 'Tenant') {
await AsyncStorage.setItem('defaultUserInfo', JSON.stringify(defaultUserInfo));
signInWorkspace();
}
};
Expand Down Expand Up @@ -242,6 +253,7 @@ const PassCode: FC<Props> = observer(
data={item}
activeTeamId={activeTeamId}
setActiveTeamId={setActiveTeamId}
setDefaultUserInfo={setDefaultUserInfo}
selectedWorkspace={selectedWorkspace}
setSelectedWorkspace={setSelectedWorkspace}
isValid={isValid}
Expand Down
67 changes: 61 additions & 6 deletions apps/mobile/app/screens/LoginScreen/Components/UserTenants.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* eslint-disable react-native/no-inline-styles */
/* eslint-disable react-native/no-color-literals */
/* eslint-disable react-native/no-unused-styles */
import React, { FC } from 'react';
import React, { FC, useEffect, useState } from 'react';
import { View, Text, StyleSheet, Image, FlatList } from 'react-native';
import { IWorkspace } from '../../../services/interfaces/IAuthentication';
import { SvgXml } from 'react-native-svg';
import { grayCircleIcon, greenCircleTickIcon } from '../../../components/svgs/icons';
import { useAppTheme } from '../../../theme';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { defaultUserInfoType } from './PassCode';

interface IValid {
step1: boolean;
Expand All @@ -23,6 +25,7 @@ interface IUserTenants {
isValid: IValid;
setIsValid: React.Dispatch<React.SetStateAction<IValid>>;
setTempAuthToken: (token: string) => void;
setDefaultUserInfo: React.Dispatch<React.SetStateAction<defaultUserInfoType>>;
}

const UserTenants: FC<IUserTenants> = ({
Expand All @@ -34,20 +37,67 @@ const UserTenants: FC<IUserTenants> = ({
selectedWorkspace,
isValid,
setIsValid,
setTempAuthToken
setTempAuthToken,
setDefaultUserInfo
}) => {
const { colors } = useAppTheme();

const [autoSetWorkspace, setAutoSetWorkspace] = useState<boolean>(true);

useEffect(() => {
const getDefaultTeamId = async () => {
try {
setAutoSetWorkspace(true);
const defaultTeamId = await AsyncStorage.getItem('defaultTeamId');
const defaultUserInfoString = await AsyncStorage.getItem('defaultUserInfo');
const defaultUserInfoObj: defaultUserInfoType = JSON.parse(defaultUserInfoString);

if (
defaultTeamId &&
data?.user?.id === defaultUserInfoObj?.defaultUserId &&
data?.user?.tenant?.id === defaultUserInfoObj?.defaultUserTenantId
) {
setActiveTeamId(defaultTeamId);
} else {
index === 0 && setActiveTeamId(data?.current_teams[0]?.team_id);
}

setIsValid({ ...isValid, step3: true });

setTempAuthToken(data?.token);
} catch (error) {
console.error(error);
}
};

getDefaultTeamId();
}, [data.current_teams]);

useEffect(() => {
if (autoSetWorkspace) {
const selectedIndex = data.current_teams.findIndex((team) => team.team_id === activeTeamId);
if (selectedIndex !== -1) {
setSelectedWorkspace(index);
}
}
}, [activeTeamId]);

return (
<View style={{ ...styles.tenantContainer, backgroundColor: colors.background, borderColor: colors.border }}>
<View style={styles.tenantNameContainer}>
<Text style={{ fontSize: 17, color: colors.primary }}>{data.user.tenant.name}</Text>
<View
onTouchStart={() => {
autoSetWorkspace && setAutoSetWorkspace(false);
setDefaultUserInfo({
defaultUserId: data?.user?.id,
defaultUserTenantId: data?.user?.tenant?.id
});
setSelectedWorkspace(index);
selectedWorkspace !== index && setActiveTeamId(data.current_teams[0].team_id);
data.current_teams.filter((team) => team.team_id === activeTeamId) &&
selectedWorkspace !== index && setActiveTeamId(data?.current_teams[0].team_id);
data?.current_teams.filter((team) => team.team_id === activeTeamId) &&
setIsValid({ ...isValid, step3: true });
setTempAuthToken(data.token);
setTempAuthToken(data?.token);
}}
>
{selectedWorkspace === index ? (
Expand All @@ -74,10 +124,15 @@ const UserTenants: FC<IUserTenants> = ({
</View>
<View
onTouchStart={() => {
autoSetWorkspace && setAutoSetWorkspace(false);
setDefaultUserInfo({
defaultUserId: data?.user?.id,
defaultUserTenantId: data?.user?.tenant?.id
});
setActiveTeamId(item.team_id);
setSelectedWorkspace(index);
setIsValid({ ...isValid, step3: true });
setTempAuthToken(data.token);
setTempAuthToken(data?.token);
}}
>
{activeTeamId === item.team_id ? (
Expand Down
Loading

0 comments on commit b754552

Please sign in to comment.