import {
    RECEIVE_PLAN_PROPOSAL,
    SYNC_PLAN,
    SYNC_PLAN_SUCCESS,
    TOGGLE_PLANNING,
    TOGGLE_SELECTION,
    FETCH_PLAN_PROPOSAL
} from '../actions';
import {authenticatedFetch} from '../helpers';

export function togglePlanning(eventId) {
    return function (dispatch) {
        dispatch({
            type: TOGGLE_PLANNING,
            eventId: eventId
        });
    };
}

export function fetchPlanProposal(eventIds) {
    return function (dispatch, getState) {
        dispatch({
            type: FETCH_PLAN_PROPOSAL,
        });

        const params = eventIds.map(e => 'eventId=' + e).join('&');

        return authenticatedFetch('/api/attendances?' + params, dispatch, getState())
            .then(response => {
                if (response.status === 200) {
                    return response.json();
                }
                return null;
            })
            .then(json => {
                if (json !== null) {
                    dispatch({
                        type: RECEIVE_PLAN_PROPOSAL,
                        payload: json
                    });
                }
            });
    };
}

export function toggleSelection(eventId, personId) {
    return function (dispatch) {
        dispatch({
            type: TOGGLE_SELECTION,
            eventId: eventId,
            personId: personId
        });
    };
}

export function savePlan() {
    return function (dispatch, getState) {
        console.log('save plan');
        const payload = getState()['app'].planning.proposal.events.map(e => ({
            eventId: e.eventId,
            personIds: e.selectedPeople
        }));

        dispatch({
            type: SYNC_PLAN,
        });

        authenticatedFetch('/api/attendances', dispatch, getState(), {
            method: 'PUT',
            body: JSON.stringify(payload),
            headers: {
                'Content-Type': 'application/json',
            },
        }).then((response) => {
            if (response.ok) {
                dispatch({
                    type: SYNC_PLAN_SUCCESS
                });
            } else {
                response.json().then((msg) => console.log(msg));
            }
        }).catch((ex) => console.log(ex));
    };
}

@@ -1,16 +1,82 @@
 import %7B
+push%7D from 'react-router-redux';%0Aimport %7B%0A    FETCH_PLAN_PROPOSAL,
 %0A    REC
@@ -178,33 +178,8 @@
 TION
-,%0A    FETCH_PLAN_PROPOSAL
 %0A%7D f
@@ -649,24 +649,103 @@
 join('&');%0A%0A
+        if (params === '') %7B%0A            dispatch(push('/future'));%0A        %7D%0A%0A
         retu

Fix #33