Skip to content

Instantly share code, notes, and snippets.

@ponty96
Created July 21, 2016 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ponty96/bb0b54143edc5bb1babcf1964384f5b7 to your computer and use it in GitHub Desktop.
Save ponty96/bb0b54143edc5bb1babcf1964384f5b7 to your computer and use it in GitHub Desktop.
import ActionTypes from '../action_types'
import { push } from 'react-router-redux'
import { httpPost, httpGet, httpDelete} from '../utils'
const BusinessActions = {
currentBusiness: () => {
// api create business
return dispatch => {
// dispatch loading sign
dispatch({type: ActionTypes.BUSINESS_PROGRESS})
httpGet('/api/v1/business')
.then((data) => {
// dispatch successful creation
dispatch({
type: ActionTypes.BUSINESS_SUCCESS,
payload:data.business
})
localStorage.setItem('BUSINESS', JSON.stringify(data.business))
})
.catch((error) => {
error.response.json()
.then((errorJSON) => {
console.log(errorJSON)
dispatch({
type: ActionTypes.BUSINESS_ERROR,
payload:errorJSON.errors
})
});
})
}
}
}
export default BusinessActions
defmodule AppName.Router do
use AppName.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
plug Guardian.Plug.VerifyHeader
plug Guardian.Plug.LoadResource
end
scope "/", AppName do
pipe_through :browser # Use the default browser stack
get "*path", PageController, :index
end
scope "/api", AppName do
pipe_through :api # use api stack
scope "/v1" do
post "/sessions", SessionController, :create
delete "/sessions", SessionController, :delete
get "/current_user", CurrentUserController, :show
get "/business", BusinessController, :show
resources "/members", MemberController, only: [:index]
resources "/farms", FarmController, only: [:index, :create]
end
end
# Other scopes may use custom stacks.
# scope "/api", AppName do
# pipe_through :api
# end
end
import React from 'react';
import fetch from 'isomorphic-fetch';
import { polyfill } from 'es6-promise';
const defaultHeaders = {
Accept: 'application/json',
'Content-Type': 'application/json',
};
function buildHeaders() {
const authToken = localStorage.getItem('JWT_TOKEN');
return { ...defaultHeaders, Authorization: authToken };
}
export function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
} else {
var error = new Error(response.statusText);
error.response = response;
throw error;
}
}
export function parseJSON(response) {
return response.json();
}
export function httpGet(url) {
return fetch(url, {
headers: buildHeaders(),
})
.then(checkStatus)
.then(parseJSON);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment