2017-10-23 21:02:04 +08:00
|
|
|
// @flow
|
|
|
|
import type {
|
|
|
|
Teams$Team,
|
|
|
|
Action$AddTeamData,
|
|
|
|
Actopm$SetCurrentTeam
|
|
|
|
} from "flow-typed";
|
|
|
|
import type { Dispatch } from "redux-thunk";
|
2017-11-06 22:14:17 +08:00
|
|
|
import { getTeams, changeCurrentTeam, fetchCurrentTeam } from "../../services/api/teams_api";
|
2017-11-02 23:41:29 +08:00
|
|
|
import { GET_LIST_OF_TEAMS, SET_CURRENT_TEAM } from "../../config/action_types";
|
2017-09-12 23:30:13 +08:00
|
|
|
|
2017-10-23 21:02:04 +08:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:14:17 +08:00
|
|
|
export function setCurrentTeam(team: Teams$CurrentTeam): Actopm$SetCurrentTeam {
|
2017-08-03 22:03:15 +08:00
|
|
|
return {
|
2017-08-28 23:05:09 +08:00
|
|
|
team,
|
2017-08-07 19:51:22 +08:00
|
|
|
type: SET_CURRENT_TEAM
|
2017-08-03 22:03:15 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-23 21:02:04 +08:00
|
|
|
export function getTeamsList(): Dispatch {
|
2017-08-03 22:03:15 +08:00
|
|
|
return dispatch => {
|
2017-10-23 21:02:04 +08:00
|
|
|
getTeams()
|
2017-08-03 22:03:15 +08:00
|
|
|
.then(response => {
|
2017-11-06 22:14:17 +08:00
|
|
|
dispatch(addTeamsData(response));
|
2017-08-03 22:03:15 +08:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.log("get Teams Error: ", error);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:14:17 +08:00
|
|
|
export function getCurrentTeam(): Dispatch {
|
|
|
|
return dispatch => {
|
|
|
|
fetchCurrentTeam().then(response => dispatch(setCurrentTeam(response)));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-23 21:02:04 +08:00
|
|
|
export function changeTeam(teamID: number): Dispatch {
|
2017-08-03 22:03:15 +08:00
|
|
|
return dispatch => {
|
2017-10-23 21:02:04 +08:00
|
|
|
changeCurrentTeam(teamID)
|
2017-08-03 22:03:15 +08:00
|
|
|
.then(response => {
|
2017-11-06 22:14:17 +08:00
|
|
|
const { teams } = response;
|
2017-08-03 22:03:15 +08:00
|
|
|
dispatch(addTeamsData(teams));
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.log("get Teams Error: ", error);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|