@@ -23,12 +23,92 @@ <h2>LOCAL TAPAS</h2>
23
23
< input type ="text " name ="item " placeholder ="Item Name " required >
24
24
< input type ="submit " value ="+ Add Item ">
25
25
</ form >
26
+ < div >
27
+ < button onclick ="checkAll() "> Check All</ button >
28
+ < button onclick ="uncheckAll() "> Uncheck All</ button >
29
+ < button onclick ="deleteList() "> Delete List</ button >
30
+ </ div >
26
31
</ div >
27
32
28
33
< script >
29
34
const addItems = document . querySelector ( '.add-items' ) ;
30
35
const itemsList = document . querySelector ( '.plates' ) ;
31
- const items = [ ] ;
36
+ const items = JSON . parse ( localStorage . getItem ( 'items' ) ) || [ ] ;
37
+
38
+ function checkAll ( ) {
39
+ items . forEach ( item => {
40
+ item . done = true ;
41
+ } ) ;
42
+ populateList ( items , itemsList ) ;
43
+ localStorage . setItem ( 'items' , JSON . stringify ( items ) ) ;
44
+ }
45
+
46
+ function uncheckAll ( ) {
47
+ items . forEach ( item => {
48
+ item . done = false ;
49
+ } ) ;
50
+ populateList ( items , itemsList ) ;
51
+ localStorage . setItem ( 'items' , JSON . stringify ( items ) ) ;
52
+ }
53
+
54
+ function deleteList ( ) {
55
+ //Remove items from list
56
+ while ( items . length > 0 ) {
57
+ items . pop ( ) ;
58
+ }
59
+ //Add input to the items list
60
+ populateList ( items , itemsList ) ;
61
+ //Save item into local storage
62
+ localStorage . setItem ( 'items' , JSON . stringify ( items ) )
63
+ }
64
+
65
+ function addItem ( e ) {
66
+ //Stops page reloading on form submission
67
+ e . preventDefault ( ) ;
68
+ //Gets the value of the input
69
+ const text = ( this . querySelector ( '[name=item]' ) ) . value ;
70
+
71
+ const item = {
72
+ text, //ES6 shortcut property
73
+ done : false
74
+ }
75
+ //Add input to item list
76
+ items . push ( item ) ;
77
+ //Add input to the items list
78
+ populateList ( items , itemsList ) ;
79
+ //Save item into local storage
80
+ localStorage . setItem ( 'items' , JSON . stringify ( items ) ) ;
81
+ //Resets the input of the form input
82
+ this . reset ( ) ;
83
+ }
84
+
85
+ function populateList ( plates = [ ] , platesList ) {
86
+ platesList . innerHTML = plates . map ( ( plate , i ) => {
87
+ return `
88
+ <li>
89
+ <input type="checkbox" data-index=${ i } id="item${ i } " ${ plate . done ? 'checked' : '' } />
90
+ <label for="item${ i } ">${ plate . text } </label>
91
+ </li>
92
+ ` ;
93
+ } ) . join ( '' ) ;
94
+ }
95
+
96
+ function toggleDone ( e ) {
97
+ if ( ! e . target . matches ( 'input' ) ) return ; //skip unless its the input
98
+ const el = e . target ;
99
+ const index = el . dataset . index ;
100
+ //Change the done property
101
+ items [ index ] . done = ! items [ index ] . done ;
102
+ //Update local storage
103
+ localStorage . setItem ( 'items' , JSON . stringify ( items ) ) ;
104
+ //Update the UI
105
+ populateList ( items , itemsList ) ;
106
+ }
107
+
108
+ addItems . addEventListener ( 'submit' , addItem ) ;
109
+ itemsList . addEventListener ( 'click' , toggleDone ) ;
110
+
111
+ populateList ( items , itemsList ) ;
32
112
33
113
</ script >
34
114
0 commit comments