Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] keep track of previously logged out users #2906

Merged
merged 7 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const AccountsBottomSheetContainer = () => {
const currentAccount = useAppSelector((state) => state.account.currentAccount);
const accounts = useAppSelector((state) => state.account.otherAccounts);
const pinHash = useAppSelector((state) => state.application.pin);
const prevLoggedInUsers = useAppSelector((state) => state.application.prevLoggedInUsers);

useEffect(() => {
if (isVisibleAccountsBottomSheet) {
Expand Down Expand Up @@ -152,6 +153,7 @@ const AccountsBottomSheetContainer = () => {
navigateToRoute={_navigateToRoute}
switchAccount={_switchAccount}
onClose={_onClose}
prevLoggedInUsers={prevLoggedInUsers}
/>
);
};
Expand Down
55 changes: 40 additions & 15 deletions src/components/accountsBottomSheet/view/accountsBottomSheetView.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import { default as ROUTES } from '../../../constants/routeNames';
import styles from './accountsBottomSheetStyles';

const AccountsBottomSheet = forwardRef(
({ accounts, currentAccount, navigateToRoute, switchAccount, onClose }, ref) => {
(
{ accounts, currentAccount, navigateToRoute, switchAccount, onClose, prevLoggedInUsers },
ref,
) => {
const bottomSheetModalRef = useRef();
const userList = useRef();
const insets = useSafeAreaInsets();
Expand All @@ -29,20 +32,42 @@ const AccountsBottomSheet = forwardRef(
},
}));

// _handlePressAccountTile(item)
const _renderAccountTile = ({ item }) => (
<TouchableOpacity style={styles.accountTile} onPress={() => switchAccount(item)}>
<View style={styles.avatarAndNameContainer}>
<UserAvatar username={item.username} />
<View style={styles.nameContainer}>
<Text style={styles.name}>{`@${item.username}`}</Text>
const _handlePressAccountTile = (item) => {
if (
item &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this huge logic can be simplified using ? and ! operators.

typeof item === 'object' &&
Object.prototype.hasOwnProperty.call(item, 'isLoggedOut') &&
item.isLoggedOut
) {
navigateToRoute(ROUTES.SCREENS.LOGIN, { username: item?.username || '' });
} else {
switchAccount(item);
}
};

const _renderAccountTile = ({ item }) => {
if (
item &&
typeof item === 'object' &&
Object.prototype.hasOwnProperty.call(item, 'isLoggedOut') &&
!item.isLoggedOut
)
return;

return (
<TouchableOpacity style={styles.accountTile} onPress={() => _handlePressAccountTile(item)}>
<View style={styles.avatarAndNameContainer}>
<UserAvatar username={item.username} />
<View style={styles.nameContainer}>
<Text style={styles.name}>{`@${item.username}`}</Text>
</View>
</View>
</View>
{currentAccount.name === item.username && (
<Icon iconType="AntDesign" name="checkcircle" style={styles.checkIcon} size={24} />
)}
</TouchableOpacity>
);
{currentAccount.name === item.username && (
noumantahir marked this conversation as resolved.
Show resolved Hide resolved
<Icon iconType="AntDesign" name="checkcircle" style={styles.checkIcon} size={24} />
)}
</TouchableOpacity>
);
};

return (
<View style={[styles.accountsModal]}>
Expand All @@ -55,7 +80,7 @@ const AccountsBottomSheet = forwardRef(
onClose={onClose}
>
<FlatList
data={accounts}
data={[...accounts, ...prevLoggedInUsers]}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to keep data flow more organised, why not added a section below list instead of mixing different objects? can even add a section labeled Logged out accounts

ref={userList}
scrollEnabled
keyExtractor={(item, index) => `${item.name || item.username}${index}`}
Expand Down
12 changes: 12 additions & 0 deletions src/redux/actions/applicationActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import getSymbolFromCurrency from 'currency-symbol-map';
import { PrevLoggedInUsers } from 'redux/reducers/applicationReducer';
import { getFiatHbdRate } from '../../providers/ecency/ecency';
import {
CHANGE_COMMENT_NOTIFICATION,
Expand Down Expand Up @@ -33,6 +34,8 @@ import {
SET_POST_UPVOTE_PERCENT,
SET_COMMENT_UPVOTE_PERCENT,
SET_WAVE_UPVOTE_PERCENT,
SET_PREV_LOGGED_IN_USERS,
CLEAR_PREV_LOGGED_IN_USERS,
} from '../constants/constants';

export const login = (payload) => ({
Expand Down Expand Up @@ -223,3 +226,12 @@ export const setEncryptedUnlockPin = (encryptedUnlockPin: string) => ({
payload: encryptedUnlockPin,
type: SET_ENC_UNLOCK_PIN,
});

export const setPrevLoggedInUsers = (data: PrevLoggedInUsers[]) => ({
payload: data,
type: SET_PREV_LOGGED_IN_USERS,
});

export const clearPrevLoggedInUsers = () => ({
type: CLEAR_PREV_LOGGED_IN_USERS,
});
2 changes: 2 additions & 0 deletions src/redux/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const SET_SETTINGS_MIGRATED = 'SET_SETTINGS_MIGRATED';
export const SET_TERMS_ACCEPTED = 'SET_TERMS_ACCEPTED';
export const SET_IS_BIOMETRIC_ENABLED = 'SET_IS_BIOMETRIC_ENABLED';
export const SET_ENC_UNLOCK_PIN = 'SET_ENC_UNLOCK_PIN';
export const SET_PREV_LOGGED_IN_USERS = 'SET_PREV_LOGGED_IN_USERS';
export const CLEAR_PREV_LOGGED_IN_USERS = 'CLEAR_PREV_LOGGED_IN_USERS';

// Accounts
export const ADD_OTHER_ACCOUNT = 'ADD_OTHER_ACCOUNT';
Expand Down
20 changes: 19 additions & 1 deletion src/redux/reducers/applicationReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ import {
SET_IS_BIOMETRIC_ENABLED,
SET_ENC_UNLOCK_PIN,
SET_WAVE_UPVOTE_PERCENT,
SET_PREV_LOGGED_IN_USERS,
CLEAR_PREV_LOGGED_IN_USERS,
} from '../constants/constants';

export interface PrevLoggedInUsers {
username: string;
isLoggedOut: boolean;
}

interface State {
api: string;
currency: {
Expand Down Expand Up @@ -72,6 +79,7 @@ interface State {
hidePostsThumbnails: boolean;
isTermsAccepted: boolean;
isBiometricEnabled: boolean;
prevLoggedInUsers: PrevLoggedInUsers[];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this the right place for preLoggedInUsers, for one application store has already overflown, and second since this is more related to account, perhaps it should be in accountReducer

Screenshot 2024-08-13 at 15 39 30

}

const initialState: State = {
Expand Down Expand Up @@ -113,6 +121,7 @@ const initialState: State = {
hidePostsThumbnails: false,
isTermsAccepted: false,
isBiometricEnabled: false,
prevLoggedInUsers: [],
};

const applicationReducer = (state = initialState, action): State => {
Expand Down Expand Up @@ -298,7 +307,16 @@ const applicationReducer = (state = initialState, action): State => {
...state,
encUnlockPin: action.payload,
};

case SET_PREV_LOGGED_IN_USERS:
return {
...state,
prevLoggedInUsers: action.payload,
};
case CLEAR_PREV_LOGGED_IN_USERS:
return {
...state,
prevLoggedInUsers: [],
};
default:
return state;
}
Expand Down
24 changes: 23 additions & 1 deletion src/screens/application/container/applicationContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
isRenderRequired,
isPinCodeOpen,
setEncryptedUnlockPin,
setPrevLoggedInUsers,
} from '../../../redux/actions/applicationActions';
import {
setAvatarCacheStamp,
Expand Down Expand Up @@ -586,6 +587,26 @@ class ApplicationContainer extends Component {
return repairUserAccountData(username, dispatch, intl, _accounts, pinCode);
};

// update previously loggedin users list,
_updatePrevLoggedInUsersList = (username: string) => {
const { dispatch, prevLoggedInUsers } = this.props as any;
if (prevLoggedInUsers && prevLoggedInUsers.length > 0) {
const userIndex = prevLoggedInUsers.findIndex((el: any) => el?.username === username);

if (userIndex > -1) {
const updatedPrevLoggedInUsers = prevLoggedInUsers;
updatedPrevLoggedInUsers[userIndex] = { username, isLoggedOut: true };
dispatch(setPrevLoggedInUsers(updatedPrevLoggedInUsers));
} else {
const u = { username, isLoggedOut: true };
dispatch(setPrevLoggedInUsers([...prevLoggedInUsers, u]));
}
} else {
const u = { username, isLoggedOut: true };
dispatch(setPrevLoggedInUsers([u]));
}
};

_logout = async (username) => {
const { otherAccounts, dispatch, intl } = this.props;

Expand All @@ -595,7 +616,7 @@ class ApplicationContainer extends Component {
if (response instanceof Error) {
throw response;
}

this._updatePrevLoggedInUsersList(username);
this._enableNotification(username, false);

// switch account if other account exist
Expand Down Expand Up @@ -815,6 +836,7 @@ export default connect(
settingsMigratedV2: state.application.settingsMigratedV2,
isNotificationsEnabled: state.application.isNotificationOpen,
notificationDetails: state.application.notificationDetails,
prevLoggedInUsers: state.application.prevLoggedInUsers,

// Account
unreadActivityCount: state.account.currentAccount.unread_activity_count,
Expand Down
34 changes: 29 additions & 5 deletions src/screens/login/container/loginContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import {
addOtherAccount,
updateCurrentAccount,
} from '../../../redux/actions/accountAction';
import { login as loginAction, setPinCode } from '../../../redux/actions/applicationActions';
import {
login as loginAction,
setPinCode,
setPrevLoggedInUsers,
} from '../../../redux/actions/applicationActions';
import { setInitPosts, setFeedPosts } from '../../../redux/actions/postsAction';
import { setPushTokenSaved, setExistUser } from '../../../realm/realm';
import { setPushToken } from '../../../providers/ecency/ecency';
Expand Down Expand Up @@ -154,8 +158,27 @@ class LoginContainer extends PureComponent {
});
};

// update previously loggedin users list,
_updatePrevLoggedInUsersList = (username: string) => {
const { dispatch, prevLoggedInUsers } = this.props as any;
if (prevLoggedInUsers && prevLoggedInUsers.length > 0) {
const userIndex = prevLoggedInUsers.findIndex((el: any) => el?.username === username);
if (userIndex > -1) {
const updatedPrevLoggedInUsers = prevLoggedInUsers;
updatedPrevLoggedInUsers[userIndex] = { username, isLoggedOut: false };
dispatch(setPrevLoggedInUsers(updatedPrevLoggedInUsers));
} else {
const u = { username, isLoggedOut: false };
dispatch(setPrevLoggedInUsers([...prevLoggedInUsers, u]));
}
} else {
const u = { username, isLoggedOut: false };
dispatch(setPrevLoggedInUsers([u]));
}
};

_handleOnPressLogin = (username, password) => {
const { dispatch, intl, isPinCodeOpen, navigation, userActivityMutation } = this.props;
const { dispatch, intl, isPinCodeOpen, navigation, userActivityMutation } = this.props as any;

this.setState({ isLoading: true });

Expand All @@ -170,7 +193,7 @@ class LoginContainer extends PureComponent {
dispatch(loginAction(true));
dispatch(setInitPosts([]));
dispatch(setFeedPosts([]));

this._updatePrevLoggedInUsersList(username);
// track user activity for login
userActivityMutation.mutate({ pointsTy: PointActivityIds.LOGIN });
setExistUser(true);
Expand Down Expand Up @@ -274,8 +297,8 @@ class LoginContainer extends PureComponent {
};

render() {
const { navigation, route } = this.props;
const { isLoading } = this.state;
const { navigation, route } = this.props as any;
const { isLoading } = this.state as any;

const initialUsername = route.params?.username ?? '';

Expand All @@ -298,6 +321,7 @@ const mapStateToProps = (state) => ({
notificationSettings: state.application.isNotificationOpen,
isConnected: state.application.isConnected,
isPinCodeOpen: state.application.isPinCodeOpen,
prevLoggedInUsers: state.application.prevLoggedInUsers,
});

const mapHooksToProps = () => ({
Expand Down