@@ -36,20 +36,37 @@ const primesInitialState = {
36
36
function primeState ( state = primesInitialState , action ) {
37
37
switch ( action . type ) {
38
38
case ADD_PRIME :
39
- const primes = [ ...state . primes , action . number ] ;
39
+ const primes = [ ...state . primes ] ;
40
+
41
+ // Only add it to the list if it's not been added before.
42
+ if ( primes . indexOf ( action . number ) === - 1 ) {
43
+ primes . push ( action . number ) ;
44
+ }
40
45
41
46
return Object . assign ( { } , state , {
42
47
primes
43
48
} ) ;
44
49
case ADD_NON_PRIME :
45
- const nonPrimes = [ ...state . nonPrimes , action . number ] ;
50
+ const nonPrimes = [ ...state . nonPrimes ] ;
51
+
52
+ // Only add it to the list if it's not been added before.
53
+ if ( nonPrimes . indexOf ( action . number ) === - 1 ) {
54
+ nonPrimes . push ( action . number ) ;
55
+ }
46
56
47
57
return Object . assign ( { } , state , {
48
58
nonPrimes
49
59
} ) ;
50
60
case ADD_QUEUE_NUMBER :
51
61
console . log ( "queueing number: " + action . number ) ;
52
- const addedQueue = [ ...state . queue , action . number ] ;
62
+ const addedQueue = [ ...state . queue ] ;
63
+
64
+ // Only add it to the queue if it's not been added before.
65
+ if ( addedQueue . indexOf ( action . number ) === - 1 &&
66
+ state . primes . indexOf ( action . number ) === - 1 &&
67
+ state . nonPrimes . indexOf ( action . number ) === - 1 ) {
68
+ addedQueue . push ( action . number ) ;
69
+ }
53
70
54
71
return Object . assign ( { } , state , {
55
72
queue : addedQueue
0 commit comments