1
1
' use strict'
2
2
3
- SubmitWorkTypeController = ($scope , SubmitWorkAPIService ) ->
4
- vm = this
5
- vm .work =
6
- name : null
7
- requestType : null
8
- summary : null
9
- features : []
10
-
11
- vm .requestTypes =
12
- code : ' code'
13
- design : ' design'
3
+ SubmitWorkTypeController = ($scope , $rootScope , Optimist , SubmitWorkService ) ->
4
+ vm = this
14
5
vm .loading = true
15
6
vm .showSuccessModal = false
16
7
vm .workId = $scope .workId
17
8
18
- isValid = ->
19
- vm .work .name && vm .work .requestTypes .length && vm .work .summary
20
-
21
- vm .save = (onSuccess ) ->
22
- if isValid ()
23
- if vm .workId
24
- params =
25
- id : vm .workId
26
-
27
- resource = SubmitWorkAPIService .put params, vm .work
28
-
29
- resource .$promise .then (response) ->
30
- onSuccess? response
31
-
32
- resource .$promise .catch (response) ->
9
+ config =
10
+ name : null
11
+
12
+ config .requestTypes = [
13
+ name : ' Design'
14
+ id : ' 1235'
15
+ selected : false
16
+ ,
17
+ name : ' Design & Development'
18
+ id : ' 1234'
19
+ selected : false
20
+ ]
21
+
22
+ config .devices = [
23
+ name : ' iPhone5c'
24
+ id : ' 1234'
25
+ selected : false
26
+ ,
27
+ name : ' iPhone5s'
28
+ id : ' 1235'
29
+ selected : false
30
+ ]
31
+
32
+ config .orientations = [
33
+ name : ' Landscape'
34
+ id : ' 1234'
35
+ selected : false
36
+ ,
37
+ name : ' Portrait'
38
+ id : ' 1235'
39
+ selected : false
40
+ ]
41
+
42
+ config .operatingSystems = [
43
+ name : ' iOS7'
44
+ id : ' 1234'
45
+ selected : false
46
+ ,
47
+ name : ' iOS8'
48
+ id : ' 1235'
49
+ selected : false
50
+ ]
51
+
52
+ vm .save = ->
53
+ workValid = typeValid ()
54
+ updates = getUpdates ()
55
+ if workValid
56
+ SubmitWorkService .save (updates).then ->
57
+ vm .showSuccessModal = true
58
+
59
+ typeValid = ->
60
+ updates = getUpdates ()
61
+ isValid = true
62
+ for type, value of updates
63
+ if Array .isArray value
64
+ isValid = false unless value .length
33
65
else
34
- vm .work .status = ' Submitted'
35
- resource = SubmitWorkAPIService .post vm .work
36
-
37
- resource .$promise .then (response) ->
38
- vm .showSuccessModal = true
39
-
40
- onSuccess? response
41
-
42
- resource .$promise .catch (response) ->
43
-
44
- onSuccess? response
45
-
46
- resource .$promise .catch (response) ->
47
-
48
- mockify = (work ) ->
49
- work .requestTypes = []
50
- work .devices =
51
- iPhone5c : false
52
- iPhone5s : false
53
-
54
- work .orientation =
55
- landscape : false
56
- portrait : false
57
-
58
- work .os =
59
- iOS7 : false
60
- iOS8 : false
61
-
66
+ isValid = value
67
+
68
+ isValid
69
+
70
+ getUpdates = ->
71
+ updates =
72
+ requestType : vm .work .requestType
73
+ name : vm .work .name
74
+ summary : vm .work .summary
75
+ devices : []
76
+ orientations : []
77
+ operatingSystems : []
78
+
79
+ vm .type .devices .forEach (device) ->
80
+ if device .selected
81
+ updates .devices .push
82
+ id : device .id
83
+
84
+ vm .type .orientations .forEach (orientation) ->
85
+ if orientation .selected
86
+ updates .orientations .push
87
+ id : orientation .id
88
+
89
+ vm .type .operatingSystems .forEach (operatingSystem) ->
90
+ if operatingSystem .selected
91
+ updates .operatingSystems .push
92
+ id : operatingSystem .id
93
+
94
+ updates
95
+
96
+ onChange = ->
97
+ if SubmitWorkService .work
98
+ # TODO: remove mock data
99
+ SubmitWorkService .work .devices = [id : ' 1234' ]
100
+ SubmitWorkService .work .orientations = [id : ' 1235' ]
101
+ SubmitWorkService .work .operatingSystems = [id : ' 1235' ]
102
+
103
+ vm .work =
104
+ name : SubmitWorkService .work .name
105
+ requestType : SubmitWorkService .work .requestType
106
+ summary : SubmitWorkService .work .summary
107
+ devices : SubmitWorkService .work .devices
108
+ orientations : SubmitWorkService .work .orientations
109
+ operatingSystems : SubmitWorkService .work .operatingSystems
110
+ else
111
+ vm .work =
112
+ name : null
113
+ requestType : null
114
+ summary : null
115
+ devices : []
116
+ orientations : []
117
+ operatingSystems : []
118
+
119
+ vm .loading = false
120
+
121
+ unless vm .type
122
+ vm .type = config
123
+ # set already selected choices to selected on vm
124
+ vm .work .devices .forEach (device) ->
125
+ vm .type .devices .forEach (vmDevice) ->
126
+ if device .id == vmDevice .id
127
+ vmDevice .selected = true
128
+
129
+ vm .work .orientations .forEach (orientation) ->
130
+ vm .type .orientations .forEach (vmOrientation) ->
131
+ if orientation .id == vmOrientation .id
132
+ vmOrientation .selected = true
133
+
134
+ vm .work .operatingSystems .forEach (os) ->
135
+ vm .type .operatingSystems .forEach (vmOs) ->
136
+ if os .id == vmOs .id
137
+ vmOs .selected = true
138
+
139
+ updates = getUpdates ()
62
140
63
141
activate = ->
64
- if vm .workId
65
- params =
66
- id : vm .workId
67
-
68
- resource = SubmitWorkAPIService .get params
142
+ destroyWorkListener = $rootScope .$on " SubmitWorkService.work:changed" , ->
143
+ onChange ()
69
144
70
- resource .$promise .then (response) ->
71
- vm .work = response
72
- # TODO: remove once all properties are in payload
73
- mockify vm .work
145
+ $scope .$on ' $destroy' , ->
146
+ destroyWorkListener ()
74
147
75
- resource .$promise .catch (response) ->
76
- # TODO: add error handling
77
-
78
- resource .$promise .finally ->
79
- vm .loading = false
148
+ if vm .workId
149
+ SubmitWorkService .fetch (vm .workId )
80
150
else
81
- vm . loading = false
151
+ onChange ()
82
152
83
153
vm
84
154
85
155
activate ()
86
156
87
- SubmitWorkTypeController .$inject = [' $scope' , ' SubmitWorkAPIService ' ]
157
+ SubmitWorkTypeController .$inject = [' $scope' , ' $rootScope ' , ' Optimist ' , ' SubmitWorkService ' ]
88
158
89
159
angular .module (' appirio-tech-ng-submit-work' ).controller ' SubmitWorkTypeController' , SubmitWorkTypeController
0 commit comments