1
+ import _ from 'lodash'
1
2
import angular from 'angular'
2
3
import { loadUser } from '../services/userv3.service.js'
3
4
@@ -13,24 +14,91 @@ import { loadUser } from '../services/userv3.service.js'
13
14
14
15
function ListingsCtrl ( CONSTANTS , logger , $q , TcAuthService , UserService , UserStatsService , ProfileService ,
15
16
ChallengeService , ExternalAccountService , ngDialog , $anchorScroll , $scope ) {
17
+ var vm = this
18
+ var handle
19
+ vm . neverParticipated = false
20
+ vm . loading = true
21
+ vm . userHasChallenges = true
22
+ vm . challengeView = 'tile'
16
23
17
24
activate ( )
18
25
19
26
function activate ( ) {
20
27
$scope . myChallenges = [ ]
21
- $scope . userProps = { isAuth : false }
28
+ $scope . userProps = { isAuth : false , myChallenges : [ ] }
22
29
logger . debug ( 'Calling ListingsController activate()' )
23
-
30
+ vm . myChallenges = [ ]
24
31
loadUser ( ) . then ( function ( token ) {
32
+ handle = UserService . getUserIdentity ( ) . handle
33
+ // mock current user have this challenges
34
+ vm . myChallenges . push ( { 'id' :30056409 } )
35
+ vm . myChallenges . push ( { 'id' :30056067 } )
36
+ vm . myChallenges . push ( { 'id' :16870 } )
37
+
25
38
// update auth flag
26
39
if ( TcAuthService . isAuthenticated ( ) ) {
27
- $scope . userProps = { isAuth : true }
40
+ getChallenges ( handle )
41
+ $scope . userProps = { isAuth : true , myChallenges : vm . myChallenges }
28
42
}
29
43
} , function ( error ) {
30
44
// do nothing, just show non logged in state of navigation bar
31
45
} )
32
46
}
33
47
48
+ function getChallenges ( handle ) {
49
+ var marathonMatchParams = {
50
+ limit : 8 ,
51
+ filter : 'status=active'
52
+ }
53
+
54
+ var challengeParams = {
55
+ limit : 8 ,
56
+ orderBy : 'submissionEndDate' ,
57
+ filter : 'status=active'
58
+ }
59
+
60
+ $q . all ( [
61
+ ChallengeService . getUserMarathonMatches ( handle , marathonMatchParams ) ,
62
+ ChallengeService . getUserChallenges ( handle , challengeParams )
63
+ ] ) . then ( function ( challenges ) {
64
+ var marathonMatches = challenges [ 0 ]
65
+ var devDesignChallenges = challenges [ 1 ]
66
+
67
+ if ( ! marathonMatches . length && ! devDesignChallenges . length ) {
68
+ vm . userHasChallenges = false
69
+ _checkForParticipation ( ) . then ( function ( ) {
70
+ vm . loading = false
71
+ } )
72
+ } else {
73
+ ChallengeService . processActiveDevDesignChallenges ( devDesignChallenges )
74
+ ChallengeService . processActiveMarathonMatches ( marathonMatches )
75
+ var userChallenges = marathonMatches . concat ( devDesignChallenges )
76
+
77
+ userChallenges = _ . sortBy ( userChallenges , function ( n ) {
78
+ return n . registrationEndDate
79
+ } )
80
+ vm . myChallenges = userChallenges . reverse ( ) . slice ( 0 , 8 )
81
+
82
+ // update myChallenges
83
+ $scope . userProps = { isAuth : true , myChallenges : vm . myChallenges }
84
+
85
+ vm . userHasChallenges = true
86
+ vm . loading = false
87
+ }
88
+ } )
89
+ . catch ( function ( err ) {
90
+ logger . error ( 'Error getting challenges and marathon matches' , err )
91
+
92
+ vm . userHasChallenges = true
93
+ vm . loading = false
94
+ } )
95
+ }
96
+
97
+ function _checkForParticipation ( ) {
98
+ return ChallengeService . checkChallengeParticipation ( handle , function ( participated ) {
99
+ vm . neverParticipated = ! participated
100
+ } )
101
+ }
34
102
}
35
103
36
104
} ) ( )
0 commit comments