scinote-web/app/javascript/src/components/actions/TeamsActions.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

// @flow
import type {
Teams$Team,
Action$LeaveTeam,
Action$AddTeamData,
Actopm$SetCurrentTeam
} from "flow-typed";
import type { Dispatch } from "redux-thunk";
import { getTeams, changeCurrentTeam } from "../../services/api/teams_api";
import {
GET_LIST_OF_TEAMS,
SET_CURRENT_TEAM
} from "../../config/action_types";
export function addTeamsData(data: Array<Teams$Team>): Action$AddTeamData {
2017-08-03 22:03:15 +08:00
return {
type: GET_LIST_OF_TEAMS,
payload: data
};
}
export function setCurrentTeam(team: Teams$Team): Actopm$SetCurrentTeam {
2017-08-03 22:03:15 +08:00
return {
team,
2017-08-07 19:51:22 +08:00
type: SET_CURRENT_TEAM
2017-08-03 22:03:15 +08:00
};
}
export function getTeamsList(): Dispatch {
2017-08-03 22:03:15 +08:00
return dispatch => {
getTeams()
2017-08-03 22:03:15 +08:00
.then(response => {
const { teams, currentTeam } = response;
2017-08-03 22:03:15 +08:00
dispatch(addTeamsData(teams));
dispatch(setCurrentTeam(currentTeam));
2017-08-03 22:03:15 +08:00
})
.catch(error => {
console.log("get Teams Error: ", error);
});
};
}
export function changeTeam(teamID: number): Dispatch {
2017-08-03 22:03:15 +08:00
return dispatch => {
changeCurrentTeam(teamID)
2017-08-03 22:03:15 +08:00
.then(response => {
const { teams, currentTeam } = response;
2017-08-03 22:03:15 +08:00
dispatch(addTeamsData(teams));
dispatch(setCurrentTeam(currentTeam));
2017-08-03 22:03:15 +08:00
})
.catch(error => {
console.log("get Teams Error: ", error);
});
};
}