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