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$AddTeamData,
Actopm$SetCurrentTeam
} from "flow-typed";
import type { Dispatch } from "redux-thunk";
2017-11-06 22:37:09 +08:00
import {
getTeams,
changeCurrentTeam,
getCurrentTeam as fetchCurrentTeam
2017-11-06 22:37:09 +08:00
} 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";
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 {
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 => {
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)));
};
}
export function changeTeam(teamID: number): Dispatch {
2017-08-03 22:03:15 +08:00
return dispatch => {
changeCurrentTeam(teamID)
2017-11-06 22:37:09 +08:00
.then(response => dispatch(addTeamsData(response)))
2017-08-03 22:03:15 +08:00
.catch(error => {
console.log("get Teams Error: ", error);
});
};
}