Skip to content

Commit

Permalink
into-redux
Browse files Browse the repository at this point in the history
  • Loading branch information
YvetteLau committed Sep 15, 2019
1 parent a4c3c23 commit 9d7bffc
Show file tree
Hide file tree
Showing 26 changed files with 22,688 additions and 0 deletions.
23 changes: 23 additions & 0 deletions myredux/counter-with-redux/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
13,149 changes: 13,149 additions & 0 deletions myredux/counter-with-redux/package-lock.json

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions myredux/counter-with-redux/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "redux-1",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-scripts": "3.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"redux": "^4.0.4"
}
}
19 changes: 19 additions & 0 deletions myredux/counter-with-redux/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root">
</div>
</body>
</html>
31 changes: 31 additions & 0 deletions myredux/counter-with-redux/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import { bindActionCreators } from 'redux';
import { store, newactions } from './store';

class Counter extends Component {
constructor(props) {
super(props);
this.state = {
number: store.getState().number
}
store.subscribe(()=>{
this.setState({
number: store.getState().number
});
})
}
render() {
return (
<div>
<p>{this.state.number}</p>
<button onClick={() => newactions.add(2)}>+</button>
<button onClick={() => newactions.minus(3)}>-</button>
</div>
)
}
}


ReactDOM.render(<Counter />, document.getElementById('root'));

135 changes: 135 additions & 0 deletions myredux/counter-with-redux/src/serviceWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// This optional code is used to register a service worker.
// register() is not called by default.

// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.

// To learn more about the benefits of this model and instructions on how to
// opt-in, read https://bit.ly/CRA-PWA

const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);

export function register(config) {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
return;
}

window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;

if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
checkValidServiceWorker(swUrl, config);

// Add some additional logging to localhost, pointing developers to the
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit https://bit.ly/CRA-PWA'
);
});
} else {
// Is not localhost. Just register service worker
registerValidSW(swUrl, config);
}
});
}
}

function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
);

// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');

// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration);
}
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
}

function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl)
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload();
});
});
} else {
// Service worker found. Proceed as normal.
registerValidSW(swUrl, config);
}
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
);
});
}

export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
registration.unregister();
});
}
}
2 changes: 2 additions & 0 deletions myredux/counter-with-redux/src/store/actionTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
18 changes: 18 additions & 0 deletions myredux/counter-with-redux/src/store/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as actionTypes from '../actionTypes';

const actions = {
add(number) {
return {
type: actionTypes.INCREMENT,
number
}
},
minus(number) {
return {
type: actionTypes.DECREMENT,
number
}
}
}

export default actions;
12 changes: 12 additions & 0 deletions myredux/counter-with-redux/src/store/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {createStore, bindActionCreators} from 'redux';
import reducers from './reducers';
import actions from './actions';

let store = createStore(reducers);
let newactions = bindActionCreators(actions, store.dispatch);


export {
store,
newactions
};
21 changes: 21 additions & 0 deletions myredux/counter-with-redux/src/store/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import actions from '../actions';
import * as actionTypes from '../actionTypes';

function reducer(state = {number: 0}, action) {
switch(action.type) {
case actionTypes.INCREMENT:
return {
...state,
number: state.number + action.number
}
case actionTypes.DECREMENT:
return {
...state,
number: state.number - action.number
}
default:
return state;
}
}

export default reducer;
35 changes: 35 additions & 0 deletions myredux/to-redux/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "redux-1",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-redux": "^7.1.0",
"react-scripts": "3.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"redux": "^4.0.4"
}
}
Loading

0 comments on commit 9d7bffc

Please sign in to comment.