- Notifications
You must be signed in to change notification settings - Fork60
Use immutable data structures#9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,88 +1,73 @@ | ||
| import Cycle from 'cyclejs'; | ||
| import {Map} from 'immutable'; | ||
Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Not sure if it's good practice to have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. there's no conflict, a module is a new scope: it's not overwritten | ||
| function getFilterFn(route) { | ||
| switch (route) { | ||
| case '/active': return (x =>!x.get('completed')); | ||
| case '/completed': return (x =>x.get('completed')); | ||
| default: return () => true; // allow anything | ||
| } | ||
| } | ||
| function determineFilter(todosData, route) { | ||
| return todosData | ||
| .set('filter', route.replace('/', '').trim()) | ||
| .set('filterFn', getFilterFn(route)); | ||
| } | ||
| function makeModification$(intent) { | ||
| let clearInputMod$ = intent.clearInput$.map(() => todosData => { | ||
| return todosData.set('input', ''); | ||
| }); | ||
| let insertTodoMod$ = intent.insertTodo$.map(todoTitle => todosData => { | ||
| letlist = todosData.get('list'); | ||
| let lastId =list.size ? list.last().get('id') : 0; | ||
| let todo = Map({ | ||
| id: lastId + 1, | ||
| title: todoTitle, | ||
| completed: false | ||
| }); | ||
| return todosData.update('list', list => list.push(todo).set('input', '')); | ||
| }); | ||
| let editTodoMod$ = intent.editTodo$.map(evdata => todosData => { | ||
| let todoIndex = todosData.get('list') | ||
| .findIndex(x => x.get('id') === evdata.id); | ||
| return todosData.update('list', list => | ||
| list.update(todoIndex, x => x.set('title', evdata.content)) | ||
| ); | ||
| }); | ||
| let toggleTodoMod$ = intent.toggleTodo$.map(todoId => todosData => { | ||
| let todoIndex = todosData.get('list') | ||
| .findIndex(x => x.get('id') === todoId); | ||
| return todosData.update('list', list => | ||
| list.update(todoIndex, x => x.set('completed', !x.get('completed'))) | ||
| ); | ||
| }); | ||
| let toggleAllMod$ = intent.toggleAll$.map(() => todosData => { | ||
| let state = todosData.get('list').some(x => !x.get('completed')); | ||
| return todosData.update('list', list => | ||
| list.map(x => x.set('completed', state)) | ||
| ); | ||
| }); | ||
| let deleteTodoMod$ = intent.deleteTodo$.map(todoId => todosData => { | ||
| returntodosData.update('list', list => | ||
| list.filter(x => x.get('id') !== todoId) | ||
| ); | ||
| }); | ||
| let deleteCompletedsMod$ = intent.deleteCompleteds$.map(() => todosData => { | ||
| returntodosData.update('list',list => | ||
| list.filter(x =>!x.get('completed')) | ||
| ); | ||
| }); | ||
| return Cycle.Rx.Observable.merge( | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,9 @@ | ||
| export default function localStorageSink(todosData) { | ||
| // Observe all todos data and save them to localStorage | ||
| let savedTodosData = { | ||
| list: todosData.get('list').filter(x => x ).toJS() | ||
Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Sometimes I got | ||
| }; | ||
| localStorage.setItem('todos-cycle', JSON.stringify(savedTodosData)) | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,7 +7,7 @@ function vrenderHeader(todosData) { | ||
| h('h1', 'todos'), | ||
| h('input#new-todo', { | ||
| type: 'text', | ||
| value: propHook(elem => elem.value = todosData.get('input')), | ||
| attributes: { | ||
| placeholder: 'What needs to be done?' | ||
| }, | ||
| @@ -18,16 +18,19 @@ function vrenderHeader(todosData) { | ||
| } | ||
| function vrenderMainSection(todosData) { | ||
| let list = todosData.get('list'); | ||
| let allCompleted = list.every(x => x.get('completed')); | ||
| return h('section#main', { | ||
| style: {'display': list.size ? '' : 'none'} | ||
| }, [ | ||
| h('input#toggle-all', { | ||
| type: 'checkbox', | ||
| checked: allCompleted | ||
| }), | ||
| h('ul#todo-list', list | ||
| .filter(todosData.get('filterFn')) | ||
| .toJS() | ||
Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I don't know if it is better to case to JS before the map or after. | ||
| .map(todoData => | ||
| h('todo-item.todo-item', { | ||
| key: todoData.id, | ||
| @@ -41,30 +44,31 @@ function vrenderMainSection(todosData) { | ||
| } | ||
| function vrenderFooter(todosData) { | ||
| let list = todosData.get('list'); | ||
| let filter = todosData.get('filter'); | ||
| let amountCompleted = list.count(x => x.get('completed')); | ||
| let amountActive = list.size - amountCompleted; | ||
| return h('footer#footer', { | ||
| style: {'display': list.size ? '' : 'none'} | ||
| }, [ | ||
| h('span#todo-count', [ | ||
| h('strong', String(amountActive)), | ||
| ' item' + (amountActive !== 1 ? 's' : '') + ' left' | ||
| ]), | ||
| h('ul#filters', [ | ||
| h('li', [ | ||
| h('a' + (filter === '' ? '.selected' : ''), { | ||
| attributes: {'href': '#/'} | ||
| }, 'All') | ||
| ]), | ||
| h('li', [ | ||
| h('a' + (filter === 'active' ? '.selected' : ''), { | ||
| attributes: {'href': '#/active'} | ||
| }, 'Active') | ||
| ]), | ||
| h('li', [ | ||
| h('a' + (filter === 'completed' ? '.selected' : ''), { | ||
| attributes: {'href': '#/completed'} | ||
| }, 'Completed') | ||
| ]) | ||