Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { combineReducers } from 'redux'
- import { createStore, applyMiddleware } from 'redux';
- import thunk from 'redux-thunk';
- const initialState = {
- currentUser: {
- credentials: {},
- user: {}
- },
- test: {},
- users: []
- }
- export const SUBMIT_LOGIN = 'SUBMIT_LOGIN'
- export const SET_USER = 'SET_USER'
- export const TEST = 'TEST'
- export const SET_USERS = 'SET_USERS'
- export const SET_CREDENTIALS = 'SET_CREDENTIALS'
- //actions
- const submitLogin = () => (dispatch) => {
- return postLoginRequest()
- .then(response => {
- dispatch(setCredentials(
- response.headers.get('access-token'),
- response.headers.get('client'),
- response.headers.get('expiry'),
- response.headers.get('token-type'),
- response.headers.get('uid')
- ));
- return response
- })
- .then(response => {
- return response.json();
- })
- .then(
- (user) => dispatch(setUser(user.data)),
- );
- }
- const performRequest = (api) => (dispatch) => {
- return api()
- .then(response => {
- dispatch(setCredentials(
- response.headers.get('access-token'),
- response.headers.get('client'),
- response.headers.get('expiry'),
- response.headers.get('token-type'),
- response.headers.get('uid')
- ));
- return response
- })
- .then(response => {return response.json()})
- .then(json => {console.log(json)})
- }
- const setUsers = (users) => {
- return {
- type: SET_USERS,
- users
- }
- }
- const setUser = (user) => {
- return {
- type: SET_USER,
- user
- }
- }
- const setCredentials = (
- access_token,
- client,
- expiry,
- token_type,
- uid
- ) => {
- return {
- type: SET_CREDENTIALS,
- credentials: {
- 'access-token': access_token,
- client,
- expiry,
- 'token-type': token_type,
- uid
- }
- }
- }
- const currentUserInitialState = {
- credentials: {},
- user: {}
- }
- const currentUser = (state = currentUserInitialState, action) => {
- switch (action.type) {
- case SET_USER:
- return Object.assign({}, state, {user: action.user})
- case SET_CREDENTIALS:
- return Object.assign({}, state, {credentials: action.credentials})
- default:
- return state
- }
- }
- const rootReducer = combineReducers({
- currentUser,
- test
- })
- const getAuthorizedHeader = (store) => {
- const credentials = store.getState().currentUser.credentials
- const headers = new Headers(credentials)
- return headers
- }
- //store creation
- const createStoreWithMiddleware = applyMiddleware(
- thunk
- )(createStore);
- const store = createStoreWithMiddleware(rootReducer);
- const postLoginRequest = () => {
- return fetch('http://localhost:3000/auth/sign_in', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({
- email: 'test@test.com',
- password: 'password',
- })
- })
- }
- const getUsers = () => {
- const autorizedHeader = getAuthorizedHeader(store)
- debugger
- return fetch('http://localhost:3000/users',
- {
- method: 'GET',
- headers : autorizedHeader
- }
- )
- }
- store.dispatch(submitLogin())
- setTimeout(() => {
- console.log(store.dispatch(performRequest(getUsers)))
- }, 2000)
- config.batch_request_buffer_throttle = 5.seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement