|
@@ -3,10 +3,13 @@ import Reflux from 'reflux';
|
|
|
import TeamActions from 'app/actions/teamActions';
|
|
|
import {Team} from 'app/types';
|
|
|
|
|
|
+const MAX_TEAMS = 100;
|
|
|
+
|
|
|
type State = {
|
|
|
teams: Team[];
|
|
|
loading: boolean;
|
|
|
hasMore: boolean | null;
|
|
|
+ loadedUserTeams: boolean;
|
|
|
};
|
|
|
|
|
|
type TeamStoreInterface = {
|
|
@@ -27,6 +30,7 @@ const teamStoreConfig: Reflux.StoreDefinition & TeamStoreInterface = {
|
|
|
initialized: false,
|
|
|
state: {
|
|
|
teams: [],
|
|
|
+ loadedUserTeams: false,
|
|
|
loading: true,
|
|
|
hasMore: null,
|
|
|
},
|
|
@@ -37,24 +41,49 @@ const teamStoreConfig: Reflux.StoreDefinition & TeamStoreInterface = {
|
|
|
this.listenTo(TeamActions.createTeamSuccess, this.onCreateSuccess);
|
|
|
this.listenTo(TeamActions.fetchDetailsSuccess, this.onUpdateSuccess);
|
|
|
this.listenTo(TeamActions.loadTeams, this.loadInitialData);
|
|
|
+ this.listenTo(TeamActions.loadUserTeams, this.loadUserTeams);
|
|
|
this.listenTo(TeamActions.removeTeamSuccess, this.onRemoveSuccess);
|
|
|
this.listenTo(TeamActions.updateSuccess, this.onUpdateSuccess);
|
|
|
},
|
|
|
|
|
|
reset() {
|
|
|
- this.state = {teams: [], loading: true, hasMore: null};
|
|
|
+ this.state = {teams: [], loadedUserTeams: false, loading: true, hasMore: null};
|
|
|
},
|
|
|
|
|
|
loadInitialData(items, hasMore = null) {
|
|
|
this.initialized = true;
|
|
|
this.state = {
|
|
|
teams: items.sort((a, b) => a.slug.localeCompare(b.slug)),
|
|
|
+ // TODO(davidenwang): Replace with a more reliable way of knowing when we have loaded all teams
|
|
|
+ loadedUserTeams: items.length < MAX_TEAMS,
|
|
|
loading: false,
|
|
|
hasMore,
|
|
|
};
|
|
|
this.trigger(new Set(items.map(item => item.id)));
|
|
|
},
|
|
|
|
|
|
+ loadUserTeams(userTeams: Team[]) {
|
|
|
+ const teamIdMap = this.state.teams.reduce((acc: Record<string, Team>, team: Team) => {
|
|
|
+ acc[team.id] = team;
|
|
|
+ return acc;
|
|
|
+ }, {});
|
|
|
+
|
|
|
+ // Replace or insert new user teams
|
|
|
+ userTeams.reduce((acc: Record<string, Team>, userTeam: Team) => {
|
|
|
+ acc[userTeam.id] = userTeam;
|
|
|
+ return acc;
|
|
|
+ }, teamIdMap);
|
|
|
+
|
|
|
+ const teams = Object.values(teamIdMap).sort((a, b) => a.slug.localeCompare(b.slug));
|
|
|
+ this.state = {
|
|
|
+ ...this.state,
|
|
|
+ loadedUserTeams: true,
|
|
|
+ teams,
|
|
|
+ };
|
|
|
+
|
|
|
+ this.trigger(new Set(Object.keys(teamIdMap)));
|
|
|
+ },
|
|
|
+
|
|
|
onUpdateSuccess(itemId, response) {
|
|
|
if (!response) {
|
|
|
return;
|