1+ import _ from 'lodash'
12import angular from 'angular'
23import { loadUser } from '../services/userv3.service.js'
34
@@ -13,24 +14,91 @@ import { loadUser } from '../services/userv3.service.js'
1314
1415 function ListingsCtrl ( CONSTANTS , logger , $q , TcAuthService , UserService , UserStatsService , ProfileService ,
1516 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'
1623
1724 activate ( )
1825
1926 function activate ( ) {
2027 $scope . myChallenges = [ ]
21- $scope . userProps = { isAuth : false }
28+ $scope . userProps = { isAuth : false , myChallenges : [ ] }
2229 logger . debug ( 'Calling ListingsController activate()' )
23-
30+ vm . myChallenges = [ ]
2431 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+
2538 // update auth flag
2639 if ( TcAuthService . isAuthenticated ( ) ) {
27- $scope . userProps = { isAuth : true }
40+ getChallenges ( handle )
41+ $scope . userProps = { isAuth : true , myChallenges : vm . myChallenges }
2842 }
2943 } , function ( error ) {
3044 // do nothing, just show non logged in state of navigation bar
3145 } )
3246 }
3347
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+ }
34102 }
35103
36104} ) ( )
0 commit comments