Skip to content

Instantly share code, notes, and snippets.

@joesnellpdx
Created November 30, 2017 23:53
Show Gist options
  • Save joesnellpdx/ed5dddf7104a580d5098b3dd6d6a7992 to your computer and use it in GitHub Desktop.
Save joesnellpdx/ed5dddf7104a580d5098b3dd6d6a7992 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/wagupek
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.4/redux.js"></script>
<script src="https://fb.me/react-0.14.7.js"></script>
<script src="https://fb.me/react-dom-0.14.7.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/4.0.0/react-redux.js"></script>
</head>
<body>
<div id="root"></div>
<script id="jsbin-javascript">
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
var todo = function todo(state, action) {
switch (action.type) {
case 'ADD_TODO':
return {
id: action.id,
text: action.text,
complted: false
};
case 'TOGGLE_TODO':
if (state.id !== action.id) {
return state;
}
return _extends({}, state, {
completed: !state.completed
});
default:
return state;
}
};
var todos = function todos(state, action) {
if (state === undefined) state = [];
switch (action.type) {
case 'ADD_TODO':
return [].concat(_toConsumableArray(state), [todo(undefined, action)]);
case 'TOGGLE_TODO':
return state.map(function (t) {
return todo(t, action);
});
default:
return state;
}
};
var visibilityFilter = function visibilityFilter(state, action) {
if (state === undefined) state = 'SHOW_ALL';
switch (action.type) {
case 'SET_VISIBILITY_FILTER':
return action.filter;
default:
return state;
}
};
var _Redux = Redux;
var combineReducers = _Redux.combineReducers;
var todoApp = combineReducers({
todos: todos,
visibilityFilter: visibilityFilter
});
// action crators
var nextTodoId = 0;
var addTodo = function addTodo(text) {
return {
type: 'ADD_TODO',
id: nextTodoId++,
text: text
};
};
var setVisibilityFilter = function setVisibilityFilter(filter) {
return {
type: 'SET_VISIBILITY_FILTER',
filter: filter
};
};
var toggleTodo = function toggleTodo(id) {
return {
type: 'TOGGLE_TODO',
id: id
};
};
// end action creators
var _React = React;
var Component = _React.Component;
var _ReactRedux = ReactRedux;
var connect = _ReactRedux.connect;
// import { connect } from ReactRedux;
var Link = function Link(_ref) {
var active = _ref.active;
var children = _ref.children;
var onClick = _ref.onClick;
if (active) {
return React.createElement(
'span',
null,
children
);
}
return React.createElement(
'a',
{ href: '#',
onClick: function (e) {
e.preventDefault();
onClick();
}
},
children
);
};
// can just use mapStateToProps if in own file
// can pass props as well as state
var mapStateToLinkProps = function mapStateToLinkProps(state, ownProps) {
return {
active: ownProps.filter === state.visibilityFilter
};
};
// can just use mapDispatchToProps if in own file
// can pass props as well as state
var mapDispatchToLinkProps = function mapDispatchToLinkProps(dispatch, ownProps) {
return {
onClick: function onClick() {
dispatch(setVisibilityFilter(ownProps.filter));
}
};
};
// create wrapper component for Link component
var FilterLink = connect(mapStateToLinkProps, mapDispatchToLinkProps)(Link);
// below has been replaced by above
// class FilterLink extends Component {
// componentDidMount() {
// const { store } = this.context;
// this.unsubscribe = store.subscribe(() =>
// this.forceUpdate()
// );
// }
// componsnetWillUnmount() {
// this.unsubscribe();
// }
// render() {
// const props = this.props;
// const { store } = this.context;
// const state = store.getState();
// return (
// <Link
// active={
// }
// onClick={() =>
// store.
// }
// >
// {props.children}
// </Link>
// );
// }
// }
// FilterLink.contextTypes = {
// store: React.PropTypes.object
// };
var Footer = function Footer() {
return React.createElement(
'p',
null,
'Show:',
' ',
React.createElement(
FilterLink,
{
filter: 'SHOW_ALL'
},
'All'
),
', ',
React.createElement(
FilterLink,
{
filter: 'SHOW_ACTIVE'
},
'Active'
),
', ',
React.createElement(
FilterLink,
{
filter: 'SHOW_COMPLETED'
},
'Completed'
)
);
};
var Todo = function Todo(_ref2) {
var onClick = _ref2.onClick;
var completed = _ref2.completed;
var text = _ref2.text;
return React.createElement(
'li',
{
onClick: onClick,
style: {
textDecoration: completed ? 'line-through' : 'none'
}
},
text
);
};
var TodoList = function TodoList(_ref3) {
var todos = _ref3.todos;
var onTodoClick = _ref3.onTodoClick;
return React.createElement(
'ul',
null,
todos.map(function (todo) {
return React.createElement(Todo, _extends({
key: todo.id
}, todo, {
onClick: function () {
return onTodoClick(todo.id);
}
}));
})
);
};
console.log('xx');
var AddTodo = function AddTodo(_ref4) {
var dispatch = _ref4.dispatch;
var input = undefined;
return React.createElement(
'div',
null,
React.createElement('input', { ref: function (node) {
input = node;
} }),
React.createElement(
'button',
{ onClick: function () {
dispatch(addTodo(input.value));
input.value = '';
} },
'Add Todo'
)
);
};
// because using let can do the following
// to create component wrapping AddTodo component
// because only needing to pass dispatch to props can do
// this instead of below
AddTodo = connect()(AddTodo);
// AddTodo = connect(
// // do not need to pass state
// null,
// // state => {
// // return {};
// // },
// // passing dispatch as props
// // if do null here, dispatch will be passed as props
// null
// // dispatch => {
// // return { dispatch }
// // }
// )(AddTodo);
var getVisibleTodos = function getVisibleTodos(todos, filter) {
switch (filter) {
case 'SHOW_ALL':
return todos;
case 'SHOW_ACTIVE':
return todos.filter(function (t) {
return !t.completed;
});
case 'SHOW_COMPLETED':
return todos.filter(function (t) {
return t.completed;
});
}
};
// can just use mapStateToProps if in own file
var mapStateToTodoListProps = function mapStateToTodoListProps(state) {
return {
todos: getVisibleTodos(state.todos, state.visibilityFilter)
};
};
// can just use mapDispatchToProps if in own file
var mapDispatchToTodListProps = function mapDispatchToTodListProps(dispatch) {
return {
onTodoClick: function onTodoClick(id) {
dispatch(toggleTodo(id));
}
};
};
var VisibleTodoList = connect(mapStateToTodoListProps, mapDispatchToTodListProps)(TodoList);
// Removed below and replaced
// with the above function
// to create a container component
// using mapStateToProps and mapDispatchToProps
// and connect from ReactRedux
// class VisibleTodoList extends Component {
// componentDidMount() {
// const { store } = this.context;
// this.unsubscribe = store.subscribe(() =>
// this.forceUpdate()
// );
// }
// componsnetWillUnmount() {
// this.unsubscribe();
// }
// render() {
// const props = this.props;
// const { store } = this.context;
// const state = store.getState();
// return (
// <TodoList
// todos={
// }
// onTodoClick={
// }
// />
// );
// }
// }
// VisibleTodoList.contextTypes = {
// store: React.PropTypes.object
// };
var TodoApp = function TodoApp() {
return React.createElement(
'div',
null,
React.createElement(AddTodo, null),
React.createElement(VisibleTodoList, null),
React.createElement(Footer, null)
);
};
var _ReactRedux2 = ReactRedux;
var Provider = _ReactRedux2.Provider;
// import { Provider } from 'react-redux';
var _Redux2 = Redux;
var createStore = _Redux2.createStore;
ReactDOM.render(React.createElement(
Provider,
{ store: createStore(todoApp) },
React.createElement(TodoApp, null)
), document.getElementById('root'));
</script>
<script id="jsbin-source-javascript" type="text/javascript">
const todo = (state, action ) => {
switch (action.type ) {
case 'ADD_TODO':
return {
id: action.id,
text: action.text,
complted: false
};
case 'TOGGLE_TODO':
if( state.id !== action.id ) {
return state;
}
return {
...state,
completed: !state.completed
};
default:
return state;
}
}
const todos = (state = [], action) => {
switch( action.type ) {
case 'ADD_TODO':
return [
...state,
todo( undefined, action )
];
case 'TOGGLE_TODO':
return state.map( t =>
todo( t, action )
);
default:
return state;
}
};
const visibilityFilter = (
state = 'SHOW_ALL',
action
) => {
switch (action.type ) {
case 'SET_VISIBILITY_FILTER':
return action.filter;
default:
return state;
}
};
const { combineReducers } = Redux;
const todoApp = combineReducers({
todos,
visibilityFilter
});
// action crators
let nextTodoId = 0;
const addTodo = (text) => {
return {
type: 'ADD_TODO',
id: nextTodoId++,
text
}
}
const setVisibilityFilter = (filter)=> {
return {
type: 'SET_VISIBILITY_FILTER',
filter
}
}
const toggleTodo = (id) => {
return {
type: 'TOGGLE_TODO',
id
}
}
// end action creators
const { Component } = React;
const { connect } = ReactRedux;
// import { connect } from ReactRedux;
const Link = ({
active,
children,
onClick
}) => {
if( active ) {
return <span>{children}</span>
}
return (
<a href='#'
onClick={e => {
e.preventDefault();
onClick();
}}
>
{children}
</a>
);
};
// can just use mapStateToProps if in own file
// can pass props as well as state
const mapStateToLinkProps = (
state,
ownProps
) => {
return {
active:
ownProps.filter ===
state.visibilityFilter
}
};
// can just use mapDispatchToProps if in own file
// can pass props as well as state
const mapDispatchToLinkProps = (
dispatch,
ownProps
) => {
return {
onClick: () => {
dispatch(
setVisibilityFilter(ownProps.filter)
);
}
};
};
// create wrapper component for Link component
const FilterLink = connect (
mapStateToLinkProps,
mapDispatchToLinkProps
)(Link);
// below has been replaced by above
// class FilterLink extends Component {
// componentDidMount() {
// const { store } = this.context;
// this.unsubscribe = store.subscribe(() =>
// this.forceUpdate()
// );
// }
// componsnetWillUnmount() {
// this.unsubscribe();
// }
// render() {
// const props = this.props;
// const { store } = this.context;
// const state = store.getState();
// return (
// <Link
// active={
// }
// onClick={() =>
// store.
// }
// >
// {props.children}
// </Link>
// );
// }
// }
// FilterLink.contextTypes = {
// store: React.PropTypes.object
// };
const Footer = () => (
<p>Show:
{' '}
<FilterLink
filter='SHOW_ALL'
>
All
</FilterLink>
{', '}
<FilterLink
filter='SHOW_ACTIVE'
>
Active
</FilterLink>
{', '}
<FilterLink
filter='SHOW_COMPLETED'
>
Completed
</FilterLink>
</p>
);
const Todo = ({
onClick,
completed,
text
}) => (
<li
onClick={onClick}
style={{
textDecoration:
completed ?
'line-through' :
'none'
}}
>
{text}
</li>
);
const TodoList = ({
todos,
onTodoClick
}) => (
<ul>
{todos.map( todo =>
<Todo
key={todo.id}
{...todo}
onClick={() => onTodoClick(todo.id)}
/>
)}
</ul>
);
console.log( 'xx' );
let AddTodo = ({ dispatch }) => {
let input;
return (
<div>
<input ref={node => {
input = node;
}} />
<button onClick={() => {
dispatch(addTodo(input.value));
input.value = '';
}}>
Add Todo
</button>
</div>
);
};
// because using let can do the following
// to create component wrapping AddTodo component
// because only needing to pass dispatch to props can do
// this instead of below
AddTodo = connect()(AddTodo);
// AddTodo = connect(
// // do not need to pass state
// null,
// // state => {
// // return {};
// // },
// // passing dispatch as props
// // if do null here, dispatch will be passed as props
// null
// // dispatch => {
// // return { dispatch }
// // }
// )(AddTodo);
const getVisibleTodos = (
todos,
filter
) => {
switch (filter) {
case 'SHOW_ALL':
return todos;
case 'SHOW_ACTIVE':
return todos.filter(
t => !t.completed
);
case 'SHOW_COMPLETED':
return todos.filter(
t => t.completed
);
}
}
// can just use mapStateToProps if in own file
const mapStateToTodoListProps = (
state
) => {
return {
todos: getVisibleTodos(
state.todos,
state.visibilityFilter
)
};
};
// can just use mapDispatchToProps if in own file
const mapDispatchToTodListProps = (
dispatch
) => {
return {
onTodoClick: (id) => {
dispatch(toggleTodo(id));
}
};
};
const VisibleTodoList = connect (
mapStateToTodoListProps,
mapDispatchToTodListProps
)(TodoList);
// Removed below and replaced
// with the above function
// to create a container component
// using mapStateToProps and mapDispatchToProps
// and connect from ReactRedux
// class VisibleTodoList extends Component {
// componentDidMount() {
// const { store } = this.context;
// this.unsubscribe = store.subscribe(() =>
// this.forceUpdate()
// );
// }
// componsnetWillUnmount() {
// this.unsubscribe();
// }
// render() {
// const props = this.props;
// const { store } = this.context;
// const state = store.getState();
// return (
// <TodoList
// todos={
// }
// onTodoClick={
// }
// />
// );
// }
// }
// VisibleTodoList.contextTypes = {
// store: React.PropTypes.object
// };
const TodoApp = () => (
<div>
<AddTodo />
<VisibleTodoList />
<Footer />
</div>
);
const { Provider } = ReactRedux
// import { Provider } from 'react-redux';
const { createStore } = Redux;
ReactDOM.render(
<Provider store={createStore(todoApp)}>
<TodoApp />
</Provider>,
document.getElementById( 'root' )
);</script></body>
</html>
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
var todo = function todo(state, action) {
switch (action.type) {
case 'ADD_TODO':
return {
id: action.id,
text: action.text,
complted: false
};
case 'TOGGLE_TODO':
if (state.id !== action.id) {
return state;
}
return _extends({}, state, {
completed: !state.completed
});
default:
return state;
}
};
var todos = function todos(state, action) {
if (state === undefined) state = [];
switch (action.type) {
case 'ADD_TODO':
return [].concat(_toConsumableArray(state), [todo(undefined, action)]);
case 'TOGGLE_TODO':
return state.map(function (t) {
return todo(t, action);
});
default:
return state;
}
};
var visibilityFilter = function visibilityFilter(state, action) {
if (state === undefined) state = 'SHOW_ALL';
switch (action.type) {
case 'SET_VISIBILITY_FILTER':
return action.filter;
default:
return state;
}
};
var _Redux = Redux;
var combineReducers = _Redux.combineReducers;
var todoApp = combineReducers({
todos: todos,
visibilityFilter: visibilityFilter
});
// action crators
var nextTodoId = 0;
var addTodo = function addTodo(text) {
return {
type: 'ADD_TODO',
id: nextTodoId++,
text: text
};
};
var setVisibilityFilter = function setVisibilityFilter(filter) {
return {
type: 'SET_VISIBILITY_FILTER',
filter: filter
};
};
var toggleTodo = function toggleTodo(id) {
return {
type: 'TOGGLE_TODO',
id: id
};
};
// end action creators
var _React = React;
var Component = _React.Component;
var _ReactRedux = ReactRedux;
var connect = _ReactRedux.connect;
// import { connect } from ReactRedux;
var Link = function Link(_ref) {
var active = _ref.active;
var children = _ref.children;
var onClick = _ref.onClick;
if (active) {
return React.createElement(
'span',
null,
children
);
}
return React.createElement(
'a',
{ href: '#',
onClick: function (e) {
e.preventDefault();
onClick();
}
},
children
);
};
// can just use mapStateToProps if in own file
// can pass props as well as state
var mapStateToLinkProps = function mapStateToLinkProps(state, ownProps) {
return {
active: ownProps.filter === state.visibilityFilter
};
};
// can just use mapDispatchToProps if in own file
// can pass props as well as state
var mapDispatchToLinkProps = function mapDispatchToLinkProps(dispatch, ownProps) {
return {
onClick: function onClick() {
dispatch(setVisibilityFilter(ownProps.filter));
}
};
};
// create wrapper component for Link component
var FilterLink = connect(mapStateToLinkProps, mapDispatchToLinkProps)(Link);
// below has been replaced by above
// class FilterLink extends Component {
// componentDidMount() {
// const { store } = this.context;
// this.unsubscribe = store.subscribe(() =>
// this.forceUpdate()
// );
// }
// componsnetWillUnmount() {
// this.unsubscribe();
// }
// render() {
// const props = this.props;
// const { store } = this.context;
// const state = store.getState();
// return (
// <Link
// active={
// }
// onClick={() =>
// store.
// }
// >
// {props.children}
// </Link>
// );
// }
// }
// FilterLink.contextTypes = {
// store: React.PropTypes.object
// };
var Footer = function Footer() {
return React.createElement(
'p',
null,
'Show:',
' ',
React.createElement(
FilterLink,
{
filter: 'SHOW_ALL'
},
'All'
),
', ',
React.createElement(
FilterLink,
{
filter: 'SHOW_ACTIVE'
},
'Active'
),
', ',
React.createElement(
FilterLink,
{
filter: 'SHOW_COMPLETED'
},
'Completed'
)
);
};
var Todo = function Todo(_ref2) {
var onClick = _ref2.onClick;
var completed = _ref2.completed;
var text = _ref2.text;
return React.createElement(
'li',
{
onClick: onClick,
style: {
textDecoration: completed ? 'line-through' : 'none'
}
},
text
);
};
var TodoList = function TodoList(_ref3) {
var todos = _ref3.todos;
var onTodoClick = _ref3.onTodoClick;
return React.createElement(
'ul',
null,
todos.map(function (todo) {
return React.createElement(Todo, _extends({
key: todo.id
}, todo, {
onClick: function () {
return onTodoClick(todo.id);
}
}));
})
);
};
console.log('xx');
var AddTodo = function AddTodo(_ref4) {
var dispatch = _ref4.dispatch;
var input = undefined;
return React.createElement(
'div',
null,
React.createElement('input', { ref: function (node) {
input = node;
} }),
React.createElement(
'button',
{ onClick: function () {
dispatch(addTodo(input.value));
input.value = '';
} },
'Add Todo'
)
);
};
// because using let can do the following
// to create component wrapping AddTodo component
// because only needing to pass dispatch to props can do
// this instead of below
AddTodo = connect()(AddTodo);
// AddTodo = connect(
// // do not need to pass state
// null,
// // state => {
// // return {};
// // },
// // passing dispatch as props
// // if do null here, dispatch will be passed as props
// null
// // dispatch => {
// // return { dispatch }
// // }
// )(AddTodo);
var getVisibleTodos = function getVisibleTodos(todos, filter) {
switch (filter) {
case 'SHOW_ALL':
return todos;
case 'SHOW_ACTIVE':
return todos.filter(function (t) {
return !t.completed;
});
case 'SHOW_COMPLETED':
return todos.filter(function (t) {
return t.completed;
});
}
};
// can just use mapStateToProps if in own file
var mapStateToTodoListProps = function mapStateToTodoListProps(state) {
return {
todos: getVisibleTodos(state.todos, state.visibilityFilter)
};
};
// can just use mapDispatchToProps if in own file
var mapDispatchToTodListProps = function mapDispatchToTodListProps(dispatch) {
return {
onTodoClick: function onTodoClick(id) {
dispatch(toggleTodo(id));
}
};
};
var VisibleTodoList = connect(mapStateToTodoListProps, mapDispatchToTodListProps)(TodoList);
// Removed below and replaced
// with the above function
// to create a container component
// using mapStateToProps and mapDispatchToProps
// and connect from ReactRedux
// class VisibleTodoList extends Component {
// componentDidMount() {
// const { store } = this.context;
// this.unsubscribe = store.subscribe(() =>
// this.forceUpdate()
// );
// }
// componsnetWillUnmount() {
// this.unsubscribe();
// }
// render() {
// const props = this.props;
// const { store } = this.context;
// const state = store.getState();
// return (
// <TodoList
// todos={
// }
// onTodoClick={
// }
// />
// );
// }
// }
// VisibleTodoList.contextTypes = {
// store: React.PropTypes.object
// };
var TodoApp = function TodoApp() {
return React.createElement(
'div',
null,
React.createElement(AddTodo, null),
React.createElement(VisibleTodoList, null),
React.createElement(Footer, null)
);
};
var _ReactRedux2 = ReactRedux;
var Provider = _ReactRedux2.Provider;
// import { Provider } from 'react-redux';
var _Redux2 = Redux;
var createStore = _Redux2.createStore;
ReactDOM.render(React.createElement(
Provider,
{ store: createStore(todoApp) },
React.createElement(TodoApp, null)
), document.getElementById('root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment