-
Notifications
You must be signed in to change notification settings - Fork 62
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use immutable data structures #9
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.list.map(todoData => | ||
({ | ||
title: todoData.title, | ||
completed: todoData.completed, | ||
id: todoData.id | ||
}) | ||
) | ||
list: todosData.get('list').filter(x => x ).toJS() | ||
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.input; }), | ||
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 allCompleted = todosData.list.reduce((x, y) => x && y.completed, true); | ||
let list = todosData.get('list'); | ||
let allCompleted = list.every(x => x.get('completed')); | ||
|
||
return h('section#main', { | ||
style: {'display': todosData.list.length ? '' : 'none'} | ||
style: {'display': list.size ? '' : 'none'} | ||
}, [ | ||
h('input#toggle-all', { | ||
type: 'checkbox', | ||
checked: allCompleted | ||
}), | ||
h('ul#todo-list', todosData.list | ||
.filter(todosData.filterFn) | ||
h('ul#todo-list', list | ||
.filter(todosData.get('filterFn')) | ||
.toJS() | ||
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 amountCompleted = todosData.list | ||
.filter(todoData => todoData.completed) | ||
.length; | ||
let amountActive = todosData.list.length - amountCompleted; | ||
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': todosData.list.length ? '' : 'none'} | ||
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' + (todosData.filter === '' ? '.selected' : ''), { | ||
h('a' + (filter === '' ? '.selected' : ''), { | ||
attributes: {'href': '#/'} | ||
}, 'All') | ||
]), | ||
h('li', [ | ||
h('a' + (todosData.filter === 'active' ? '.selected' : ''), { | ||
h('a' + (filter === 'active' ? '.selected' : ''), { | ||
attributes: {'href': '#/active'} | ||
}, 'Active') | ||
]), | ||
h('li', [ | ||
h('a' + (todosData.filter === 'completed' ? '.selected' : ''), { | ||
h('a' + (filter === 'completed' ? '.selected' : ''), { | ||
attributes: {'href': '#/completed'} | ||
}, 'Completed') | ||
]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's good practice to have
Map
conflicting with the native one in new JS engines ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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