@@ -11,6 +11,7 @@ import {
11
11
} from './components' ;
12
12
import { handleLottoInput } from './utils' ;
13
13
import { DEMO_TICKETS } from './constants' ;
14
+
14
15
const STORAGE_KEY = 'appState' ;
15
16
16
17
class App extends Component {
@@ -39,41 +40,63 @@ class App extends Component {
39
40
return initialState ;
40
41
} ;
41
42
43
+ componentDidMount ( ) {
44
+ if ( window . localStorage ) {
45
+ const cache = JSON . parse ( window . localStorage . getItem ( STORAGE_KEY ) ) ;
46
+ if ( cache != null ) {
47
+ this . setState ( { ...cache , value : '' } ) ; // eslint-disable-line react/no-did-mount-set-state
48
+ }
49
+ }
50
+ window . addEventListener ( 'beforeunload' , this . componentUnmount ) ;
51
+ }
52
+
53
+ componentWillUnmount ( ) {
54
+ this . componentUnmount ( ) ;
55
+ }
56
+
42
57
getDemoTickets = ( ) => {
43
58
this . setState ( { tickets : DEMO_TICKETS } ) ;
44
59
this . disableEditMode ( ) ;
45
60
} ;
46
61
47
- addTicket = ( ) => {
48
- console . log ( 'TODO: Add a ticket functionality' ) ;
62
+ setStatus = status => {
63
+ this . setState ( { status } ) ;
64
+ } ;
65
+
66
+ resetStatus = ( ) => {
67
+ this . setState ( { status : this . getInitialState ( ) . status } ) ;
49
68
} ;
50
69
51
70
resetState = ( ) => {
52
71
this . setState ( this . getInitialState ( ) ) ;
53
72
} ;
54
73
55
74
removeLocalStorage = ( ) => {
56
- localStorage . removeItem ( STORAGE_KEY ) ;
75
+ if ( window . localStorage ) window . localStorage . removeItem ( STORAGE_KEY ) ;
57
76
} ;
58
77
59
78
pushStateToStorage = ( ) => {
60
79
this . removeLocalStorage ( ) ;
61
- localStorage . setItem ( STORAGE_KEY , JSON . stringify ( this . state ) ) ;
80
+ if ( window . localStorage )
81
+ window . localStorage . setItem ( STORAGE_KEY , JSON . stringify ( this . state ) ) ;
62
82
} ;
63
83
64
- setStatus = status => {
65
- this . setState ( { status } ) ;
66
- } ;
67
-
68
- resetStatus = ( ) => {
69
- this . setState ( { status : this . getInitialState ( ) . status } ) ;
84
+ addTicket = ( ) => {
85
+ // eslint-disable-next-line no-console
86
+ console . log ( 'TODO: Add a ticket functionality' ) ;
70
87
} ;
71
88
72
89
countOccurencies = number => {
73
90
let occurenciesCount = 0 ;
74
91
this . state . tickets . map ( ticket => {
75
92
const arr = ticket . values . toString ( ) . split ( ',' ) ;
76
- occurenciesCount += arr . reduce ( ( a , v ) => ( v === number ? ++ a : a ) , 0 ) ;
93
+ occurenciesCount += arr . reduce ( ( a , v ) => {
94
+ if ( v === number ) {
95
+ const increment = a + 1 ;
96
+ return increment ;
97
+ }
98
+ return a ;
99
+ } , 0 ) ;
77
100
return true ;
78
101
} ) ;
79
102
return occurenciesCount ;
@@ -104,7 +127,7 @@ class App extends Component {
104
127
105
128
handleEnteredNumberCrossout = e => {
106
129
e . preventDefault ( ) ;
107
- const value = this . state . value ;
130
+ const { value } = this . state ;
108
131
if (
109
132
value . length >= 1 &&
110
133
value >= 1 &&
@@ -118,6 +141,7 @@ class App extends Component {
118
141
119
142
invalidateCache = ( ) => {
120
143
if (
144
+ // eslint-disable-next-line no-alert
121
145
window . confirm ( 'Ar tikrai? Bus ištrinti bilietai ir kamuoliukų istorija.' )
122
146
) {
123
147
this . removeLocalStorage ( ) ;
@@ -127,6 +151,7 @@ class App extends Component {
127
151
128
152
removeLastRolledValue = ( ) => {
129
153
if (
154
+ // eslint-disable-next-line no-alert
130
155
window . confirm ( 'Ar tikrai norite pašalinti paskutinį ridentą kamuoliuką?' )
131
156
) {
132
157
this . setState ( { rolledValues : this . state . rolledValues . slice ( 0 , - 1 ) } ) ;
@@ -163,12 +188,15 @@ class App extends Component {
163
188
} ;
164
189
165
190
handleEditModeChange = ( ) => {
166
- this . state . options . isEditMode
167
- ? this . disableEditMode ( )
168
- : this . enableEditMode ( ) ;
191
+ if ( this . state . options . isEditMode ) {
192
+ this . disableEditMode ( ) ;
193
+ } else {
194
+ this . enableEditMode ( ) ;
195
+ }
169
196
} ;
170
197
171
198
removeTicketByIndex = id => {
199
+ // eslint-disable-next-line no-alert
172
200
if ( window . confirm ( 'Ar tikrai? Bus ištrintas pasirinktas bilietas.' ) ) {
173
201
this . setState ( {
174
202
tickets : this . state . tickets . filter ( ( item , index ) => index !== id ) ,
@@ -181,18 +209,6 @@ class App extends Component {
181
209
window . removeEventListener ( 'beforeunload' , this . componentUnmount ) ;
182
210
} ;
183
211
184
- componentWillMount ( ) {
185
- var cache = JSON . parse ( localStorage . getItem ( STORAGE_KEY ) ) ;
186
- if ( cache != null ) {
187
- this . setState ( { ...cache , value : '' } ) ;
188
- }
189
- window . addEventListener ( 'beforeunload' , this . componentUnmount ) ;
190
- }
191
-
192
- componentWillUnmount ( ) {
193
- this . componentUnmount ( ) ;
194
- }
195
-
196
212
openCreateTicketModal = ( ) => {
197
213
this . setState ( { modalCreateTicketOpen : true } ) ;
198
214
} ;
@@ -260,10 +276,10 @@ class App extends Component {
260
276
{ rolledValues . length ? (
261
277
< div className = "d-flex align-items-center w-100" >
262
278
< div style = { { flex : 1 } } >
263
- { rolledValues . map ( ( value , index ) => (
279
+ { rolledValues . map ( ( rolledValue , index ) => (
264
280
< Sphere
265
281
key = { index }
266
- value = { value }
282
+ value = { rolledValue }
267
283
className = "d-inline-block mr-1 my-1"
268
284
/>
269
285
) ) }
@@ -288,16 +304,14 @@ class App extends Component {
288
304
{ tickets . map ( ( ticket , index ) => (
289
305
< div key = { index } className = "col-12 col-md-6 col-lg-4" >
290
306
< Ticket
291
- index = { index }
307
+ ticketIndex = { index }
292
308
onTicketRemove = {
293
- isEditMode
294
- ? value => this . removeTicketByIndex ( value )
295
- : undefined
309
+ isEditMode ? this . removeTicketByIndex : undefined
296
310
}
297
311
className = "d-inline-block text-center my-2"
298
312
rolledValues = { this . state . rolledValues }
299
313
isClickable = { isTicketsClickable }
300
- clickHandler = { value => this . crossOutNumber ( value ) }
314
+ clickHandler = { this . crossOutNumber }
301
315
// isEditable
302
316
{ ...ticket }
303
317
/>
0 commit comments