├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt └── src ├── App.js ├── Counter.js ├── Modal.js ├── Products.js ├── actions.js ├── countReducer.js ├── index.css ├── index.js ├── modalReducer.js └── productReducer.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `npm test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `npm run build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `npm run eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `npm run build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-tutorial", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^4.2.4", 7 | "@testing-library/react": "^9.4.0", 8 | "@testing-library/user-event": "^7.1.2", 9 | "react": "^16.12.0", 10 | "react-dom": "^16.12.0", 11 | "react-redux": "^7.1.3", 12 | "react-scripts": "3.3.0", 13 | "redux": "^4.0.4", 14 | "redux-devtools-extension": "^2.13.8", 15 | "redux-thunk": "^2.3.0" 16 | }, 17 | "scripts": { 18 | "start": "react-scripts start", 19 | "build": "react-scripts build", 20 | "test": "react-scripts test", 21 | "eject": "react-scripts eject" 22 | }, 23 | "eslintConfig": { 24 | "extends": "react-app" 25 | }, 26 | "browserslist": { 27 | "production": [ 28 | ">0.2%", 29 | "not dead", 30 | "not op_mini all" 31 | ], 32 | "development": [ 33 | "last 1 chrome version", 34 | "last 1 firefox version", 35 | "last 1 safari version" 36 | ] 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-smilga/redux-tutorial-simple-counter/0583d59cae8121b7f2b825d8dca3cae4738f9fc5/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | Redux App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-smilga/redux-tutorial-simple-counter/0583d59cae8121b7f2b825d8dca3cae4738f9fc5/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-smilga/redux-tutorial-simple-counter/0583d59cae8121b7f2b825d8dca3cae4738f9fc5/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | // Provider, connect - react-redux 2 | 3 | import React from "react"; 4 | import Counter from "./Counter"; 5 | import { createStore, combineReducers, applyMiddleware } from "redux"; 6 | import thunk from "redux-thunk"; 7 | import { Provider } from "react-redux"; 8 | import { composeWithDevTools } from "redux-devtools-extension"; 9 | import Modal from "./Modal"; 10 | import Products from "./Products"; 11 | 12 | import countReducer from "./countReducer"; 13 | import modalReducer from "./modalReducer"; 14 | import productReducer from "./productReducer"; 15 | import { getProducts } from "./actions"; 16 | const middleware = [thunk]; 17 | // setup store 18 | const store = createStore( 19 | combineReducers({ 20 | countState: countReducer, 21 | modalState: modalReducer, 22 | productState: productReducer 23 | }), 24 | composeWithDevTools(applyMiddleware(...middleware)) 25 | ); 26 | // store.dispatch(getProducts()); 27 | const App = () => { 28 | return ( 29 | 30 | 31 | 32 | 33 | 34 | ); 35 | }; 36 | 37 | export default App; 38 | -------------------------------------------------------------------------------- /src/Counter.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { connect } from "react-redux"; 3 | import { DECREASE, RESET, INCREASE, MODAL_OPEN } from "./actions"; 4 | import { modalOpen } from "./actions"; 5 | function Counter({ name, count, increase, decrease, reset }) { 6 | return ( 7 |
8 |
9 |

counter

10 |

{name}

11 |

{count}

12 |
13 | 16 | 19 | 22 |
23 |
24 |
25 | ); 26 | } 27 | function mapStateToProps(state) { 28 | return { count: state.countState.count, name: state.countState.name }; 29 | } 30 | function mapDispatchToProps(dispatch, ownProps) { 31 | // console.log(ownProps); 32 | 33 | return { 34 | increase: () => dispatch({ type: INCREASE }), 35 | decrease: () => dispatch({ type: DECREASE }), 36 | reset: () => { 37 | dispatch({ type: RESET }); 38 | dispatch( 39 | modalOpen( 40 | "susan", 41 | " Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aperiam sed quis totam saepe mollitia tempore, modi eveniet repellat! Odio, non!" 42 | ) 43 | ); 44 | // dispatch({ 45 | // type: MODAL_OPEN, 46 | // payload: { name: "bob", text: "hello there user" } 47 | // }); 48 | } 49 | }; 50 | } 51 | 52 | export default connect(mapStateToProps, mapDispatchToProps)(Counter); 53 | -------------------------------------------------------------------------------- /src/Modal.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import PropTypes from "prop-types"; 3 | import { connect } from "react-redux"; 4 | import { MODAL_CLOSE } from "./actions"; 5 | const Modal = ({ isOpen, name, text, close }) => { 6 | // const isOpen = true; 7 | return ( 8 |
9 |
10 |

{name}

11 |

{text}

12 | 15 |
16 |
17 | ); 18 | }; 19 | 20 | Modal.propTypes = { 21 | isOpen: PropTypes.bool.isRequired, 22 | name: PropTypes.string.isRequired, 23 | text: PropTypes.string.isRequired, 24 | close: PropTypes.func.isRequired 25 | }; 26 | function mapStateToProps({ modalState: { isOpen, name, text } }) { 27 | return { isOpen, name, text }; 28 | } 29 | function mapDispatchToProps(dispatch) { 30 | return { close: () => dispatch({ type: MODAL_CLOSE }) }; 31 | } 32 | export default connect(mapStateToProps, mapDispatchToProps)(Modal); 33 | -------------------------------------------------------------------------------- /src/Products.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import PropTypes from "prop-types"; 3 | import { connect } from "react-redux"; 4 | import { getProducts } from "./actions"; 5 | const Products = ({ getProducts, loading, products }) => { 6 | React.useEffect(() => { 7 | getProducts(); 8 | }, [getProducts]); 9 | if (loading) { 10 | return

Loading...

; 11 | } 12 | return ( 13 |
14 |

our products

15 | 25 |
26 | ); 27 | }; 28 | 29 | Products.propTypes = { 30 | loading: PropTypes.bool.isRequired, 31 | products: PropTypes.arrayOf(PropTypes.object).isRequired, 32 | getProducts: PropTypes.func.isRequired 33 | }; 34 | const mapStateToProps = ({ productState: { products, loading } }) => { 35 | return { loading, products }; 36 | }; 37 | export default connect(mapStateToProps, { getProducts })(Products); 38 | -------------------------------------------------------------------------------- /src/actions.js: -------------------------------------------------------------------------------- 1 | // count actions 2 | export const DECREASE = "DECREASE"; 3 | export const INCREASE = "INCREASE"; 4 | export const RESET = "RESET"; 5 | // modal actions 6 | export const MODAL_OPEN = "MODAL_OPEN"; 7 | export const MODAL_CLOSE = "MODAL_CLOSE"; 8 | // products actions 9 | export const SET_LOADING = "SET_LOADING"; 10 | export const GET_PRODUCTS = "GET_PRODUCTS"; 11 | // action creators 12 | export const modalOpen = (name, text) => { 13 | return { type: MODAL_OPEN, payload: { name, text } }; 14 | }; 15 | 16 | export const setLoading = () => { 17 | return { type: SET_LOADING }; 18 | }; 19 | export const getProducts = () => { 20 | return async function(dispatch) { 21 | dispatch(setLoading()); 22 | const response = await fetch( 23 | "https://johnsmilgatutorials.com/projects/react-tech-store-v2/products" 24 | ); 25 | const data = await response.json(); 26 | dispatch({ type: GET_PRODUCTS, payload: data }); 27 | }; 28 | }; 29 | -------------------------------------------------------------------------------- /src/countReducer.js: -------------------------------------------------------------------------------- 1 | import { DECREASE, RESET, INCREASE } from "./actions"; 2 | const defaultState = { 3 | count: 0, 4 | name: "john" 5 | }; 6 | export default function reducer(state = defaultState, action) { 7 | switch (action.type) { 8 | case DECREASE: 9 | return { ...state, count: state.count - 1 }; 10 | case INCREASE: 11 | return { ...state, count: state.count + 1 }; 12 | case RESET: 13 | return { ...state, count: 0 }; 14 | default: 15 | return state; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | /* 2 | ====== 3 | Fonts 4 | ====== 5 | */ 6 | @import url("https://fonts.googleapis.com/css?family=Catamaran:300,400&display=swap"); 7 | /* 8 | ====== 9 | Variables 10 | ====== 11 | */ 12 | :root { 13 | --primaryColor: #2892d7; 14 | --mainWhite: #fff; 15 | --offWhite: #f7f7f7; 16 | --mainBlack: #222; 17 | --mainGrey: #ececec; 18 | --darkGrey: #afafaf; 19 | --mainTransition: all 0.3s linear; 20 | --mainSpacing: 0.3rem; 21 | --lightShadow: 2px 5px 3px 0px rgba(0, 0, 0, 0.5); 22 | --darkShadow: 4px 10px 5px 0px rgba(0, 0, 0, 0.5); 23 | --mainBorderRadius: 0.25rem; 24 | --smallWidth: 85vw; 25 | --maxWidth: 1170px; 26 | } 27 | /* 28 | ====== 29 | Global Styles 30 | ====== 31 | */ 32 | * { 33 | margin: 0; 34 | padding: 0; 35 | box-sizing: border-box; 36 | } 37 | 38 | body { 39 | font-family: "Catamaran", sans-serif; 40 | color: var(--mainBlack); 41 | background: var(--mainWhite); 42 | line-height: 1.4; 43 | font-size: 1rem; 44 | font-weight: 300; 45 | } 46 | h1, 47 | h2, 48 | h3, 49 | h4, 50 | h5, 51 | h6 { 52 | margin-bottom: 1.25rem; 53 | letter-spacing: var(--mainSpacing); 54 | } 55 | p { 56 | margin-bottom: 1.25rem; 57 | } 58 | ul { 59 | list-style-type: none; 60 | } 61 | a { 62 | text-decoration: none; 63 | color: var(--mainBlack); 64 | } 65 | /* 66 | ====== 67 | Buttons 68 | ====== 69 | */ 70 | .btn { 71 | text-transform: uppercase; 72 | letter-spacing: var(--mainSpacing); 73 | color: var(--mainBlack); 74 | border: 2px solid var(--mainBlack); 75 | padding: 0.45rem 0.8rem; 76 | display: inline-block; 77 | transition: var(--mainTransition); 78 | cursor: pointer; 79 | font-size: 0.8rem; 80 | border-radius: var(--mainBorderRadius); 81 | background: transparent; 82 | } 83 | .btn:hover { 84 | background: var(--mainBlack); 85 | color: var(--mainWhite); 86 | } 87 | /* 88 | ====== 89 | Counter 90 | ====== 91 | */ 92 | 93 | .container { 94 | min-height: 100vh; 95 | display: flex; 96 | justify-content: center; 97 | align-items: center; 98 | text-align: center; 99 | text-transform: capitalize; 100 | } 101 | .container h1 { 102 | font-size: 3rem; 103 | } 104 | .buttons { 105 | display: flex; 106 | flex-wrap: wrap; 107 | justify-content: center; 108 | } 109 | .counter { 110 | font-size: 10rem; 111 | } 112 | .btn { 113 | margin: 0.25rem; 114 | } 115 | /* 116 | ====== 117 | Modal 118 | ====== 119 | */ 120 | 121 | .modal-overlay { 122 | position: fixed; 123 | top: 0; 124 | left: 0; 125 | width: 100%; 126 | height: 100%; 127 | background: rgba(0, 0, 0, 0.5); 128 | opacity: 0; 129 | z-index: -1; 130 | } 131 | .modal-overlay.isModalOpen { 132 | opacity: 1; 133 | z-index: 10; 134 | } 135 | .modal-container { 136 | background: #fff; 137 | width: 80vw; 138 | max-width: 30rem; 139 | margin: 0 auto; 140 | margin-top: -6rem; 141 | border-radius: var(--mainBorderRadius); 142 | text-align: center; 143 | text-transform: capitalize; 144 | padding: 4rem 2rem; 145 | transition: all 0s ease-in-out; 146 | } 147 | .modal-container p { 148 | margin-bottom: 5rem; 149 | } 150 | .modal-overlay.isModalOpen .modal-container { 151 | margin-top: 6rem; 152 | } 153 | /* 154 | ====== 155 | Products 156 | ====== 157 | */ 158 | .section { 159 | padding: 4rem 0; 160 | } 161 | .section-title { 162 | font-size: 2rem; 163 | text-transform: capitalize; 164 | letter-spacing: var(--mainSpacing); 165 | text-align: center; 166 | margin: 3.5rem 0; 167 | } 168 | .products { 169 | width: var(--smallWidth); 170 | margin: 0 auto; 171 | max-width: 20rem; 172 | display: grid; 173 | grid-template-columns: 1fr; 174 | row-gap: 4rem; 175 | } 176 | .product { 177 | text-align: center; 178 | } 179 | .product img { 180 | width: 100%; 181 | } 182 | @media screen and (min-width: 768px) { 183 | .products { 184 | max-width: 35rem; 185 | grid-template-columns: 1fr 1fr 1fr; 186 | column-gap: 4rem; 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import "./index.css"; 4 | import App from "./App"; 5 | 6 | ReactDOM.render(, document.getElementById("root")); 7 | -------------------------------------------------------------------------------- /src/modalReducer.js: -------------------------------------------------------------------------------- 1 | import { MODAL_OPEN, MODAL_CLOSE } from "./actions"; 2 | 3 | const defaultSate = { 4 | isOpen: false, 5 | name: "", 6 | text: "" 7 | }; 8 | 9 | export default function modalReducer(state = defaultSate, action) { 10 | if (action.type === MODAL_OPEN) { 11 | return { 12 | ...state, 13 | isOpen: true, 14 | name: action.payload.name, 15 | text: action.payload.text 16 | }; 17 | } 18 | if (action.type === MODAL_CLOSE) { 19 | return { ...state, isOpen: false, name: "", text: "" }; 20 | } 21 | return state; 22 | } 23 | -------------------------------------------------------------------------------- /src/productReducer.js: -------------------------------------------------------------------------------- 1 | import { SET_LOADING, GET_PRODUCTS } from "./actions"; 2 | 3 | const defaultState = { 4 | loading: false, 5 | products: [] 6 | }; 7 | 8 | export default function(state = defaultState, action) { 9 | if (action.type === SET_LOADING) { 10 | return { ...state, loading: true }; 11 | } 12 | if (action.type === GET_PRODUCTS) { 13 | return { ...state, loading: false, products: action.payload }; 14 | } 15 | return state; 16 | } 17 | --------------------------------------------------------------------------------