forked from arp82/samples-for-react-testing-library-training
-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/issue#2_redux_arquitecture #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Carchuli
wants to merge
8
commits into
develop
Choose a base branch
from
feature/issue#2-redux-arquitecture
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f17d2cd
fetching data into store
6f2cef5
new packages and webpack updated to be able to use scss and css
397791a
delete and add a fruit
6b0e4b7
new component folder
9b112c0
fix
442991d
changed mocked fruits to fake posts api
749a4ed
updated tsconfig file
7c5bb0e
merge develop and solve conflicts
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import * as React from 'react'; | ||
| import { MessagesSection } from './components'; | ||
| import { MyComponent } from './components/myComponent'; | ||
| import './styles.less'; | ||
|
|
||
| export const App: React.FunctionComponent = props => ( | ||
| export const App: React.FunctionComponent = (props) => ( | ||
| <div> | ||
| <MessagesSection /> | ||
| <MyComponent nameFromProps="Fruit User" /> | ||
| </div> | ||
| ); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { MyComponent } from './myComponent'; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| import { useSelector, useDispatch } from 'react-redux'; | ||
| import { | ||
| /* fetchfruits, | ||
| deleteFruit, | ||
| addFruit, */ | ||
| fetchPosts, | ||
| deletePost, | ||
| addPost, | ||
| } from './../../redux/actions/index'; | ||
| import './styles.less'; | ||
|
|
||
| import Button from '@material-ui/core/Button'; | ||
| import Chip from '@material-ui/core/Chip'; | ||
|
|
||
| import { MyInputComponent } from '../myInputComponent/myInputComponent'; | ||
| export interface Props { | ||
| nameFromProps: string; | ||
| } | ||
|
|
||
| export const MyComponent: React.FunctionComponent<Props> = (props) => { | ||
| const { nameFromProps } = props; | ||
| const dispatch = useDispatch(); | ||
| const posts = useSelector((state) => state.posts.posts); | ||
|
|
||
| return ( | ||
| <> | ||
| <div className="container"> | ||
| <h1>Hello {nameFromProps}!</h1> | ||
| <Button | ||
| variant="contained" | ||
| color="primary" | ||
| onClick={() => dispatch(fetchPosts())} | ||
| > | ||
| Get post from API | ||
| </Button> | ||
| <div className="chip-container"> | ||
| {!!posts | ||
| ? posts.map((el /* : Object */) => ( | ||
| <Chip | ||
| color="primary" | ||
| label={el.title} | ||
| key={el.id} | ||
| onDelete={() => dispatch(deletePost(el.id))} | ||
| /> | ||
| )) | ||
| : null} | ||
| </div> | ||
| <MyInputComponent addPost={(newPost) => dispatch(addPost(newPost))} /> | ||
| </div> | ||
| </> | ||
| ); | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| .container { | ||
| text-align: center; | ||
| .chip-container { | ||
| display: block; | ||
| position: relative; | ||
| margin-top: 25px; | ||
| .MuiChip-root { | ||
| margin-left: 5px; | ||
| margin-top: 5px; | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { MyInputComponent } from './myInputComponent'; |
31 changes: 31 additions & 0 deletions
31
00 Base/src/components/myInputComponent/myInputComponent.tsx
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import * as React from 'react'; | ||
| import TextField from '@material-ui/core/TextField'; | ||
| import { Add } from '@material-ui/icons'; | ||
| import './styles.less'; | ||
|
|
||
| export interface Props { | ||
| addPost: Function; | ||
| } | ||
|
|
||
| export const MyInputComponent: React.FunctionComponent<Props> = (props) => { | ||
| const { addPost } = props; | ||
| const [name, setName] = React.useState(''); | ||
|
|
||
| return ( | ||
| <div className="input-component"> | ||
| <TextField | ||
| label="Add post" | ||
| value={name} | ||
| onChange={(e) => setName(e.target.value)} | ||
| /> | ||
| {!!name ? ( | ||
| <Add | ||
| onClick={() => { | ||
| addPost(name); | ||
| setName(''); | ||
| }} | ||
| /> | ||
| ) : null} | ||
| </div> | ||
| ); | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| .input-component { | ||
| .MuiFormControl-root { | ||
| margin-top: 10px; | ||
| } | ||
| .MuiSvgIcon-root { | ||
| margin-top: 35px; | ||
| color: #00ff00; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,14 @@ | ||
| import * as React from 'react'; | ||
| import * as ReactDOM from 'react-dom'; | ||
| import { App } from './app'; | ||
| import { Provider } from 'react-redux' | ||
| import { createStore, compose, applyMiddleware } from 'redux' | ||
| import {MessagesReducer} from './redux/reducers/MessagesReducer' | ||
| import thunk from 'redux-thunk' | ||
| import { Provider } from 'react-redux'; | ||
| import store from './redux/store'; | ||
|
|
||
| const MessagesStore = createStore(MessagesReducer,compose(applyMiddleware(thunk))); | ||
| import { App } from './app'; | ||
| import 'babel-polyfill' | ||
|
|
||
| ReactDOM.render( | ||
| <Provider store={MessagesStore}> | ||
| <Provider store={store}> | ||
| <App /> | ||
| </Provider>, | ||
|
|
||
| document.getElementById('root')); | ||
| </Provider>, | ||
| document.getElementById('root') | ||
| ); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { getListOfFruit, getPosts } from './myApi'; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import * as BEApi from './myBackEndApiEndpoint'; | ||
| import axios from 'axios'; | ||
|
|
||
| export const getListOfFruit = (): Promise<string[]> => { | ||
| return BEApi.getFruits('http://fruityfruit.com') | ||
| .then(resolveFruits) | ||
| .catch(handleError); | ||
| }; | ||
|
|
||
| const resolveFruits = (fruits: string[]) => { | ||
| return fruits; | ||
| }; | ||
|
|
||
| const handleError = () => { | ||
| throw new Error('Where is my fruit???'); | ||
| }; | ||
| export const getPosts = (): any => { | ||
| return axios | ||
| .get('https://jsonplaceholder.typicode.com/posts') | ||
| .then(resolvePosts); | ||
| }; | ||
| const resolvePosts = (posts: any) => { | ||
| return posts; | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| export const getFruits = (_url: string) => { | ||
| return Promise.resolve([ | ||
| 'grape', | ||
| 'pineapple', | ||
| 'watermelon', | ||
| 'orange', | ||
| 'lemon', | ||
| 'strawberry', | ||
| 'cherry', | ||
| 'peach', | ||
| ]); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { GET_ALL_FRUITS, DELETE_FRUIT, ADD_FRUIT } from './../constants'; | ||
| import { getListOfFruit } from '../../myApi/index'; | ||
|
|
||
| export const getAllFruits = (fruits: String[]) => { | ||
| return { | ||
| type: GET_ALL_FRUITS, | ||
| payload: fruits, | ||
| }; | ||
| }; | ||
| export const fetchfruits = () => { | ||
| return (dispatch) => { | ||
| return getListOfFruit().then((res) => dispatch(getAllFruits(res))); | ||
| }; | ||
| }; | ||
|
|
||
| export const deleteFruit = (name: string) => { | ||
| return { | ||
| type: DELETE_FRUIT, | ||
| payload: name, | ||
| }; | ||
| }; | ||
|
|
||
| export const addFruit = (name: string) => { | ||
| return { | ||
| type: ADD_FRUIT, | ||
| payload: name, | ||
| }; | ||
| }; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export { | ||
| addFruit, | ||
| deleteFruit, | ||
| fetchfruits, | ||
| getAllFruits, | ||
| } from './fruitsActions'; | ||
| export { fetchPosts, deletePost,addPost } from './postActions'; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { GET_POST_FROM_API,DELETE_POST,ADD_POST } from './../constants'; | ||
| import { getPosts } from '../../myApi/index'; | ||
|
|
||
| export const getPostFromApi = ( posts: Object[]) => { | ||
| return { | ||
| type: GET_POST_FROM_API, | ||
| payload: posts, | ||
| }; | ||
| }; | ||
| export const fetchPosts = () => { | ||
| return (dispatch) => { | ||
| return getPosts().then((res) => dispatch(getPostFromApi(res))); | ||
| }; | ||
| }; | ||
|
|
||
| export const deletePost = (id: Number) => { | ||
| return { | ||
| type: DELETE_POST, | ||
| payload: id, | ||
| }; | ||
| }; | ||
|
|
||
| export const addPost = (name: string) => { | ||
| return { | ||
| type: ADD_POST, | ||
| payload: name, | ||
| }; | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export const GET_ALL_FRUITS = 'GET_ALL_FRUITS'; | ||
| export const DELETE_FRUIT = 'DELETE_FRUIT'; | ||
| export const ADD_FRUIT = 'ADD_FRUIT'; | ||
|
|
||
| export const GET_POST_FROM_API = 'GET_POST_FROM_API'; | ||
| export const DELETE_POST = 'DELETE_POST'; | ||
| export const ADD_POST = 'ADD_POST'; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { GET_ALL_FRUITS, DELETE_FRUIT, ADD_FRUIT } from './../constants'; | ||
| const initialState = { | ||
| fruitList: [], | ||
| }; | ||
| export default function (state = initialState, action) { | ||
| switch (action.type) { | ||
| case GET_ALL_FRUITS: { | ||
| return { | ||
| ...state, | ||
| fruitList: action.payload, | ||
| }; | ||
| } | ||
| case DELETE_FRUIT: { | ||
| return { | ||
| ...state, | ||
| fruitList: state.fruitList.filter((e: string) => e !== action.payload), | ||
| }; | ||
| } | ||
| case ADD_FRUIT: { | ||
| return { | ||
| ...state, | ||
| fruitList: state.fruitList.concat([action.payload]), | ||
| }; | ||
| } | ||
| default: { | ||
| return { | ||
| ...state, | ||
| }; | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { combineReducers } from 'redux'; | ||
| import fruitsReducer from './fruitReducer'; | ||
| import postReducer from './postReducer' | ||
|
|
||
| export default combineReducers({ | ||
| fruits: fruitsReducer, | ||
| posts: postReducer | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please could you simulate api requests using axios with this fake api: https://jsonplaceholder.typicode.com/?