Skip to content

Instantly share code, notes, and snippets.

@cakiem8x
Created December 21, 2017 23:44
Show Gist options
  • Save cakiem8x/edf73eca9e01b6437bf3c846fbd00f0a to your computer and use it in GitHub Desktop.
Save cakiem8x/edf73eca9e01b6437bf3c846fbd00f0a to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/lisemef
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://fb.me/react-15.1.0.js"></script>
<script src="https://fb.me/react-dom-15.1.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.7.2/redux.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect/1.20.2/expect.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/5.0.6/react-redux.min.js"></script>
<title>JS Bin</title>
</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 'TOGGLE_TODO':
if (state.id !== action.id) return state;
return _extends({}, state, {
completed: !state.completed
});
case 'ADD_TODO':
return action.payload;
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 nextTodoId = 0;
var addTodo = function addTodo(text) {
return {
type: 'ADD_TODO',
payload: {
id: nextTodoId++,
text: text,
completed: false
}
};
};
var _Redux = Redux;
var combineReducers = _Redux.combineReducers;
var todoApp = combineReducers({
todos: todos,
visibilityFilter: visibilityFilter
});
var _React = React;
var Component = _React.Component;
var _ReactRedux = ReactRedux;
var connect = _ReactRedux.connect;
var Style = {
complete: {
textDecoration: 'line-through'
}
};
var Link = function Link(_ref) {
var children = _ref.children;
var active = _ref.active;
var onClick = _ref.onClick;
if (active) {
return React.createElement(
'span',
null,
children
);
}
return React.createElement(
'a',
{ href: '#',
onClick: onClick
},
children
);
};
var mapStateToFilterLinkProps = function mapStateToFilterLinkProps(state, ownProps) {
return {
active: ownProps.filter === state.visibilityFilter
};
};
var mapDispatchToFilterLinkProps = function mapDispatchToFilterLinkProps(dispatch, ownProps) {
return {
onClick: function onClick() {
dispatch({
type: 'SET_VISIBILITY_FILTER',
filter: ownProps.filter
});
}
};
};
var FilterLink = connect(mapStateToFilterLinkProps, mapDispatchToFilterLinkProps)(Link);
var Todo = function Todo(_ref2) {
var id = _ref2.id;
var onClick = _ref2.onClick;
var completed = _ref2.completed;
var text = _ref2.text;
return React.createElement(
'li',
{
onClick: onClick,
style: completed ? Style.complete : {}
},
text
);
};
var getVisiableTodos = function getVisiableTodos(todos, visibilityFilter) {
switch (visibilityFilter) {
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;
});
}
};
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);
}
}));
})
);
};
var mapStateToTodoListProps = function mapStateToTodoListProps(state) {
return {
todos: getVisiableTodos(state.todos, state.visibilityFilter)
};
};
var mapDispatchToTodoListProps = function mapDispatchToTodoListProps(dispatch) {
return {
onTodoClick: function onTodoClick(id) {
return dispatch({
type: 'TOGGLE_TODO',
id: id
});
}
};
};
// Thay container bang connect cua redux
var VisiableTodoList = connect(mapStateToTodoListProps, mapDispatchToTodoListProps)(TodoList);
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 () {
if (input.value == '') return;
dispatch(addTodo(input.value));
input.value = '';
}
},
'Add Todo'
)
);
};
AddTodo = connect(function (state) {
return {};
}, function (dispatch) {
return { dispatch: dispatch };
})(AddTodo);
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 TodoApp = function TodoApp() {
return React.createElement(
'div',
null,
React.createElement(AddTodo, null),
React.createElement(VisiableTodoList, null),
React.createElement(Footer, null)
);
};
var _Redux2 = Redux;
var createStore = _Redux2.createStore;
var _ReactRedux2 = ReactRedux;
var Provider = _ReactRedux2.Provider;
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 'TOGGLE_TODO':
if (state.id !== action.id)
return state;
return {
...state,
completed: !state.completed
};
case 'ADD_TODO':
return action.payload;
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;
}
}
let nextTodoId = 0;
const addTodo = (text) => {
return {
type: 'ADD_TODO',
payload: {
id: nextTodoId ++,
text: text,
completed: false
}
}
}
const { combineReducers } = Redux
const todoApp = combineReducers({
todos,
visibilityFilter
});
const { Component } = React;
const { connect } = ReactRedux;
const Style = {
complete: {
textDecoration: 'line-through'
}
};
const Link = ({
children,
active,
onClick
}) => {
if (active) {
return <span>{children}</span>
}
return (
<a href="#"
onClick={onClick}
>
{children}
</a>
);
};
const mapStateToFilterLinkProps = (
state,
ownProps
) => {
return {
active: ownProps.filter === state.visibilityFilter
};
};
const mapDispatchToFilterLinkProps = (
dispatch,
ownProps
) => {
return {
onClick: () => {
dispatch({
type: 'SET_VISIBILITY_FILTER',
filter: ownProps.filter
})
}
};
};
const FilterLink = connect(
mapStateToFilterLinkProps,
mapDispatchToFilterLinkProps
)(Link);
const Todo = ({
id,
onClick,
completed,
text
}) => {
return <li
onClick={ onClick }
style={ completed ? Style.complete : {} }
>
{text}
</li>
};
const getVisiableTodos = (
todos,
visibilityFilter
) => {
switch (visibilityFilter) {
case 'SHOW_ALL':
return todos;
case 'SHOW_ACTIVE':
return todos.filter(
t => !t.completed
);
case 'SHOW_COMPLETED':
return todos.filter(
t => t.completed
);
}
};
const TodoList = ({
todos,
onTodoClick
}) => {
return <ul>
{todos.map(todo =>
<Todo
key={ todo.id }
{...todo}
onClick={() => onTodoClick(todo.id)}
/>
)}
</ul>
}
const mapStateToTodoListProps = (state) => {
return {
todos: getVisiableTodos(
state.todos,
state.visibilityFilter
)
};
};
const mapDispatchToTodoListProps = (dispatch) => {
return {
onTodoClick: (id) => dispatch({
type: 'TOGGLE_TODO',
id
})
};
};
// Thay container bang connect cua redux
const VisiableTodoList = connect(
mapStateToTodoListProps,
mapDispatchToTodoListProps
)(TodoList);
let AddTodo = ({ dispatch }) => {
let input;
return <div>
<input
ref={node => {
input = node;
}}
/>
<button
onClick={() => {
if (input.value == '') return;
dispatch(addTodo(input.value));
input.value = '';
}}
>
Add Todo
</button>
</div>
};
AddTodo = connect(
state => {
return {}
},
dispatch => {
return {dispatch}
}
)(AddTodo);
const Footer = () => {
return <p>
Show:
{' '}
<FilterLink filter="SHOW_ALL" >
All
</FilterLink>
{' '}
<FilterLink filter="SHOW_ACTIVE" >
Active
</FilterLink>
{' '}
<FilterLink filter="SHOW_COMPLETED" >
Completed
</FilterLink>
</p>
};
const TodoApp = () => (
<div>
<AddTodo />
<VisiableTodoList />
<Footer />
</div>
);
const { createStore } = Redux;
const { Provider } = ReactRedux;
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 'TOGGLE_TODO':
if (state.id !== action.id) return state;
return _extends({}, state, {
completed: !state.completed
});
case 'ADD_TODO':
return action.payload;
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 nextTodoId = 0;
var addTodo = function addTodo(text) {
return {
type: 'ADD_TODO',
payload: {
id: nextTodoId++,
text: text,
completed: false
}
};
};
var _Redux = Redux;
var combineReducers = _Redux.combineReducers;
var todoApp = combineReducers({
todos: todos,
visibilityFilter: visibilityFilter
});
var _React = React;
var Component = _React.Component;
var _ReactRedux = ReactRedux;
var connect = _ReactRedux.connect;
var Style = {
complete: {
textDecoration: 'line-through'
}
};
var Link = function Link(_ref) {
var children = _ref.children;
var active = _ref.active;
var onClick = _ref.onClick;
if (active) {
return React.createElement(
'span',
null,
children
);
}
return React.createElement(
'a',
{ href: '#',
onClick: onClick
},
children
);
};
var mapStateToFilterLinkProps = function mapStateToFilterLinkProps(state, ownProps) {
return {
active: ownProps.filter === state.visibilityFilter
};
};
var mapDispatchToFilterLinkProps = function mapDispatchToFilterLinkProps(dispatch, ownProps) {
return {
onClick: function onClick() {
dispatch({
type: 'SET_VISIBILITY_FILTER',
filter: ownProps.filter
});
}
};
};
var FilterLink = connect(mapStateToFilterLinkProps, mapDispatchToFilterLinkProps)(Link);
var Todo = function Todo(_ref2) {
var id = _ref2.id;
var onClick = _ref2.onClick;
var completed = _ref2.completed;
var text = _ref2.text;
return React.createElement(
'li',
{
onClick: onClick,
style: completed ? Style.complete : {}
},
text
);
};
var getVisiableTodos = function getVisiableTodos(todos, visibilityFilter) {
switch (visibilityFilter) {
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;
});
}
};
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);
}
}));
})
);
};
var mapStateToTodoListProps = function mapStateToTodoListProps(state) {
return {
todos: getVisiableTodos(state.todos, state.visibilityFilter)
};
};
var mapDispatchToTodoListProps = function mapDispatchToTodoListProps(dispatch) {
return {
onTodoClick: function onTodoClick(id) {
return dispatch({
type: 'TOGGLE_TODO',
id: id
});
}
};
};
// Thay container bang connect cua redux
var VisiableTodoList = connect(mapStateToTodoListProps, mapDispatchToTodoListProps)(TodoList);
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 () {
if (input.value == '') return;
dispatch(addTodo(input.value));
input.value = '';
}
},
'Add Todo'
)
);
};
AddTodo = connect(function (state) {
return {};
}, function (dispatch) {
return { dispatch: dispatch };
})(AddTodo);
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 TodoApp = function TodoApp() {
return React.createElement(
'div',
null,
React.createElement(AddTodo, null),
React.createElement(VisiableTodoList, null),
React.createElement(Footer, null)
);
};
var _Redux2 = Redux;
var createStore = _Redux2.createStore;
var _ReactRedux2 = ReactRedux;
var Provider = _ReactRedux2.Provider;
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