Skip to content

Commit c8098e9

Browse files
committed
add SAVE todo item title edit implementation for #60
1 parent 2968cd8 commit c8098e9

File tree

3 files changed

+85
-8
lines changed

3 files changed

+85
-8
lines changed

edit-todo.md

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,18 +587,94 @@ test.only('5.3 [ENTER] Key in edit mode triggers SAVE action', function (t) {
587587
// render the view and append it to the DOM inside the `test-app` node:
588588
elmish.mount(model, app.update, app.view, id, app.subscriptions);
589589
// change the
590-
const updated_title = "Do things that don\'t scale!"
590+
const updated_title = "Do things that don\'t scale! "
591591
// apply the updated_title to the <input class="edit">:
592592
document.querySelectorAll('.edit')[0].value = updated_title;
593593
// trigger the [Enter] keyboard key to ADD the new todo:
594594
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 13}));
595-
// confirm that the todo item title was updated to the updated_title
595+
// confirm that the todo item title was updated to the updated_title:
596596
const label = document.querySelectorAll('.view > label')[1].textContent;
597-
t.equal(label, updated_title, "item title updated to:" + updated_title)
597+
t.equal(label, updated_title.trim(),
598+
"item title updated to:" + updated_title + ' (trimmed)');
598599
t.end();
599600
});
600601
```
601602
If you attempt to run this test: `node test/todo-app.test.js`
602603
you will see output similar to the following:
603604

604-
![edit-double-click-test-failing](https://user-images.githubusercontent.com/194400/45183202-54b7e880-b21b-11e8-84d8-7b3b50162113.png)
605+
![save-edit-test-fails](https://user-images.githubusercontent.com/194400/45187886-2c83b600-b22a-11e8-8782-6d3fcaa240df.png)
606+
607+
608+
### 5.3 `'SAVE' update case` _Implementation_
609+
610+
The _first_ step in the implementation is to create the `'SAVE'` case
611+
in `update` function:
612+
613+
```js
614+
case 'SAVE':
615+
var edit = document.getElementsByClassName('edit')[0];
616+
var value = edit.value;
617+
var id = parseInt(edit.id, 10);
618+
// End Editing
619+
new_model.clicked = false;
620+
new_model.editing = false;
621+
622+
if (!value || value.length === 0) { // delete item if title is blank:
623+
return update('DELETE', new_model, id);
624+
}
625+
// update the value of the item.title that has been edited:
626+
new_model.todos = new_model.todos.map(function (item) {
627+
if (item.id === id && value && value.length > 0) {
628+
item.title = value.trim();
629+
}
630+
return item; // return all todo items.
631+
});
632+
break;
633+
```
634+
635+
The _second_ step is _triggering_ this `case` in the `subscriptions`
636+
event listener for `keyup`:
637+
638+
_Before_:
639+
640+
```js
641+
document.addEventListener('keyup', function handler (e) {
642+
switch(e.keyCode) {
643+
case ENTER_KEY:
644+
var new_todo = document.getElementById('new-todo');
645+
if(new_todo.value.length > 0) {
646+
signal('ADD')(); // invoke singal inner callback
647+
new_todo.value = ''; // reset <input> so we can add another todo
648+
document.getElementById('new-todo').focus();
649+
}
650+
break;
651+
}
652+
});
653+
```
654+
655+
656+
_After_:
657+
658+
```js
659+
document.addEventListener('keyup', function handler (e) {
660+
switch(e.keyCode) {
661+
case ENTER_KEY:
662+
var editing = document.getElementsByClassName('editing');
663+
if (editing && editing.length > 0) {
664+
signal('SAVE')(); // invoke singal inner callback
665+
}
666+
667+
var new_todo = document.getElementById('new-todo');
668+
if(new_todo.value.length > 0) {
669+
signal('ADD')(); // invoke singal inner callback
670+
new_todo.value = ''; // reset <input> so we can add another todo
671+
document.getElementById('new-todo').focus();
672+
}
673+
break;
674+
}
675+
});
676+
```
677+
678+
When you run the tests: `node test/todo-app.test.js`
679+
they should now _pass_:
680+
![save-update-test-pass](https://user-images.githubusercontent.com/194400/45188350-d879d100-b22b-11e8-8669-94e080a25ef7.png)

examples/todo-list/todo-app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function update(action, model, data) {
7979
}
8080
break;
8181
case 'SAVE':
82-
var edit = document.querySelectorAll('.edit')[0];
82+
var edit = document.getElementsByClassName('edit')[0];
8383
var value = edit.value;
8484
var id = parseInt(edit.id, 10);
8585
// End Editing

test/todo-app.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ test('5.2.2 Slow clicks do not count as double-click > no edit!', function (t) {
474474
});
475475

476476

477-
test.only('5.3 [ENTER] Key in edit mode triggers SAVE action', function (t) {
477+
test('5.3 [ENTER] Key in edit mode triggers SAVE action', function (t) {
478478
elmish.empty(document.getElementById(id));
479479
localStorage.removeItem('todos-elmish_' + id);
480480
const model = {
@@ -488,13 +488,14 @@ test.only('5.3 [ENTER] Key in edit mode triggers SAVE action', function (t) {
488488
// render the view and append it to the DOM inside the `test-app` node:
489489
elmish.mount(model, app.update, app.view, id, app.subscriptions);
490490
// change the
491-
const updated_title = "Do things that don\'t scale!"
491+
const updated_title = "Do things that don\'t scale! "
492492
// apply the updated_title to the <input class="edit">:
493493
document.querySelectorAll('.edit')[0].value = updated_title;
494494
// trigger the [Enter] keyboard key to ADD the new todo:
495495
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 13}));
496496
// confirm that the todo item title was updated to the updated_title:
497497
const label = document.querySelectorAll('.view > label')[1].textContent;
498-
t.equal(label, updated_title, "item title updated to:" + updated_title)
498+
t.equal(label, updated_title.trim(),
499+
"item title updated to:" + updated_title + ' (trimmed)');
499500
t.end();
500501
});

0 commit comments

Comments
 (0)