-
-
Save pavelnikolov/f40605c29dd201fecd92 to your computer and use it in GitHub Desktop.
JS Bin React + Redux todo app // source http://jsbin.com/qejizi
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="React + Redux todo app"> | |
<script src="http://fb.me/react-0.14.3.js"></script> | |
<script src="http://fb.me/react-dom-0.14.3.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/redux/3.2.1/redux.js"></script> | |
<script src="https://wzrd.in/standalone/expect@latest"></script> | |
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<style> | |
font: sans-serif | |
</style> | |
</head> | |
<body> | |
<div id="root"></div> | |
<script id="jsbin-javascript"> | |
'use strict'; | |
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, | |
completed: false | |
}; | |
case 'TOGGLE_TODO': | |
if (state.id !== action.id) { | |
return state; | |
} | |
return Object.assign({}, 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 createStore = _Redux.createStore; | |
var todoApp = combineReducers({ | |
todos: todos, | |
visibilityFilter: visibilityFilter | |
}); | |
var store = createStore(todoApp); | |
var _React = React; | |
var Component = _React.Component; | |
var FilterLink = function FilterLink(_ref) { | |
var filter = _ref.filter; | |
var currentFilter = _ref.currentFilter; | |
var children = _ref.children; | |
if (filter !== currentFilter) { | |
return React.createElement( | |
'a', | |
{ href: '#', | |
onClick: function (e) { | |
e.preventDefault(); | |
store.dispatch({ | |
type: 'SET_VISIBILITY_FILTER', | |
filter: filter | |
}); | |
} | |
}, | |
children | |
); | |
} else { | |
return React.createElement( | |
'span', | |
null, | |
children | |
); | |
} | |
}; | |
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; | |
}); | |
default: | |
return todos; | |
} | |
}; | |
var nextTodoID = 0; | |
var TodoApp = function TodoApp(_ref2) { | |
var todos = _ref2.todos; | |
var visibilityFilter = _ref2.visibilityFilter; | |
var input = null; | |
var visibleTodos = getVisibleTodos(todos, visibilityFilter); | |
return React.createElement( | |
'div', | |
null, | |
React.createElement('input', { ref: function (node) { | |
input = node; | |
} }), | |
React.createElement( | |
'button', | |
{ | |
onClick: function () { | |
store.dispatch({ | |
type: 'ADD_TODO', | |
text: input.value, | |
id: nextTodoID++ | |
}); | |
input.value = ''; | |
} }, | |
'Add todo' | |
), | |
React.createElement( | |
'ul', | |
null, | |
visibleTodos.map(function (t) { | |
return React.createElement( | |
'li', | |
{ | |
key: t.id, | |
onClick: function () { | |
store.dispatch({ | |
type: 'TOGGLE_TODO', | |
id: t.id | |
}); | |
}, | |
style: { | |
textDecoration: t.completed ? 'line-through' : 'none' | |
} | |
}, | |
t.text | |
); | |
}) | |
), | |
React.createElement( | |
'p', | |
null, | |
'Show:', | |
' ', | |
React.createElement( | |
FilterLink, | |
{ | |
filter: 'SHOW_ALL', | |
currentFilter: visibilityFilter | |
}, | |
'All' | |
), | |
' ', | |
React.createElement( | |
FilterLink, | |
{ | |
filter: 'SHOW_ACTIVE', | |
currentFilter: visibilityFilter | |
}, | |
'Active' | |
), | |
' ', | |
React.createElement( | |
FilterLink, | |
{ | |
filter: 'SHOW_COMPLETED', | |
currentFilter: visibilityFilter | |
}, | |
'Completed' | |
) | |
) | |
); | |
}; | |
var render = function render() { | |
//console.log(store.getState()); | |
ReactDOM.render(React.createElement(TodoApp, store.getState()), document.getElementById('root')); | |
}; | |
store.subscribe(render); | |
render(); | |
//console.log(store.getState()); | |
//store.dispatch({type: 'SET_VISIBILITY_FILTER', filter: 'SHOW_COMPLETED'}); | |
//console.log(store.getState()); | |
//store.dispatch({type: 'ADD_TODO', id: 0, text: 'Go shopping'}); | |
//console.log(store.getState()); | |
//store.dispatch({type: 'ADD_TODO', id: 1, text: 'Do something else'}); | |
//console.log(store.getState()); | |
//store.dispatch({type: 'SET_VISIBILITY_FILTER', filter: 'SHOW_ALL'}); | |
//console.log(store.getState()); | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="React + Redux todo app"> | |
<script src="//fb.me/react-0.14.3.js"><\/script> | |
<script src="//fb.me/react-dom-0.14.3.js"><\/script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/redux/3.2.1/redux.js"><\/script> | |
<script src="https://wzrd.in/standalone/expect@latest"><\/script> | |
<script src="https://wzrd.in/standalone/deep-freeze@latest"><\/script> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<style> | |
font: sans-serif | |
</style> | |
</head> | |
<body> | |
<div id="root"></div> | |
</body> | |
</html></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, | |
completed: false | |
}; | |
case 'TOGGLE_TODO': | |
if (state.id !== action.id) { | |
return state; | |
} | |
return Object.assign({}, 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, createStore } = Redux; | |
const todoApp = combineReducers({ | |
todos, | |
visibilityFilter | |
}); | |
const store = createStore(todoApp); | |
const { Component } = React; | |
const FilterLink = ({filter, currentFilter, children}) => { | |
if (filter !== currentFilter) { | |
return ( | |
<a href='#' | |
onClick={e => { | |
e.preventDefault(); | |
store.dispatch({ | |
type: 'SET_VISIBILITY_FILTER', | |
filter: filter | |
}); | |
}} | |
> | |
{children} | |
</a>) | |
} else { | |
return ( | |
<span>{children}</span> | |
); | |
} | |
}; | |
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); | |
default: | |
return todos; | |
} | |
}; | |
let nextTodoID = 0; | |
const TodoApp = ({todos, visibilityFilter}) => { | |
let input = null; | |
let visibleTodos = getVisibleTodos(todos, visibilityFilter); | |
return ( | |
<div> | |
<input ref={ node => { input = node; }} /> | |
<button | |
onClick={() => { | |
store.dispatch({ | |
type: 'ADD_TODO', | |
text: input.value, | |
id: nextTodoID++ | |
}); | |
input.value = ''; | |
}}> | |
Add todo | |
</button> | |
<ul> | |
{visibleTodos.map(t => | |
<li | |
key={t.id} | |
onClick={ () => { | |
store.dispatch({ | |
type: 'TOGGLE_TODO', | |
id: t.id | |
}); | |
}} | |
style={{ | |
textDecoration: t.completed ? 'line-through': 'none' | |
}} | |
> | |
{t.text} | |
</li> | |
)} | |
</ul> | |
<p> | |
Show: | |
{' '} | |
<FilterLink | |
filter='SHOW_ALL' | |
currentFilter={visibilityFilter} | |
> | |
All | |
</FilterLink> | |
{' '} | |
<FilterLink | |
filter='SHOW_ACTIVE' | |
currentFilter={visibilityFilter} | |
> | |
Active | |
</FilterLink> | |
{' '} | |
<FilterLink | |
filter='SHOW_COMPLETED' | |
currentFilter={visibilityFilter} | |
> | |
Completed | |
</FilterLink> | |
</p> | |
</div> | |
); | |
}; | |
const render = () => { | |
//console.log(store.getState()); | |
ReactDOM.render( | |
<TodoApp | |
{...store.getState()} | |
/>, | |
document.getElementById('root') | |
); | |
}; | |
store.subscribe(render); | |
render(); | |
//console.log(store.getState()); | |
//store.dispatch({type: 'SET_VISIBILITY_FILTER', filter: 'SHOW_COMPLETED'}); | |
//console.log(store.getState()); | |
//store.dispatch({type: 'ADD_TODO', id: 0, text: 'Go shopping'}); | |
//console.log(store.getState()); | |
//store.dispatch({type: 'ADD_TODO', id: 1, text: 'Do something else'}); | |
//console.log(store.getState()); | |
//store.dispatch({type: 'SET_VISIBILITY_FILTER', filter: 'SHOW_ALL'}); | |
//console.log(store.getState()); | |
</script></body> | |
</html> |
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
'use strict'; | |
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, | |
completed: false | |
}; | |
case 'TOGGLE_TODO': | |
if (state.id !== action.id) { | |
return state; | |
} | |
return Object.assign({}, 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 createStore = _Redux.createStore; | |
var todoApp = combineReducers({ | |
todos: todos, | |
visibilityFilter: visibilityFilter | |
}); | |
var store = createStore(todoApp); | |
var _React = React; | |
var Component = _React.Component; | |
var FilterLink = function FilterLink(_ref) { | |
var filter = _ref.filter; | |
var currentFilter = _ref.currentFilter; | |
var children = _ref.children; | |
if (filter !== currentFilter) { | |
return React.createElement( | |
'a', | |
{ href: '#', | |
onClick: function (e) { | |
e.preventDefault(); | |
store.dispatch({ | |
type: 'SET_VISIBILITY_FILTER', | |
filter: filter | |
}); | |
} | |
}, | |
children | |
); | |
} else { | |
return React.createElement( | |
'span', | |
null, | |
children | |
); | |
} | |
}; | |
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; | |
}); | |
default: | |
return todos; | |
} | |
}; | |
var nextTodoID = 0; | |
var TodoApp = function TodoApp(_ref2) { | |
var todos = _ref2.todos; | |
var visibilityFilter = _ref2.visibilityFilter; | |
var input = null; | |
var visibleTodos = getVisibleTodos(todos, visibilityFilter); | |
return React.createElement( | |
'div', | |
null, | |
React.createElement('input', { ref: function (node) { | |
input = node; | |
} }), | |
React.createElement( | |
'button', | |
{ | |
onClick: function () { | |
store.dispatch({ | |
type: 'ADD_TODO', | |
text: input.value, | |
id: nextTodoID++ | |
}); | |
input.value = ''; | |
} }, | |
'Add todo' | |
), | |
React.createElement( | |
'ul', | |
null, | |
visibleTodos.map(function (t) { | |
return React.createElement( | |
'li', | |
{ | |
key: t.id, | |
onClick: function () { | |
store.dispatch({ | |
type: 'TOGGLE_TODO', | |
id: t.id | |
}); | |
}, | |
style: { | |
textDecoration: t.completed ? 'line-through' : 'none' | |
} | |
}, | |
t.text | |
); | |
}) | |
), | |
React.createElement( | |
'p', | |
null, | |
'Show:', | |
' ', | |
React.createElement( | |
FilterLink, | |
{ | |
filter: 'SHOW_ALL', | |
currentFilter: visibilityFilter | |
}, | |
'All' | |
), | |
' ', | |
React.createElement( | |
FilterLink, | |
{ | |
filter: 'SHOW_ACTIVE', | |
currentFilter: visibilityFilter | |
}, | |
'Active' | |
), | |
' ', | |
React.createElement( | |
FilterLink, | |
{ | |
filter: 'SHOW_COMPLETED', | |
currentFilter: visibilityFilter | |
}, | |
'Completed' | |
) | |
) | |
); | |
}; | |
var render = function render() { | |
//console.log(store.getState()); | |
ReactDOM.render(React.createElement(TodoApp, store.getState()), document.getElementById('root')); | |
}; | |
store.subscribe(render); | |
render(); | |
//console.log(store.getState()); | |
//store.dispatch({type: 'SET_VISIBILITY_FILTER', filter: 'SHOW_COMPLETED'}); | |
//console.log(store.getState()); | |
//store.dispatch({type: 'ADD_TODO', id: 0, text: 'Go shopping'}); | |
//console.log(store.getState()); | |
//store.dispatch({type: 'ADD_TODO', id: 1, text: 'Do something else'}); | |
//console.log(store.getState()); | |
//store.dispatch({type: 'SET_VISIBILITY_FILTER', filter: 'SHOW_ALL'}); | |
//console.log(store.getState()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment