Skip to content

Instantly share code, notes, and snippets.

@yashikagarg13
Last active March 18, 2017 10:14
Show Gist options
  • Save yashikagarg13/87dd3ac4faee71466f2ceb0186f59013 to your computer and use it in GitHub Desktop.
Save yashikagarg13/87dd3ac4faee71466f2ceb0186f59013 to your computer and use it in GitHub Desktop.
TODO App - Store source https://jsbin.com/keqexaj
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://npmcdn.com/expect/umd/expect.min.js"></script>
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.6.0/redux.js"></script>
</head>
<body>
<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 todoReducer = function todoReducer(state, action) {
if (state === undefined) state = {};
switch (action.type) {
case "ADD_TODO":
return _extends({}, action.todo, { completed: false });
case "TOGGLE_TODO":
if (state.id !== action.id) {
return state;
}
return _extends({}, state, {
completed: !state.completed
});
default:
return state;
}
};
var todosReducer = function todosReducer(state, action) {
if (state === undefined) state = [];
switch (action.type) {
case "ADD_TODO":
return [].concat(_toConsumableArray(state), [todoReducer(undefined, action)]);
case "TOGGLE_TODO":
return state.map(function (todo) {
return todoReducer(todo, action);
});
default:
return state;
}
};
var visbilityFilterReducer = function visbilityFilterReducer(state, action) {
if (state === undefined) state = "SHOW_ALL";
switch (action.type) {
case "SET_VISIBILITY_FILTER":
return action.filter;
default:
return state;
}
};
var todoAppReducer = function todoAppReducer(state, action) {
if (state === undefined) state = {};
return {
todos: todosReducer(state.todos, action),
visibilityFilter: visbilityFilterReducer(state.visibilityFilter, action)
};
};
var _Redux = Redux;
var createStore = _Redux.createStore;
var store = createStore(todoAppReducer);
console.log("/************** INITIAL STATE **************/");
console.log(store.getState());
console.log("/*******************************************/");
console.log("/********** DISPATCHING ADD TODO ***********/");
store.dispatch({ type: "ADD_TODO", todo: { id: 1, text: "Exercise" } });
store.dispatch({ type: "ADD_TODO", todo: { id: 2, text: "Learn" } });
store.dispatch({ type: "ADD_TODO", todo: { id: 3, text: "Dance" } });
store.dispatch({ type: "ADD_TODO", todo: { id: 4, text: "Cook" } });
store.dispatch({ type: "ADD_TODO", todo: { id: 5, text: "Eat" } });
console.log(store.getState());
console.log("/*******************************************/");
console.log("/******** DISPATCHING TOGGLE TODO ***********/");
store.dispatch({ type: "TOGGLE_TODO", id: 2 });
console.log(store.getState());
console.log("/*******************************************/");
console.log("/******** DISPATCHING SET VISIBILITY FILTER ***********/");
store.dispatch({ type: "SET_VISIBILITY_FILTER", filter: "SHOW_COMPLETED" });
console.log(store.getState());
console.log("/*******************************************/");
/*const testAddTodo = () => {
const stateBefore = [];
const action = {
todo: {
id: 1,
text: "item 1",
},
type: "ADD_TODO",
};
const stateAfter = [{
id: 1,
text: "item 1",
completed: false,
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todosReducer(stateBefore, action)).toEqual(stateAfter);
}
const testToggleTodo = () => {
const stateBefore = [{
id: 1,
text: "item 1",
completed: false,
}, {
id: 2,
text: "item 2",
completed: false,
}];
const action = {
id: 1,
type: "TOGGLE_TODO",
};
const stateAfter = [{
id: 1,
text: "item 1",
completed: true,
}, {
id: 2,
text: "item 2",
completed: false,
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todosReducer(stateBefore, action)).toEqual(stateAfter);
}
testAddTodo();
testToggleTodo();
console.log("Tests Passed!!");*/
</script>
<script id="jsbin-source-javascript" type="text/javascript">const todoReducer = (state = {}, action) => {
switch(action.type) {
case "ADD_TODO":
return {...action.todo, completed: false};
case "TOGGLE_TODO":
if (state.id !== action.id) {
return state;
}
return {
...state,
completed: !state.completed
}
default:
return state;
}
}
const todosReducer = (state = [], action) => {
switch(action.type) {
case "ADD_TODO":
return [...state, todoReducer(undefined, action)];
case "TOGGLE_TODO":
return state.map(todo => todoReducer(todo, action));
default:
return state;
}
}
const visbilityFilterReducer = (state = "SHOW_ALL", action) => {
switch(action.type) {
case "SET_VISIBILITY_FILTER":
return action.filter;
default:
return state;
}
}
const todoAppReducer = (state = {}, action) => {
return {
todos: todosReducer(state.todos, action),
visibilityFilter: visbilityFilterReducer(state.visibilityFilter, action)
};
}
const {createStore} = Redux;
const store = createStore(todoAppReducer);
console.log("/************** INITIAL STATE **************/");
console.log(store.getState());
console.log("/*******************************************/");
console.log("/********** DISPATCHING ADD TODO ***********/");
store.dispatch({type: "ADD_TODO", todo: {id: 1, text: "Exercise"}});
store.dispatch({type: "ADD_TODO", todo: {id: 2, text: "Learn"}});
store.dispatch({type: "ADD_TODO", todo: {id: 3, text: "Dance"}});
store.dispatch({type: "ADD_TODO", todo: {id: 4, text: "Cook"}});
store.dispatch({type: "ADD_TODO", todo: {id: 5, text: "Eat"}});
console.log(store.getState());
console.log("/*******************************************/");
console.log("/******** DISPATCHING TOGGLE TODO ***********/");
store.dispatch({type: "TOGGLE_TODO", id: 2});
console.log(store.getState());
console.log("/*******************************************/");
console.log("/******** DISPATCHING SET VISIBILITY FILTER ***********/");
store.dispatch({type: "SET_VISIBILITY_FILTER", filter: "SHOW_COMPLETED"});
console.log(store.getState());
console.log("/*******************************************/");
/*const testAddTodo = () => {
const stateBefore = [];
const action = {
todo: {
id: 1,
text: "item 1",
},
type: "ADD_TODO",
};
const stateAfter = [{
id: 1,
text: "item 1",
completed: false,
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todosReducer(stateBefore, action)).toEqual(stateAfter);
}
const testToggleTodo = () => {
const stateBefore = [{
id: 1,
text: "item 1",
completed: false,
}, {
id: 2,
text: "item 2",
completed: false,
}];
const action = {
id: 1,
type: "TOGGLE_TODO",
};
const stateAfter = [{
id: 1,
text: "item 1",
completed: true,
}, {
id: 2,
text: "item 2",
completed: false,
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todosReducer(stateBefore, action)).toEqual(stateAfter);
}
testAddTodo();
testToggleTodo();
console.log("Tests Passed!!");*/</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 todoReducer = function todoReducer(state, action) {
if (state === undefined) state = {};
switch (action.type) {
case "ADD_TODO":
return _extends({}, action.todo, { completed: false });
case "TOGGLE_TODO":
if (state.id !== action.id) {
return state;
}
return _extends({}, state, {
completed: !state.completed
});
default:
return state;
}
};
var todosReducer = function todosReducer(state, action) {
if (state === undefined) state = [];
switch (action.type) {
case "ADD_TODO":
return [].concat(_toConsumableArray(state), [todoReducer(undefined, action)]);
case "TOGGLE_TODO":
return state.map(function (todo) {
return todoReducer(todo, action);
});
default:
return state;
}
};
var visbilityFilterReducer = function visbilityFilterReducer(state, action) {
if (state === undefined) state = "SHOW_ALL";
switch (action.type) {
case "SET_VISIBILITY_FILTER":
return action.filter;
default:
return state;
}
};
var todoAppReducer = function todoAppReducer(state, action) {
if (state === undefined) state = {};
return {
todos: todosReducer(state.todos, action),
visibilityFilter: visbilityFilterReducer(state.visibilityFilter, action)
};
};
var _Redux = Redux;
var createStore = _Redux.createStore;
var store = createStore(todoAppReducer);
console.log("/************** INITIAL STATE **************/");
console.log(store.getState());
console.log("/*******************************************/");
console.log("/********** DISPATCHING ADD TODO ***********/");
store.dispatch({ type: "ADD_TODO", todo: { id: 1, text: "Exercise" } });
store.dispatch({ type: "ADD_TODO", todo: { id: 2, text: "Learn" } });
store.dispatch({ type: "ADD_TODO", todo: { id: 3, text: "Dance" } });
store.dispatch({ type: "ADD_TODO", todo: { id: 4, text: "Cook" } });
store.dispatch({ type: "ADD_TODO", todo: { id: 5, text: "Eat" } });
console.log(store.getState());
console.log("/*******************************************/");
console.log("/******** DISPATCHING TOGGLE TODO ***********/");
store.dispatch({ type: "TOGGLE_TODO", id: 2 });
console.log(store.getState());
console.log("/*******************************************/");
console.log("/******** DISPATCHING SET VISIBILITY FILTER ***********/");
store.dispatch({ type: "SET_VISIBILITY_FILTER", filter: "SHOW_COMPLETED" });
console.log(store.getState());
console.log("/*******************************************/");
/*const testAddTodo = () => {
const stateBefore = [];
const action = {
todo: {
id: 1,
text: "item 1",
},
type: "ADD_TODO",
};
const stateAfter = [{
id: 1,
text: "item 1",
completed: false,
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todosReducer(stateBefore, action)).toEqual(stateAfter);
}
const testToggleTodo = () => {
const stateBefore = [{
id: 1,
text: "item 1",
completed: false,
}, {
id: 2,
text: "item 2",
completed: false,
}];
const action = {
id: 1,
type: "TOGGLE_TODO",
};
const stateAfter = [{
id: 1,
text: "item 1",
completed: true,
}, {
id: 2,
text: "item 2",
completed: false,
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todosReducer(stateBefore, action)).toEqual(stateAfter);
}
testAddTodo();
testToggleTodo();
console.log("Tests Passed!!");*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment