Created
August 16, 2017 13:06
-
-
Save AbhayShiro/72368cbb711e330b06435b3186035702 to your computer and use it in GitHub Desktop.
// source https://jsbin.com
This file contains 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 charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<div id='app'></div> | |
<script src="https://unpkg.com/react@0.14.7/dist/react.js"></script> | |
<script src="https://unpkg.com/react-dom@0.14.7/dist/react-dom.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.6.0/redux.min.js"></script> | |
<script src="https://npmcdn.com/expect/umd/expect.min.js"></script> | |
<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); } } | |
function todoSlaveReducer(state, action) { | |
switch (action.type) { | |
case 'ADD_TODO': | |
return { | |
id: action.todo.id, | |
text: action.todo.text, | |
completed: false | |
}; | |
case 'TOGGLE_TODO': | |
if (state.id !== action.id) { | |
return state; | |
} | |
return _extends({}, state, { | |
completed: !state.completed | |
}); | |
default: | |
return state; | |
} | |
}; | |
function todoReducer(state, action) { | |
if (state === undefined) state = []; | |
// console.log(action); console.log(state); | |
switch (action.type) { | |
case 'ADD_TODO': | |
console.log("sending"); | |
return [].concat(_toConsumableArray(state), [todoSlaveReducer(undefined, action)]); | |
case 'TOGGLE_TODO': | |
console.log('I am invoked for ' + action.id); | |
if (action.id != null) { | |
return state.map(function (todo) { | |
return todoSlaveReducer(todo, action); | |
}); | |
} else { | |
return state; | |
} | |
default: | |
return state; | |
} | |
} | |
function addTodo(todo) { | |
return { | |
type: 'ADD_TODO', | |
todo: todo | |
}; | |
} | |
function toggleTodo(id) { | |
return { | |
type: 'TOGGLE_TODO', | |
id: id | |
}; | |
} | |
var _Redux = Redux; | |
var createStore = _Redux.createStore; | |
var store = createStore(todoReducer); | |
var testToggle = function testToggle() { | |
var todoBefore = { | |
id: 0, | |
text: 'Learn Redux' | |
}; | |
var todoAfter = { | |
id: 1, | |
text: 'Learn React' | |
}; | |
Object.freeze(todoAfter); | |
Object.freeze(todoBefore); | |
store.dispatch(addTodo(todoBefore)); | |
store.dispatch(addTodo(todoAfter)); | |
store.dispatch(addTodo({ | |
id: 3, | |
text: 'Learn Express' | |
})); | |
store.dispatch(addTodo({ | |
id: 4, | |
text: 'Learn Node' | |
})); | |
store.dispatch(toggleTodo(1)); | |
console.log(store.getState()); | |
}; | |
testToggle(); | |
console.log('All tests passed'); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function todoSlaveReducer(state, action){ | |
switch(action.type){ | |
case 'ADD_TODO': | |
return { | |
id: action.todo.id, | |
text: action.todo.text, | |
completed: false | |
}; | |
case 'TOGGLE_TODO': | |
if(state.id !== action.id){ | |
return state; | |
} | |
return { | |
...state, | |
completed: !state.completed | |
}; | |
default: | |
return state; | |
} | |
}; | |
function todoReducer(state = [], action){ | |
// console.log(action); console.log(state); | |
switch (action.type) { | |
case 'ADD_TODO': | |
console.log("sending"); | |
return [...state,todoSlaveReducer(undefined, action) ]; | |
case 'TOGGLE_TODO': | |
console.log('I am invoked for '+action.id); | |
if(action.id != null){ | |
return state.map(todo => todoSlaveReducer(todo, action)) | |
}else{ | |
return state; | |
} | |
default: | |
return state; | |
} | |
} | |
function addTodo(todo) { | |
return { | |
type: 'ADD_TODO', | |
todo | |
} | |
} | |
function toggleTodo(id) { | |
return { | |
type: 'TOGGLE_TODO', | |
id | |
} | |
} | |
const { | |
createStore | |
} = Redux; | |
const store = createStore(todoReducer); | |
const testToggle = () => { | |
const todoBefore = { | |
id: 0, | |
text: 'Learn Redux' | |
}; | |
const todoAfter = { | |
id: 1, | |
text: 'Learn React' | |
} | |
Object.freeze(todoAfter); | |
Object.freeze(todoBefore); | |
store.dispatch(addTodo(todoBefore)); | |
store.dispatch(addTodo(todoAfter)); | |
store.dispatch(addTodo({ | |
id: 3, | |
text: 'Learn Express' | |
})); | |
store.dispatch(addTodo({ | |
id: 4, | |
text: 'Learn Node' | |
})); | |
store.dispatch(toggleTodo(1)); | |
console.log(store.getState()); | |
} | |
testToggle(); | |
console.log('All tests passed');</script></body> | |
</html> |
This file contains 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'; | |
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); } } | |
function todoSlaveReducer(state, action) { | |
switch (action.type) { | |
case 'ADD_TODO': | |
return { | |
id: action.todo.id, | |
text: action.todo.text, | |
completed: false | |
}; | |
case 'TOGGLE_TODO': | |
if (state.id !== action.id) { | |
return state; | |
} | |
return _extends({}, state, { | |
completed: !state.completed | |
}); | |
default: | |
return state; | |
} | |
}; | |
function todoReducer(state, action) { | |
if (state === undefined) state = []; | |
// console.log(action); console.log(state); | |
switch (action.type) { | |
case 'ADD_TODO': | |
console.log("sending"); | |
return [].concat(_toConsumableArray(state), [todoSlaveReducer(undefined, action)]); | |
case 'TOGGLE_TODO': | |
console.log('I am invoked for ' + action.id); | |
if (action.id != null) { | |
return state.map(function (todo) { | |
return todoSlaveReducer(todo, action); | |
}); | |
} else { | |
return state; | |
} | |
default: | |
return state; | |
} | |
} | |
function addTodo(todo) { | |
return { | |
type: 'ADD_TODO', | |
todo: todo | |
}; | |
} | |
function toggleTodo(id) { | |
return { | |
type: 'TOGGLE_TODO', | |
id: id | |
}; | |
} | |
var _Redux = Redux; | |
var createStore = _Redux.createStore; | |
var store = createStore(todoReducer); | |
var testToggle = function testToggle() { | |
var todoBefore = { | |
id: 0, | |
text: 'Learn Redux' | |
}; | |
var todoAfter = { | |
id: 1, | |
text: 'Learn React' | |
}; | |
Object.freeze(todoAfter); | |
Object.freeze(todoBefore); | |
store.dispatch(addTodo(todoBefore)); | |
store.dispatch(addTodo(todoAfter)); | |
store.dispatch(addTodo({ | |
id: 3, | |
text: 'Learn Express' | |
})); | |
store.dispatch(addTodo({ | |
id: 4, | |
text: 'Learn Node' | |
})); | |
store.dispatch(toggleTodo(1)); | |
console.log(store.getState()); | |
}; | |
testToggle(); | |
console.log('All tests passed'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment