Skip to content
Permalink
Browse files

Merge pull request #81 from lucasgdev/web-folder

Adjust login expected data, and first hydratate behaviour to prevent chain calls
  • Loading branch information...
moul committed Jul 4, 2019
2 parents 3b5cdd4 + 0a576db commit 29b4a6b59f971e4af1596c2ea23206c51e2b4c00
@@ -1,4 +1,5 @@
import {
SET_ACTIVE_TEAM,
GET_USER_TEAMS_SUCCESS,
GET_USER_TEAMS_FAILED,
SET_TEAMS_LIST,
@@ -7,24 +8,26 @@ import {
JOIN_TEAM_FAILED
} from "../constants/actionTypes"
import { getAllTeams, getUserTeams, joinTeam as joinTeamCall } from "../api/teams"
import { fetchTeamTournaments as fetchTeamTournamentsAction } from "./tournaments";

export const setActiveTeam = (teamObjData) => async dispatch => {
dispatch({
type: SET_ACTIVE_TEAM,
payload: { team: teamObjData }
});
}

export const fetchUserTeams = (userID) => async dispatch => {

try {
const response = await getUserTeams(userID);
const teams = response.data.items;
const lastActiveTeam = teams.find((team) => team.last_active);

dispatch({
type: GET_USER_TEAMS_SUCCESS,
payload: {
userTeamsList: teams,
lastActiveTeam: lastActiveTeam
}
})

dispatch(fetchTeamTournamentsAction(lastActiveTeam.metadata.id))

} catch (error) {
dispatch({
@@ -16,16 +16,14 @@ import {
setTournamentActive as setTournamentActiveCall
} from "../api/tournaments"

export const setActiveTournament = (teamID, tournamentData) => async dispatch => {
export const setActiveTournament = (tournamentData) => async dispatch => {
try {
// const response = await setTournamentActiveCall(teamID, tournamentData.metadata.id)
// const response = await setTournamentActiveCall(tournamentData.metadata.id)
// if (response.status === 200) {
dispatch({
type: SET_ACTIVE_TOURNAMENT,
payload: { activeTournament: tournamentData }
});

dispatch(fetchLevels(tournamentData.metadata.id))
// }
}
catch(error) {
@@ -69,10 +67,10 @@ export const fetchTeamTournaments = (teamID) => async dispatch => {
});

if (lastActiveTournament === defaultTournament) {
dispatch(setActiveTournament(teamID, lastActiveTournament));
dispatch(setActiveTournament(lastActiveTournament));
} else if (!lastActiveTournament && defaultTournament) {
dispatch(setDefaultTournament(defaultTournament));
dispatch(setActiveTournament(teamID, defaultTournament));
dispatch(setActiveTournament(defaultTournament));
}

} catch (error) {
@@ -9,7 +9,8 @@ import {
} from "../constants/actionTypes"
import { USER_SESSION_TOKEN_NAME } from "../constants/userSession";
import { performLogin, pingUser } from "../api/userSession"
import { fetchUserTeams as fetchUserTeamsAction } from "./teams";
import { setActiveTeam as setActiveTeamAction } from "./teams";
import { setActiveTournament as setActiveTournamentAction } from "./tournaments"


export const performLoginAction = (email, password) => async dispatch => {
@@ -19,16 +20,18 @@ export const performLoginAction = (email, password) => async dispatch => {

try {
const response = await performLogin(email, password);
const userID = response.data.metadata.id;
const { userSession, token, lastActiveTeam, defaultTournament } = response.data;

dispatch({
type: SET_USER_SESSION,
payload: { activeUser: response.data }
payload: { activeUser: userSession }
});

Cookies.set(USER_SESSION_TOKEN_NAME, response.data.token)
Cookies.set(USER_SESSION_TOKEN_NAME, token)

dispatch(setActiveTeamAction(lastActiveTeam))
dispatch(setActiveTournamentAction(defaultTournament));

dispatch(fetchUserTeamsAction(userID))

} catch (error) {
dispatch({ type: LOGIN_FAILED, payload: { error } });
@@ -39,18 +42,20 @@ export const pingUserAction = () => async dispatch => {

try {
const response = await pingUser();
const userID = response.data.user.metadata.id;
const { isAuthenticated, token, userSession, lastActiveTeam, defaultTournament } = response.data;
dispatch({
type: PING_USER_SUCCESS,
payload: {
isAuthenticated: response.data.isAuthenticated,
activeUser: response.data.user
isAuthenticated: isAuthenticated,
activeUser: userSession
}
});

Cookies.set(USER_SESSION_TOKEN_NAME, response.data.token)
Cookies.set(USER_SESSION_TOKEN_NAME, token)

dispatch(setActiveTeamAction(lastActiveTeam))
dispatch(setActiveTournamentAction(defaultTournament));

dispatch(fetchUserTeamsAction(userID))
} catch (error) {
dispatch({ type: PING_USER_FAILED, payload: { error } });
}
@@ -2,8 +2,8 @@
// import { baseApi } from "./index";
import axios from "axios";

export function setTournamentActive(activeTeamID, tournamentID) {
// return baseApi.post(`/user-tournaments`, { activeTeamID, tournamentID });
export function setTournamentActive(tournamentID) {
// return baseApi.post(`/user-tournaments`, { tournamentID });
}
export function getAllTournaments() {
return axios.get("https://gist.githubusercontent.com/moul/826ef89d52651570a396ef3210a72e40/raw/e95d0e0391abca995949ab1258d5569e0b5ec356/GET%2520tournaments.json")
@@ -6,7 +6,45 @@ import { USER_SESSION_TOKEN_NAME } from "../constants/userSession";
import { history } from "../store/configureStore";

export function performLogin(email, password) {
return axios.get("https://gist.githubusercontent.com/moul/826ef89d52651570a396ef3210a72e40/raw/e95d0e0391abca995949ab1258d5569e0b5ec356/GET%2520user-session.json")
const apiForExample = axios.create();

apiForExample.interceptors.response.use((response) => {
// eslint-disable-next-line no-console
let interceptedResponse = {
...response,
data: {
userSession: response.data,
token: "sessionToken#@#@",
defaultTournament: {
"metadata": {
"id": "6ugtOJHGRrumcmufKZmTTQ==",
"created_at": "2019-04-25T12:32:36Z",
"updated_at": "2019-04-25T12:32:36Z"
},
"name": "tumblr",
"status": "Started",
"visibility": "Public",
"is_default": true
},
lastActiveTeam:{
"metadata": {
"id": "2dNuq9SuRvKcVHDOfNJSFQ==",
"created_at": "2019-04-25T11:41:24Z",
"updated_at": "2019-04-25T11:41:24Z"
},
"name": "chartreuse",
"gravatar_url": "http://www.internationalmonetize.io/harness/communities",
"locale": "fr_FR",
"last_active": true
}
}
}

return interceptedResponse;
// eslint-disable-next-line no-console
}, (error) => { console.log(error) });

return apiForExample.get("https://gist.githubusercontent.com/moul/826ef89d52651570a396ef3210a72e40/raw/e95d0e0391abca995949ab1258d5569e0b5ec356/GET%2520user-session.json")
// Uncomment line to use base api with auth token
// return baseApi.post(`/user-session`, { email, password });
}
@@ -25,8 +63,30 @@ export function pingUser() {
...response,
data: {
isAuthenticated: true,
user: response.data,
token: "token"
userSession: response.data,
token: "sessionToken#@#@",
defaultTournament: {
"metadata": {
"id": "6ugtOJHGRrumcmufKZmTTQ==",
"created_at": "2019-04-25T12:32:36Z",
"updated_at": "2019-04-25T12:32:36Z"
},
"name": "tumblr",
"status": "Started",
"visibility": "Public",
"is_default": true
},
lastActiveTeam:{
"metadata": {
"id": "2dNuq9SuRvKcVHDOfNJSFQ==",
"created_at": "2019-04-25T11:41:24Z",
"updated_at": "2019-04-25T11:41:24Z"
},
"name": "chartreuse",
"gravatar_url": "http://www.internationalmonetize.io/harness/communities",
"locale": "fr_FR",
"last_active": true
}
}
}

@@ -11,8 +11,7 @@ import SiteWrapper from "../SiteWrapper";
import AllTournamentsList from "../tournament/AllTournamentsList";
import AllTeamTournamentsList from "../tournament/AllTeamTournamentsList";
import {
fetchAllTournaments as fetchAllTournamentsAction,
setActiveTournament as setActiveTournamentAction
fetchAllTournaments as fetchAllTournamentsAction
} from "../../actions/tournaments"

class AllTournamentsPage extends React.PureComponent {
@@ -24,26 +23,20 @@ class AllTournamentsPage extends React.PureComponent {

render() {
const {
tournaments: { allTournaments, allTeamTournaments, activeTournament },
tournaments: { allTournaments },
activeTeam,
setActiveTournamentAction
} = this.props;
return (
<SiteWrapper>
<Page.Content title="All Tournaments">
<Grid.Row cards={true}>
<Grid.Col xs={12} sm={12} lg={6}>
{ allTournaments && <AllTournamentsList tournaments={allTournaments} /> }
{activeTeam &&
<AllTeamTournamentsList />
}
</Grid.Col>
<Grid.Col xs={12} sm={12} lg={6}>
{ allTeamTournaments &&
<AllTeamTournamentsList
teamTournaments={allTeamTournaments}
activeTournament={activeTournament}
setActive={setActiveTournamentAction}
activeTeam={activeTeam}
/>
}
{ allTournaments && <AllTournamentsList tournaments={allTournaments} /> }
</Grid.Col>
</Grid.Row>
</Page.Content>
@@ -55,8 +48,7 @@ class AllTournamentsPage extends React.PureComponent {
AllTournamentsPage.propTypes = {
tournaments: PropTypes.object,
activeTeam: PropTypes.object,
fetchAllTournamentsAction: PropTypes.func,
setActiveTournamentAction: PropTypes.func
fetchAllTournamentsAction: PropTypes.func
};

const mapStateToProps = state => ({
@@ -65,8 +57,7 @@ const mapStateToProps = state => ({
});

const mapDispatchToProps = {
fetchAllTournamentsAction: () => fetchAllTournamentsAction(),
setActiveTournamentAction: (teamID, tournament) => setActiveTournamentAction(teamID, tournament)
fetchAllTournamentsAction: () => fetchAllTournamentsAction()
};

export default connect(
@@ -6,8 +6,14 @@ import { Page, Grid } from "tabler-react";
import SiteWrapper from "../SiteWrapper";
import LevelsCardPreview from "../levels/LevelCardPreview";
import ValidationCouponStamp from "../coupon/ValidateCouponStampCard";
import { fetchLevels as fetchLevelsAction } from "../../actions/tournaments";

class TournamentPage extends React.Component {

componentDidMount() {
const { fetchLevelsAction, tournaments: { activeTournament } } = this.props;
fetchLevelsAction(activeTournament.metadata.id);
}

render() {
const { tournaments: { activeTournament, activeLevels } } = this.props;
@@ -36,14 +42,17 @@ class TournamentPage extends React.Component {

TournamentPage.propTypes = {
tournaments: PropTypes.object,
activeTeam: PropTypes.object
activeTeam: PropTypes.object,
fetchLevelsAction: PropTypes.func
};

const mapStateToProps = state => ({
tournaments: state.tournaments
});

const mapDispatchToProps = {};
const mapDispatchToProps = {
fetchLevelsAction: (tournamentID) => fetchLevelsAction(tournamentID),
};

export default connect(
mapStateToProps,

0 comments on commit 29b4a6b

Please sign in to comment.
You can’t perform that action at this time.