Skip to content

Instantly share code, notes, and snippets.

@bentooth
Created June 12, 2019 18:19
Show Gist options
  • Save bentooth/7f4abc54842b18ca10b152f9856a4ed9 to your computer and use it in GitHub Desktop.
Save bentooth/7f4abc54842b18ca10b152f9856a4ed9 to your computer and use it in GitHub Desktop.
redux-pender
import axios from "axios";
import { pender } from "redux-pender";
import { createAction, handleActions } from "redux-actions";
function getDate() {
let utcDate = new Date(Date.now());
return utcDate.toUTCString();
}
const _getUser = (token) => {
return axios({
url: `${process.env.REACT_APP_API_URL}/user/`,
method: "GET",
headers: { Authorization: token }
});
}
const _signup = credentials => {
return axios.post("/auth/signup", credentials);
};
const _signin = credentials => {
return axios.post("/auth/signin", credentials);
};
const _addToCart = (albumId, token) => {
return axios({
url: `${process.env.REACT_APP_API_URL}/user/cart/add/${albumId}`,
method: "POST",
headers: { Authorization: token }
});
};
const _getCoins = (token) => {
return axios({
url: `${process.env.REACT_APP_API_URL}/user/coins/`,
method: "GET",
headers: { Authorization: token }
});
}
const _exchangeCoins = (token, type, amount) => {
return axios({
url: `${process.env.REACT_APP_API_URL}/user/coins/exchange`,
method: "POST",
data: {
type: type,
amount: amount
},
headers: { Authorization: token }
});
}
export const GET_COINS = "user/GET_COINS";
export const getCoins = createAction(GET_COINS, _getCoins);
export const EXCHANGE_COINS = "user/EXCHANGE_COINS";
export const exchangeCoins = createAction(EXCHANGE_COINS, _exchangeCoins);
export const GET_USER = "user/GET_USER";
export const getUser = createAction(GET_USER, _getUser);
export const SIGN_UP = "user/SIGN_UP";
export const signUp = createAction(SIGN_UP, _signup);
export const SIGN_IN = "user/SIGN_IN";
export const signIn = createAction(SIGN_IN, _signin);
export const ADD_TO_CART = "user/ADD_TO_CART";
export const addToCart = createAction(ADD_TO_CART, _addToCart);
export const SIGN_OUT = "user/SIGN_OUT";
export const signOut = createAction(SIGN_OUT);
export const SHOW_MODAL = "user/SHOW_MODAL";
export const showModal = createAction(SHOW_MODAL);
//TODO SEND USER_ID SET IN STORAGE
const initialState = {
userId: localStorage.getItem("userId"),
authenticated: localStorage.getItem("token"),
albumCollection: [],
cart: {},
coins: 0,
showModal: false,
error: ""
};
export default handleActions(
{
...pender({
type: GET_USER,
onSuccess: (state, { payload }) => {
const newState = { ...state, updatedAt: getDate(), error: null };
newState.albumCollection = payload.data.user.albumCollection;
newState.coins = payload.data.user.coins;
return newState;
},
onFailure: (state, { payload }) => {
return { ...state, error: 'bad' };
}
}),
...pender({
type: SIGN_UP,
onSuccess: (state, { payload }) => {
const newState = { ...state, updatedAt: getDate(), error: null };
localStorage.setItem("token", payload.data.token);
localStorage.setItem("userId", payload.data.userId);
newState.authenticated = payload.data.token;
newState.userId = payload.data.userId;
newState.albumCollection = payload.data.albumCollection;
newState.cart = payload.data.cart;
newState.coins = payload.data.coins;
return newState;
},
onFailure: (state, { payload }) => {
return { ...state, error: payload.response.data.error };
}
}),
...pender({
type: SIGN_IN,
onSuccess: (state, { payload }) => {
const newState = { ...state, updatedAt: getDate(), error: null };
console.log(payload);
localStorage.setItem("token", payload.data.token);
localStorage.setItem("userId", payload.data.userId);
newState.userId = payload.data.userId;
newState.authenticated = payload.data.token;
newState.albumCollection = payload.data.albumCollection;
newState.cart = payload.data.cart;
newState.coins = payload.data.coins;
return newState;
},
onFailure: (state, { payload }) => {
return { ...state, error: payload.response.data.error };
}
}),
...pender({
type: ADD_TO_CART,
onSuccess: (state, { payload }) => {
const newState = { ...state, updatedAt: getDate(), error: null };
console.log(payload);
newState.cart = payload.data.user.cart;
return newState;
},
onFailure: (state, { payload }) => {
return { ...state, error: payload.response.data.error };
}
}),
...pender({
type: GET_COINS,
onSuccess: (state, { payload }) => {
const newState = { ...state, updatedAt: getDate(), error: null };
newState.coins = payload.data.coins;
return newState;
},
onFailure: (state, { payload }) => {
return { ...state, error: payload.response.data.error };
}
}),
...pender({
type: EXCHANGE_COINS,
onSuccess: (state, { payload }) => {
const newState = { ...state, updatedAt: getDate(), error: null };
localStorage.setItem("coins", payload.data.coins);
newState.coins = payload.data.coins;
return newState;
},
onFailure: (state, { payload }) => {
return { ...state, error: payload.response.data.error };
}
}),
[SIGN_OUT]: state => {
const newState = { ...state, updatedAt: getDate(), error: null };
localStorage.removeItem("token");
newState.authenticated = "";
return newState;
},
[SHOW_MODAL]: state => {
const newState = { ...state, updatedAt: getDate(), error: null };
newState.showModal = !state.showModal;
return newState;
}
},
initialState
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment