Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. const todo = (state, action) => {
  2. switch (action.type) {
  3. case 'ADD_TODO':
  4. return {
  5. id: action.id,
  6. text: action.text,
  7. completed: false
  8. };
  9. case 'TOGGLE_TODO':
  10. if (state.id !== action.id) {
  11. return state;
  12. }
  13.  
  14. return {
  15. ...state,
  16. completed: !state.completed
  17. };
  18. default:
  19. return state;
  20. }
  21. };
  22.  
  23. const todos = (state = [], action) => {
  24. switch (action.type) {
  25. case 'ADD_TODO':
  26. return [
  27. ...state,
  28. todo(undefined, action)
  29. ];
  30. case 'TOGGLE_TODO':
  31. return state.map(t =>
  32. todo(t, action)
  33. );
  34. default:
  35. return state;
  36. }
  37. };
  38.  
  39. const visibilityFilter = (
  40. state = 'SHOW_ALL',
  41. action
  42. ) => {
  43. switch (action.type) {
  44. case 'SET_VISIBILITY_FILTER':
  45. return action.filter;
  46. default:
  47. return state;
  48. }
  49. };
  50.  
  51. const { combineReducers } = Redux;
  52. const todoApp = combineReducers({
  53. todos,
  54. visibilityFilter
  55. });
  56.  
  57. let nextTodoId = 0;
  58. const addTodo = (text) => {
  59. return {
  60. type: 'ADD_TODO',
  61. id: nextTodoId++,
  62. text: text
  63. }
  64. }
  65. const setVisibilityFilter = (filter) => {
  66. return {
  67. type: 'SET_VISIBILITY_FILTER',
  68. filter: filter
  69. }
  70. }
  71. const toggleTodo = (id) => {
  72. return {
  73. type: 'TOGGLE_TODO',
  74. id
  75. }
  76. }
  77.  
  78. const { Component } = React;
  79. const { connect } = ReactRedux;
  80.  
  81. const Link = ({
  82. active,
  83. children,
  84. onClick
  85. }) => {
  86. if (active) {
  87. return <span>{children}</span>;
  88. }
  89.  
  90. return (
  91. <a href='#'
  92. onClick={e => {
  93. e.preventDefault();
  94. onClick();
  95. }}
  96. >
  97. {children}
  98. </a>
  99. );
  100. };
  101.  
  102. const mapStateToLinkProps = (state, ownProps) => {
  103. return {
  104. active: (ownProps.filter === state.visibilityFilter)
  105. }
  106. }
  107. const mapDispatchToLinkProps = (dispatch, ownProps) => {
  108. return {
  109. onClick: () => {
  110. dispatch(
  111. setVisibilityFilter(ownProps.filter)
  112. )
  113. }
  114. }
  115. }
  116. const FilterLink = connect(
  117. mapStateToLinkProps,
  118. mapDispatchToLinkProps
  119. )(Link)
  120.  
  121. const Footer = () => (
  122. <p>
  123. Show:
  124. {' '}
  125. <FilterLink filter='SHOW_ALL'>
  126. All
  127. </FilterLink>
  128. {', '}
  129. <FilterLink filter='SHOW_ACTIVE'>
  130. Active
  131. </FilterLink>
  132. {', '}
  133. <FilterLink filter='SHOW_COMPLETED'>
  134. Completed
  135. </FilterLink>
  136. </p>
  137. );
  138.  
  139. const Todo = ({
  140. onClick,
  141. completed,
  142. text
  143. }) => (
  144. <li
  145. onClick={onClick}
  146. style={{
  147. textDecoration:
  148. completed ?
  149. 'line-through' :
  150. 'none'
  151. }}
  152. >
  153. {text}
  154. </li>
  155. );
  156.  
  157. const TodoList = ({
  158. todos,
  159. onTodoClick
  160. }) => (
  161. <ul>
  162. {todos.map(todo =>
  163. <Todo
  164. key={todo.id}
  165. {...todo}
  166. onClick={() => onTodoClick(todo.id)}
  167. />
  168. )}
  169. </ul>
  170. );
  171.  
  172. let AddTodo = ({ dispatch }) => {
  173. let input;
  174.  
  175. return (
  176. <div>
  177. <input ref={node => {
  178. input = node;
  179. }} />
  180. <button onClick={() => {
  181. dispatch(addTodo(input.value))
  182. input.value = '';
  183. }}>
  184. Add Todo
  185. </button>
  186. </div>
  187. );
  188. };
  189. AddTodo = connect()(AddTodo)
  190.  
  191.  
  192. const getVisibleTodos = (
  193. todos,
  194. filter
  195. ) => {
  196. switch (filter) {
  197. case 'SHOW_ALL':
  198. return todos;
  199. case 'SHOW_COMPLETED':
  200. return todos.filter(
  201. t => t.completed
  202. );
  203. case 'SHOW_ACTIVE':
  204. return todos.filter(
  205. t => !t.completed
  206. );
  207. }
  208. }
  209. const mapStateToTodoListProps = (state) => {
  210. return {
  211. todos: getVisibleTodos(
  212. state.todos,
  213. state.visibilityFilter
  214. )
  215. }
  216. }
  217. const mapDispatchToTodoListProps = (dispatch) => {
  218. return {
  219. onTodoClick: (id) => {
  220. dispatch(
  221. toggleTodo(id)
  222. )
  223. }
  224. }
  225. }
  226. const VisibleTodoList = connect(
  227. mapStateToTodoListProps,
  228. mapDispatchToTodoListProps
  229. )(TodoList)
  230.  
  231.  
  232. const TodoApp = () => (
  233. <div>
  234. <AddTodo />
  235. <VisibleTodoList />
  236. <Footer />
  237. </div>
  238. );
  239.  
  240. const { Provider } = ReactRedux;
  241. const { createStore } = Redux;
  242.  
  243. ReactDOM.render(
  244. <Provider store={createStore(todoApp)}>
  245. <TodoApp />
  246. </Provider>,
  247. document.getElementById('root')
  248. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement