-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring application to redux (flux pattern)
- Loading branch information
Luiz Felicio
committed
Dec 19, 2019
1 parent
417da66
commit f970b81
Showing
12 changed files
with
135 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { fetchCardsPending, fetchCardsSuccess, fetchCardsFailure } from './index'; | ||
import api from '../../api'; | ||
|
||
export const fetchCards = () => (dispatch) => { | ||
dispatch(fetchCardsPending()); | ||
api.get('/cards') | ||
.then((response) => dispatch(fetchCardsSuccess(response.data.cards))) | ||
.catch((error) => dispatch(fetchCardsFailure(error))); | ||
}; | ||
|
||
export const fetchCardByPokeName = (input) => (dispatch) => { | ||
dispatch(fetchCardsPending()); | ||
api.get(`/cards?name=${input}`) | ||
.then((response) => dispatch(fetchCardsSuccess(response.data.cards))) | ||
.catch((error) => dispatch(fetchCardsFailure(error))); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { FETCH_CARDS_PENDING, FETCH_CARDS_SUCCESS, FETCH_CARDS_ERROR } from '../types'; | ||
|
||
export const test = () => {}; | ||
|
||
export const fetchCardsPending = () => ({ | ||
type: FETCH_CARDS_PENDING, | ||
}); | ||
|
||
export const fetchCardsSuccess = (payload) => ({ | ||
type: FETCH_CARDS_SUCCESS, | ||
payload, | ||
}); | ||
|
||
export const fetchCardsFailure = (payload) => ({ | ||
type: FETCH_CARDS_ERROR, | ||
payload, | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { FETCH_CARDS_PENDING, FETCH_CARDS_SUCCESS, FETCH_CARDS_ERROR } from '../types'; | ||
|
||
const INITAL_STATE = { | ||
cards: [], | ||
loading: false, | ||
error: false, | ||
}; | ||
|
||
const cardsReducer = (state = INITAL_STATE, action) => { | ||
switch (action.type) { | ||
case FETCH_CARDS_PENDING: | ||
return { | ||
...state, | ||
loading: true, | ||
error: false, | ||
}; | ||
case FETCH_CARDS_SUCCESS: | ||
return { | ||
...state, | ||
cards: state.cards.concat(action.payload), | ||
loading: true, | ||
error: false, | ||
}; | ||
|
||
case FETCH_CARDS_ERROR: | ||
return { | ||
...state, | ||
loading: false, | ||
error: true, | ||
}; | ||
|
||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default cardsReducer; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { createStore, applyMiddleware } from 'redux'; | ||
import { composeWithDevTools } from 'redux-devtools-extension'; | ||
import cardsReducer from '../reducers/cardsReducer'; | ||
import thunk from 'redux-thunk'; | ||
|
||
const middlewares = [thunk]; | ||
|
||
const store = createStore( | ||
cardsReducer, | ||
composeWithDevTools( | ||
applyMiddleware(...middlewares), | ||
), | ||
); | ||
|
||
export default store; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const FETCH_CARDS_PENDING = 'FETCH_CARDS_PENDING'; | ||
export const FETCH_CARDS_SUCCESS = 'FETCH_CARDS_SUCCESS'; | ||
export const FETCH_CARDS_ERROR = 'FETCH_CARDS_ERROR'; | ||
export const UPDATE_CARDS = 'UPDATE_CARDS'; | ||
export const DELETE_CARDS = 'DELETE_CARDS'; |
This file contains 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