Last active
January 8, 2017 09:58
-
-
Save RayLuxembourg/74bde65154940489ea96aac50670537c to your computer and use it in GitHub Desktop.
React.js Modular Crud with Redux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Actions | |
import axios from 'axios'; | |
import * as types from '../constants/actionTypes'; | |
import { browserHistory } from 'react-router'; | |
import setAuth from '../radiomize/auth/setAuth'; | |
import jwt from 'jsonwebtoken'; | |
const AUTH_URL = 'http://dev.admin.api.radiomize.com/v1/login'; | |
const BASE_URL = 'http://dev.admin.api.radiomize.com/v1/'; | |
// TYPES OF ACTIONS | |
const {ENDPOINTS, ACTIONS, POST, FETCH_ALL, FETCH_SINGLE, ERROR, PROGRESS, UPDATE, DELETE} = types; | |
/** | |
*======================= | |
*TO ADD NEW METHODS GO TO SWTICHHELPER FUNCTION BELOW AND READ INSTRUCTIONS | |
*======================= | |
*/ | |
export function signinUser({email, password}) { | |
return (dispatch) => { | |
// console.log("THIS", email, password); | |
return axios.post(`${AUTH_URL}`, { Email: email, Password: password }) | |
.then(response => { | |
// dispatch({ type: types.AUTH_USER }); | |
const token = response.data.token; | |
localStorage.setItem('jwtToken', token); | |
setAuth(token); | |
dispatch(setCurrentUser(jwt.decode(token))); | |
}).catch((error) => { | |
console.log(error); | |
}); | |
} | |
} | |
export function authError(error) { | |
console.log('error!!!'); | |
return { | |
type: types.AUTH_ERROR, | |
payload: error | |
} | |
} | |
export function setCurrentUser(user) { | |
return { | |
type: types.AUTH_USER, | |
user | |
} | |
} | |
/** | |
*======================= | |
*@CRUD HELPER FUNCTIONS!!!!!!!! | |
*======================= | |
*/ | |
/** | |
* | |
* | |
* @param {any} endpoint - the api's final endpoint for the request | |
* @param {any} [id=null] - if the endpoint has id it will be used. | |
* @param {any} type - what kind of request you are doing (USE CONSTS)(GET,POST,UPDATE,DELETE) | |
* @param {any} [data=null] - wehn doing post or update you want to send data to the api. | |
* @returns full url for the request | |
*/ | |
function apiRequest(endpoint, id = null, type, data = null) { | |
switch (type) { | |
case ACTIONS.GET: | |
return axios[type](BASE_URL + endpoint + (id ? id : '')); | |
case ACTIONS.POST: | |
return axios[type](BASE_URL + endpoint, data); | |
case ACTIONS.UPDATE: | |
return axios[type](BASE_URL + endpoint, data); | |
case ACTIONS.DELETE: | |
return axios[type](BASE_URL + endpoint + id); | |
default: | |
break; | |
} | |
} | |
/** | |
* | |
* | |
* @param {any} dispatcher - used to distaptch actions to reducers. | |
* @param {any} type - Action Type(Use consts) | |
* @param {any} [payload=null] - if there is payload(data) include it. | |
* @returns a disptach to the reducers. | |
*/ | |
function dispatchHelper(dispatcher, type, payload = null) { | |
return dispatcher({ type, payload }) | |
} | |
/** | |
*======================= | |
*@SWITCH HELPER IS WHERE YOU ADD NEW REQUESTS | |
* REMEMBER TO ADD THE REQUIRED CONSTANTS,ENDPOINTS,POST,DELETE,UPDATE,FETCH | |
*======================= | |
*/ | |
/** | |
* this functions checks the endpoint and dispatches accordinglly . | |
* | |
* @param {any} condition - the condition is just the api endpoint so we would what params to pass to dispatchHelper. | |
* @param {any} dispatch - reference for the dispatch method. | |
* @param {any} type - Action Type | |
* @param {any} request - the api response object. | |
*/ | |
function switchHelper(condition, dispatch, type, request) { | |
switch (condition) { | |
case ENDPOINTS.channels: | |
dispatchHelper(dispatch, type.CHANNEL, request.data); | |
return | |
case ENDPOINTS.genres: | |
dispatchHelper(dispatch, type.GENERE, request.data); | |
return | |
case ENDPOINTS.users: | |
dispatchHelper(dispatch, type.USER, request.data); | |
return | |
default: | |
return | |
} | |
} | |
/** | |
*======================= | |
@CRUD FUNCTIONS (NO NEED TO TOCUH!!!!!) | |
*======================= | |
*/ | |
export function Get(endpoint, id = null) { | |
return async (dispatch) => { | |
try { | |
dispatchHelper(dispatch, PROGRESS.FETCH); | |
const request = await apiRequest(endpoint, id, ACTIONS.GET); | |
// When id is true | |
if (id) { | |
switchHelper(endpoint, dispatch, FETCH_SINGLE, request); | |
} | |
switchHelper(endpoint, dispatch, FETCH_ALL, request) | |
} | |
catch (e) { | |
dispatchHelper(dispatch, ERROR.FETCH_ALL, e); | |
} | |
} | |
} | |
export function Post(endpoint, data) { | |
return async (dispatch) => { | |
try { | |
dispatchHelper(dispatch, PROGRESS.POST); | |
const request = await apiRequest(endpoint, null, ACTIONS.POST, data); | |
switchHelper(endpoint, dispatch, POST, request); | |
// If i want to get new data straight after posting without navigating. | |
// If backend returns the created object after post request, no need additional requests to the server. | |
const get = await apiRequest(endpoint, null, ACTIONS.GET); | |
switchHelper(endpoint, dispatch, FETCH, get); | |
} | |
catch (e) { | |
dispatchHelper(dispatch, ERROR.POST, e); | |
} | |
} | |
} | |
export function Update(endpoint, data) { | |
return async (dispatch) => { | |
try { | |
dispatchHelper(dispatch, PROGRESS.UPDATE); | |
const request = await apiRequest(endpoint, null, ACTIONS.UPDATE, data); | |
switchHelper(endpoint, dispatch, UPDATE, request); | |
} | |
catch (e) { | |
dispatchHelper(dispatch, ERROR.POST, e); | |
} | |
} | |
} | |
export function Delete(endpoint, id, data) { | |
return async (dispatch) => { | |
try { | |
dispatchHelper(dispatch, PROGRESS.DELETE); | |
const request = await apiRequest(endpoint, id, ACTIONS.GET.POST, data); | |
switchHelper(endpoint, dispatch, DELETE, request); | |
} | |
catch (e) { | |
dispatchHelper(dispatch, ERROR.DELETE, e, ); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment