import dispatcher from "../dispatcher";
import api from "axios";

var jsonReq = {
    action: "?",
    filters: []
};

export function logout() {
    dispatcher.dispatch({ type: 'LOGOUT', data: { auth: 0, msg: 'logged out', token: '' } });
}

export function apiCall(route, action, filters) {
    jsonReq.action = action;
    jsonReq.filters = filters;
    api.post('/api/' + route, jsonReq)
        .then(function (resp) {
            dispatcher.dispatch({
                type: action,
                data: resp.data
            });
        })
        .catch(function (error) {
            dispatcher.dispatch({
                type: "API_ERR",
                data: error
            });
        });
}

export function localCall(action, data) {
    dispatcher.dispatch({ type: action, data: data });
}