11import angular from 'angular'
2- import { loadUser } from '../services/userv3.service.js'
2+ import { loadUser , getCurrentUser } from '../services/userv3.service.js'
33
44( function ( ) {
55 'use strict'
@@ -8,29 +8,96 @@ import { loadUser } from '../services/userv3.service.js'
88
99 ListingsCtrl . $inject = [ 'CONSTANTS' , 'logger' , '$q' , 'TcAuthService' , 'UserService' ,
1010 'UserStatsService' , 'ProfileService' , 'ChallengeService' ,
11- 'ExternalAccountService' , 'ngDialog' , '$anchorScroll' , '$scope'
11+ 'ExternalAccountService' , 'ngDialog' , '$anchorScroll' , '$scope' ,
1212 ]
1313
1414 function ListingsCtrl ( CONSTANTS , logger , $q , TcAuthService , UserService , UserStatsService , ProfileService ,
1515 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'
1622
1723 activate ( )
1824
1925 function activate ( ) {
2026 $scope . myChallenges = [ ]
21- $scope . userProps = { isAuth : false }
27+ $scope . userProps = { isAuth : false , myChallenges : [ ] }
2228 logger . debug ( 'Calling ListingsController activate()' )
23-
29+ vm . myChallenges = [ ]
2430 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+
2537 // update auth flag
2638 if ( TcAuthService . isAuthenticated ( ) ) {
27- $scope . userProps = { isAuth : true }
39+ getChallenges ( handle )
40+ $scope . userProps = { isAuth : true , myChallenges : vm . myChallenges }
2841 }
2942 } , function ( error ) {
3043 // do nothing, just show non logged in state of navigation bar
3144 } )
3245 }
3346
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+ }
34101 }
35102
36103} ) ( )
0 commit comments