Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit f258e55

Browse files
committed
Merge pull request #24 from appirio-tech/DEM-1207
DEM-1207 Update Type Controller for new Data Model
2 parents 49fcc93 + 78bae33 commit f258e55

File tree

3 files changed

+180
-117
lines changed

3 files changed

+180
-117
lines changed
Lines changed: 140 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,159 @@
11
'use strict'
22

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
145
vm.loading = true
156
vm.showSuccessModal = false
167
vm.workId = $scope.workId
178

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
3365
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()
62140

63141
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()
69144

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()
74147

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)
80150
else
81-
vm.loading = false
151+
onChange()
82152

83153
vm
84154

85155
activate()
86156

87-
SubmitWorkTypeController.$inject = ['$scope', 'SubmitWorkAPIService']
157+
SubmitWorkTypeController.$inject = ['$scope', '$rootScope', 'Optimist', 'SubmitWorkService']
88158

89159
angular.module('appirio-tech-ng-submit-work').controller 'SubmitWorkTypeController', SubmitWorkTypeController

src/views/submit-work-type.directive.jade

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,45 @@ form(ng-submit="vm.save()")
1919
h4 Devices
2020

2121
ul
22-
li
23-
checkbox(label="iPhone 5c" ng-model="vm.work.devices.iPhone5c")
24-
25-
li
26-
checkbox(label="iPhone 5s" ng-model="vm.work.devices.iPhone5s")
22+
li(ng-repeat="device in vm.type.devices")
23+
checkbox(label="{{device.name}}" ng-model="device.selected")
2724

2825
li
2926
h4 Orientation
3027

3128
ul
32-
li
33-
checkbox(label="Landscape" ng-model="vm.work.orientation.landscape")
29+
li(ng-repeat="orientation in vm.type.orientations")
30+
checkbox(label="{{orientation.name}}" ng-model="orientation.selected")
3431

35-
li
36-
checkbox(label="Portrait" ng-model="vm.work.orientation.portrait")
3732
li
3833
h4 OS
3934

4035
ul
41-
li
42-
checkbox(label="iOS 7" ng-model="vm.work.os.iOS7")
43-
44-
li
45-
checkbox(label="iOS 8" ng-model="vm.work.os.iOS8")
36+
li(ng-repeat="os in vm.type.operatingSystems")
37+
checkbox(label="{{os.name}}" ng-model="os.selected")
4638

4739
button.wider.continue.action(type="button" scroll-element="typeDetails") continue
4840

4941
ul.type.interactive(id="typeDetails")
5042
li
5143
h2 What <strong>type of work</strong> are you looking for?
5244

53-
li
54-
h4 Design
45+
li(ng-repeat="requestType in vm.type.requestTypes")
46+
h4 {{requestType.name}}
5547

5648
p Lorem ipsum dolor sit amet, consectetur adipiscing elit.
5749

58-
img(src="http://www.collegequest.com/wp-content/uploads/what-do-graphic-designers-do.jpg")
50+
img(ng-if="requestType.name == 'Design'" src="http://www.collegequest.com/wp-content/uploads/what-do-graphic-designers-do.jpg")
5951

60-
selected-button(
52+
img(ng-if="requestType.name == 'Design & Development'" src="http://pbwebdev.com/blog/wp-content/uploads/2014/06/developer.jpg")
53+
54+
button(
6155
type="button"
62-
value="vm.requestTypes.design"
63-
ng-model="vm.work.requestTypes"
56+
selectable="true"
57+
ng-model="vm.work.requestType"
58+
value="requestType.name"
6459
) Design
6560

66-
li
67-
h4 Development
68-
69-
p Lorem ipsum dolor sit amet, consectetur adipiscing elit.
70-
71-
img(src="http://pbwebdev.com/blog/wp-content/uploads/2014/06/developer.jpg")
72-
73-
selected-button(
74-
type="button"
75-
value="vm.requestTypes.code"
76-
ng-model="vm.work.requestTypes"
77-
) Development
7861

7962
button.wider.continue.action(type="button" scroll-element="briefDetails") continue
8063

tests/specs/type.controller.spec.coffee

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ saveSpy = null
55

66
describe 'SubmitWorkTypeController', ->
77
beforeEach ->
8-
bard.inject this, '$rootScope', '$q', '$controller', 'SubmitWorkAPIService'
8+
bard.inject this, '$rootScope', '$q', '$controller', 'SubmitWorkService'
99
scope = $rootScope.$new()
1010

1111
work =
@@ -17,11 +17,13 @@ describe 'SubmitWorkTypeController', ->
1717
promise = $q.when work
1818
_default = $promise: promise
1919

20-
bard.mockService SubmitWorkAPIService, _default: _default
20+
bard.mockService SubmitWorkService,
21+
_default: _default
22+
save: $q.when
2123

2224
controller = $controller 'SubmitWorkTypeController', $scope: scope
2325
scope.vm = controller
24-
saveSpy = sinon.spy controller, 'save'
26+
saveSpy = sinon.spy SubmitWorkService, 'save'
2527

2628
afterEach ->
2729
saveSpy.restore()
@@ -31,8 +33,8 @@ describe 'SubmitWorkTypeController', ->
3133
expect(controller).to.be.defined
3234

3335
context 'when a new project', ->
34-
it 'should not call API service for work data', ->
35-
expect(SubmitWorkAPIService.get.called).not.to.be.ok
36+
it 'should call service for work data', ->
37+
expect(SubmitWorkService.fetch.called).not.to.be.ok
3638

3739
it 'should initialize work', ->
3840
expect(controller.work).to.be.ok
@@ -44,20 +46,28 @@ describe 'SubmitWorkTypeController', ->
4446
it 'should have a save method', ->
4547
expect(controller.save).to.exist
4648

47-
it 'should call API service with put to save project', ->
49+
it 'should call service to save project', ->
4850
controller.workId = '123'
4951
controller.work.name = 'abc'
50-
controller.work.requestTypes = ['Design', 'Code']
5152
controller.work.summary = 'abc'
53+
controller.type.requestTypes = [
54+
name: 'Design'
55+
selected: true
56+
]
57+
controller.type.devices = [
58+
name: 'iphone'
59+
selected: true
60+
]
61+
controller.type.orientations = [
62+
name: 'landscape'
63+
selected: true
64+
]
65+
controller.type.operatingSystems = [
66+
name: 'ios'
67+
selected: true
68+
]
5269
controller.save()
53-
expect(SubmitWorkAPIService.put.called).to.be.ok
54-
55-
it 'should call API service with post to create new project', ->
56-
controller.work.name = 'abc'
57-
controller.work.requestTypes = ['Design']
58-
controller.work.summary = 'abc'
59-
controller.save()
60-
expect(SubmitWorkAPIService.post.called).to.be.ok
70+
expect(SubmitWorkService.save.called).to.be.ok
6171

6272
it 'should initialize work', ->
6373
expect(controller.work).to.be.defined

0 commit comments

Comments
 (0)