Skip to content

Instantly share code, notes, and snippets.

@giopunt
Created July 14, 2017 12:56
Show Gist options
  • Save giopunt/0ee5c0cec89488d73d7ca8c72d718011 to your computer and use it in GitHub Desktop.
Save giopunt/0ee5c0cec89488d73d7ca8c72d718011 to your computer and use it in GitHub Desktop.
REDUX TRANING - APPLY UI // source https://jsbin.com/xareso
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>REDUX TRANING - APPLY UI</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.4/redux.js"></script>
<script src="https://unpkg.com/react@0.14.0/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@0.14.0/dist/react-dom.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,
completed: 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 createStore = _Redux.createStore;
var combineReducers = _Redux.combineReducers;
var todoApp = combineReducers({
todos: todos,
visibilityFilter: visibilityFilter
});
var store = createStore(todoApp);
var nextTodoId = 0;
var FilterLink = function FilterLink(_ref) {
var filter = _ref.filter;
var currentFilter = _ref.currentFilter;
var children = _ref.children;
if (filter == currentFilter) {
return React.createElement(
"span",
null,
children
);
}
return React.createElement(
"a",
{ href: "#", onClick: function (e) {
e.preventDefault();
store.dispatch({
type: 'SET_VISIBILITY_FILTER',
filter: filter
});
} },
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;
});
}
};
var AddTodo = function AddTodo(_ref2) {
var onAddClick = _ref2.onAddClick;
var input = undefined;
return React.createElement(
"div",
null,
React.createElement("input", { ref: function (node) {
input = node;
} }),
React.createElement(
"button",
{ onClick: function () {
onAddClick(input.value);
input.value = '';
} },
"ADD TODO"
)
);
};
var Todo = function Todo(_ref3) {
var onClick = _ref3.onClick;
var completed = _ref3.completed;
var text = _ref3.text;
return React.createElement(
"li",
{ key: todo.id,
onClick: onClick,
style: {
textDecoration: completed ? 'line-through' : 'none'
} },
text
);
};
var TodoList = function TodoList(_ref4) {
var todos = _ref4.todos;
var onTodoClick = _ref4.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 Footer = function Footer(_ref5) {
var visibilityFilter = _ref5.visibilityFilter;
var onFilterClick = _ref5.onFilterClick;
return 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 TodoApp = function TodoApp(_ref6) {
var todos = _ref6.todos;
var visibilityFilter = _ref6.visibilityFilter;
return React.createElement(
"div",
null,
React.createElement(AddTodo, {
onAddClick: function (text) {
return store.dispatch({
type: 'ADD_TODO',
text: text,
id: nextTodoId++
});
} }),
React.createElement(TodoList, {
todos: getVisibleTodos(todos, visibilityFilter),
onTodoClick: function (id) {
store.dispatch({
type: 'TOGGLE_TODO',
id: id
});
}
}),
React.createElement(Footer, {
visibilityFilter: true
})
);
};
var render = function render() {
ReactDOM.render(React.createElement(TodoApp, store.getState()), document.getElementById('root'));
};
store.subscribe(render);
render();
</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 {
...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 { createStore, combineReducers } = Redux;
const todoApp = combineReducers({
todos,
visibilityFilter
});
const store = createStore(todoApp);
let nextTodoId = 0;
const FilterLink = ({
filter,
currentFilter,
children
}) => {
if(filter == currentFilter) {
return <span>{children}</span>
}
return(
<a href="#" onClick={e => {
e.preventDefault();
store.dispatch({
type: 'SET_VISIBILITY_FILTER',
filter
})
}}>
{children}
</a>
)};
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);
}
}
const AddTodo = ({
onAddClick
}) => {
let input;
return(
<div>
<input ref={node => {
input = node;
}} />
<button onClick={() => {
onAddClick(input.value)
input.value = '';
}}>
ADD TODO
</button>
</div>
)
};
const Todo = ({
onClick,
completed,
text
}) => (
<li key={todo.id}
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>
)
const Footer = ({
visibilityFilter,
onFilterClick
}) => {
return(
<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>
)
}
const TodoApp = ({
todos,
visibilityFilter
}) => (
<div>
<AddTodo
onAddClick={text =>
store.dispatch({
type: 'ADD_TODO',
text: text,
id: nextTodoId++
})
}/>
<TodoList
todos={
getVisibleTodos(
todos,
visibilityFilter
)
}
onTodoClick={id => {
store.dispatch({
type: 'TOGGLE_TODO',
id
});
}}
/>
<Footer
visibilityFilter
/>
</div>
);
const render = () => {
ReactDOM.render(
<TodoApp { ...store.getState() } />,
document.getElementById('root')
);
}
store.subscribe(render);
render();</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,
completed: 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 createStore = _Redux.createStore;
var combineReducers = _Redux.combineReducers;
var todoApp = combineReducers({
todos: todos,
visibilityFilter: visibilityFilter
});
var store = createStore(todoApp);
var nextTodoId = 0;
var FilterLink = function FilterLink(_ref) {
var filter = _ref.filter;
var currentFilter = _ref.currentFilter;
var children = _ref.children;
if (filter == currentFilter) {
return React.createElement(
"span",
null,
children
);
}
return React.createElement(
"a",
{ href: "#", onClick: function (e) {
e.preventDefault();
store.dispatch({
type: 'SET_VISIBILITY_FILTER',
filter: filter
});
} },
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;
});
}
};
var AddTodo = function AddTodo(_ref2) {
var onAddClick = _ref2.onAddClick;
var input = undefined;
return React.createElement(
"div",
null,
React.createElement("input", { ref: function (node) {
input = node;
} }),
React.createElement(
"button",
{ onClick: function () {
onAddClick(input.value);
input.value = '';
} },
"ADD TODO"
)
);
};
var Todo = function Todo(_ref3) {
var onClick = _ref3.onClick;
var completed = _ref3.completed;
var text = _ref3.text;
return React.createElement(
"li",
{ key: todo.id,
onClick: onClick,
style: {
textDecoration: completed ? 'line-through' : 'none'
} },
text
);
};
var TodoList = function TodoList(_ref4) {
var todos = _ref4.todos;
var onTodoClick = _ref4.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 Footer = function Footer(_ref5) {
var visibilityFilter = _ref5.visibilityFilter;
var onFilterClick = _ref5.onFilterClick;
return 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 TodoApp = function TodoApp(_ref6) {
var todos = _ref6.todos;
var visibilityFilter = _ref6.visibilityFilter;
return React.createElement(
"div",
null,
React.createElement(AddTodo, {
onAddClick: function (text) {
return store.dispatch({
type: 'ADD_TODO',
text: text,
id: nextTodoId++
});
} }),
React.createElement(TodoList, {
todos: getVisibleTodos(todos, visibilityFilter),
onTodoClick: function (id) {
store.dispatch({
type: 'TOGGLE_TODO',
id: id
});
}
}),
React.createElement(Footer, {
visibilityFilter: true
})
);
};
var render = function render() {
ReactDOM.render(React.createElement(TodoApp, store.getState()), document.getElementById('root'));
};
store.subscribe(render);
render();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment