Auto select last team viewed (#161)
* WIP Autoselect last team viewed * Auto select last team viewed * Fix auto login in root. * Fix per PR comments Remove unused actions. Use currentTeam selector.
This commit is contained in:
parent
d0481605fb
commit
677f7a8cb0
6 changed files with 35 additions and 14 deletions
|
|
@ -2,14 +2,21 @@
|
|||
// See License.txt for license information.
|
||||
|
||||
import {AsyncStorage} from 'react-native';
|
||||
import {GeneralTypes, UsersTypes} from 'service/constants';
|
||||
import {batchActions} from 'redux-batched-actions';
|
||||
import {GeneralTypes, TeamsTypes, UsersTypes} from 'service/constants';
|
||||
import Client from 'service/client';
|
||||
|
||||
export function loadStorage() {
|
||||
return async (dispatch, getState) => {
|
||||
try {
|
||||
const data = JSON.parse(await AsyncStorage.getItem('storage'));
|
||||
dispatch({type: GeneralTypes.RECEIVED_APP_CREDENTIALS, data}, getState);
|
||||
const {token, url, currentTeamId} = data;
|
||||
const credentials = {token, url};
|
||||
|
||||
dispatch(batchActions([
|
||||
{type: GeneralTypes.RECEIVED_APP_CREDENTIALS, data: credentials},
|
||||
{type: TeamsTypes.SELECT_TEAM, data: currentTeamId}
|
||||
]), getState);
|
||||
} catch (error) {
|
||||
// Error loading data
|
||||
dispatch({type: GeneralTypes.REMOVED_APP_CREDENTIALS, error}, getState);
|
||||
|
|
@ -17,15 +24,17 @@ export function loadStorage() {
|
|||
};
|
||||
}
|
||||
|
||||
export function saveStorage() {
|
||||
export function saveStorage(data = {}) {
|
||||
return async (dispatch, getState) => {
|
||||
try {
|
||||
const data = {
|
||||
const clientData = {
|
||||
token: Client.getToken(),
|
||||
url: Client.getUrl()
|
||||
};
|
||||
|
||||
await AsyncStorage.setItem('storage', JSON.stringify(data));
|
||||
const mergedStorageData = Object.assign({}, data, clientData);
|
||||
|
||||
await AsyncStorage.setItem('storage', JSON.stringify(mergedStorageData));
|
||||
dispatch({type: GeneralTypes.RECEIVED_APP_CREDENTIALS, data}, getState);
|
||||
} catch (error) {
|
||||
// Error saving data
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ export default class Root extends React.Component {
|
|||
logoutRequest: React.PropTypes.object,
|
||||
loginRequest: React.PropTypes.object,
|
||||
actions: React.PropTypes.shape({
|
||||
goToChannelView: React.PropTypes.func,
|
||||
goToSelectServer: React.PropTypes.func,
|
||||
goToSelectTeam: React.PropTypes.func,
|
||||
loadStorage: React.PropTypes.func,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import {connect} from 'react-redux';
|
|||
|
||||
import {loadStorage, removeStorage} from 'app/actions/storage';
|
||||
import {goToSelectServer, setStoreFromLocalData} from 'app/actions/views/root';
|
||||
import {goToSelectTeam} from 'app/actions/navigation';
|
||||
import {goToSelectTeam, goToChannelView} from 'app/actions/navigation';
|
||||
import {resetLogout} from 'service/actions/users';
|
||||
|
||||
import Root from './root';
|
||||
|
|
@ -23,6 +23,7 @@ function mapStateToProps(state, ownProps) {
|
|||
function mapDispatchToProps(dispatch) {
|
||||
return {
|
||||
actions: bindActionCreators({
|
||||
goToChannelView,
|
||||
goToSelectServer,
|
||||
goToSelectTeam,
|
||||
loadStorage,
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ export default class SelectTeam extends Component {
|
|||
teams: PropTypes.object.isRequired,
|
||||
myMembers: PropTypes.object.isRequired,
|
||||
teamsRequest: PropTypes.object.isRequired,
|
||||
currentTeam: PropTypes.object,
|
||||
actions: PropTypes.object.isRequired
|
||||
};
|
||||
|
||||
|
|
@ -29,7 +30,13 @@ export default class SelectTeam extends Component {
|
|||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.selectFirstTeam(this.props.teams, this.props.myMembers);
|
||||
const {currentTeam, myMembers, teams} = this.props;
|
||||
|
||||
if (currentTeam) {
|
||||
this.onSelectTeam(currentTeam);
|
||||
} else if (!currentTeam) {
|
||||
this.selectFirstTeam(teams, myMembers);
|
||||
}
|
||||
}
|
||||
|
||||
componentWillReceiveProps(nextProps) {
|
||||
|
|
@ -49,7 +56,8 @@ export default class SelectTeam extends Component {
|
|||
|
||||
onSelectTeam(team) {
|
||||
this.props.actions.selectTeam(team).then(() => {
|
||||
this.props.actions.goToChannelView();
|
||||
this.props.actions.saveStorage({currentTeamId: team.id}).
|
||||
then(this.props.actions.goToChannelView());
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@ import {connect} from 'react-redux';
|
|||
|
||||
import {selectTeam} from 'service/actions/teams';
|
||||
import {init as websocket} from 'service/actions/websocket';
|
||||
import {saveStorage} from 'app/actions/storage';
|
||||
import {goToChannelView} from 'app/actions/navigation';
|
||||
import {getCurrentTeam} from 'service/selectors/entities/teams';
|
||||
|
||||
import SelectTeamView from './select_team.js';
|
||||
|
||||
|
|
@ -15,6 +17,7 @@ function mapStateToProps(state) {
|
|||
config: state.entities.general.config,
|
||||
teamsRequest: state.requests.teams.allTeams,
|
||||
teams: state.entities.teams.teams,
|
||||
currentTeam: getCurrentTeam(state),
|
||||
myMembers: state.entities.teams.myMembers
|
||||
};
|
||||
}
|
||||
|
|
@ -23,6 +26,7 @@ function mapDispatchToProps(dispatch) {
|
|||
return {
|
||||
actions: bindActionCreators({
|
||||
goToChannelView,
|
||||
saveStorage,
|
||||
selectTeam,
|
||||
websocket
|
||||
}, dispatch)
|
||||
|
|
|
|||
|
|
@ -32,12 +32,10 @@ async function getProfilesAndStatusesForMembers(userIds, dispatch, getState) {
|
|||
}
|
||||
|
||||
export function selectTeam(team) {
|
||||
return async (dispatch, getState) => {
|
||||
dispatch({
|
||||
type: TeamsTypes.SELECT_TEAM,
|
||||
data: team.id
|
||||
}, getState);
|
||||
};
|
||||
return async (dispatch, getState) => dispatch({
|
||||
type: TeamsTypes.SELECT_TEAM,
|
||||
data: team.id
|
||||
}, getState);
|
||||
}
|
||||
|
||||
export function fetchTeams() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue