From 88420ccca671bddc9d958d9b96dd8d1d32b529bb Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 23 Mar 2021 21:01:50 -0500 Subject: [PATCH] Fetch unfetched otherAccounts --- app/soapbox/actions/auth.js | 15 +++++++++++++++ app/soapbox/actions/me.js | 1 - .../features/ui/components/profile_dropdown.js | 15 ++++++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/soapbox/actions/auth.js b/app/soapbox/actions/auth.js index 26662803c..28bbfed2a 100644 --- a/app/soapbox/actions/auth.js +++ b/app/soapbox/actions/auth.js @@ -1,4 +1,6 @@ import api from '../api'; +import { importFetchedAccount } from './importer'; +import { throttle } from 'lodash'; import snackbar from 'soapbox/actions/snackbar'; export const SWITCH_ACCOUNT = 'SWITCH_ACCOUNT'; @@ -146,6 +148,7 @@ export function verifyCredentials(token) { }; return api(getState).request(request).then(({ data: account }) => { + dispatch(importFetchedAccount(account)); dispatch({ type: VERIFY_CREDENTIALS_SUCCESS, token, account }); return account; }).catch(error => { @@ -192,6 +195,18 @@ export function switchAccount(accountId) { return { type: SWITCH_ACCOUNT, accountId }; } +export function fetchOwnAccounts() { + return throttle((dispatch, getState) => { + const state = getState(); + state.getIn(['auth', 'users']).forEach((token, id) => { + const account = state.getIn(['accounts', id]); + if (!account) { + dispatch(verifyCredentials(token)); + } + }); + }, 2000); +} + export function register(params) { return (dispatch, getState) => { params.fullname = params.username; diff --git a/app/soapbox/actions/me.js b/app/soapbox/actions/me.js index c0571b27d..7ccf07757 100644 --- a/app/soapbox/actions/me.js +++ b/app/soapbox/actions/me.js @@ -1,6 +1,5 @@ import api from '../api'; import { importFetchedAccount } from './importer'; -import { List as ImmutableList } from 'immutable'; import { verifyCredentials } from './auth'; export const ME_FETCH_REQUEST = 'ME_FETCH_REQUEST'; diff --git a/app/soapbox/features/ui/components/profile_dropdown.js b/app/soapbox/features/ui/components/profile_dropdown.js index 57114f275..abb257852 100644 --- a/app/soapbox/features/ui/components/profile_dropdown.js +++ b/app/soapbox/features/ui/components/profile_dropdown.js @@ -1,13 +1,14 @@ import React from 'react'; import { connect } from 'react-redux'; // import { openModal } from '../../../actions/modal'; +import { fetchOwnAccounts } from 'soapbox/actions/auth'; import PropTypes from 'prop-types'; import ImmutablePropTypes from 'react-immutable-proptypes'; import DropdownMenuContainer from '../../../containers/dropdown_menu_container'; import { isStaff } from 'soapbox/utils/accounts'; import { defineMessages, injectIntl } from 'react-intl'; import { logOut, switchAccount } from 'soapbox/actions/auth'; -import { Map as ImmutableMap, List as ImmutableList } from 'immutable'; +import { List as ImmutableList } from 'immutable'; const messages = defineMessages({ switch: { id: 'profile_dropdown.switch_account', defaultMessage: 'Switch to @{acct}' }, @@ -23,8 +24,8 @@ const mapStateToProps = state => { .keySeq() .reduce((list, id) => { if (id === me) return list; - const account = state.getIn(['accounts', id]) || ImmutableMap({ id: id, acct: id }); - return list.push(account); + const account = state.getIn(['accounts', id]); + return account ? list.push(account) : list; }, ImmutableList()); return { @@ -61,6 +62,14 @@ class ProfileDropdown extends React.PureComponent { }; } + componentDidMount() { + this.props.dispatch(fetchOwnAccounts()); + } + + componentDidUpdate() { + this.props.dispatch(fetchOwnAccounts()); + } + render() { const { intl, account, otherAccounts } = this.props; const size = this.props.size || 16;