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

DEM-1207 Update Type Controller for new Data Model #24

Merged
merged 8 commits into from
Sep 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 140 additions & 70 deletions src/scripts/controllers/type.controller.coffee
Original file line number Diff line number Diff line change
@@ -1,89 +1,159 @@
'use strict'

SubmitWorkTypeController = ($scope, SubmitWorkAPIService) ->
vm = this
vm.work =
name : null
requestType: null
summary : null
features : []

vm.requestTypes =
code: 'code'
design: 'design'
SubmitWorkTypeController = ($scope, $rootScope, Optimist, SubmitWorkService) ->
vm = this
vm.loading = true
vm.showSuccessModal = false
vm.workId = $scope.workId

isValid = ->
vm.work.name && vm.work.requestTypes.length && vm.work.summary

vm.save = (onSuccess) ->
if isValid()
if vm.workId
params =
id: vm.workId

resource = SubmitWorkAPIService.put params, vm.work

resource.$promise.then (response) ->
onSuccess? response

resource.$promise.catch (response) ->
config =
name: null

config.requestTypes = [
name: 'Design'
id: '1235'
selected: false
,
name: 'Design & Development'
id: '1234'
selected: false
]

config.devices = [
name: 'iPhone5c'
id: '1234'
selected: false
,
name: 'iPhone5s'
id: '1235'
selected: false
]

config.orientations = [
name: 'Landscape'
id: '1234'
selected: false
,
name: 'Portrait'
id: '1235'
selected: false
]

config.operatingSystems = [
name: 'iOS7'
id: '1234'
selected: false
,
name: 'iOS8'
id: '1235'
selected: false
]

vm.save = ->
workValid = typeValid()
updates = getUpdates()
if workValid
SubmitWorkService.save(updates).then ->
vm.showSuccessModal = true

typeValid = ->
updates = getUpdates()
isValid = true
for type, value of updates
if Array.isArray value
isValid = false unless value.length
else
vm.work.status = 'Submitted'
resource = SubmitWorkAPIService.post vm.work

resource.$promise.then (response) ->
vm.showSuccessModal = true

onSuccess? response

resource.$promise.catch (response) ->

onSuccess? response

resource.$promise.catch (response) ->

mockify = (work) ->
work.requestTypes = []
work.devices =
iPhone5c: false
iPhone5s: false

work.orientation =
landscape: false
portrait: false

work.os =
iOS7: false
iOS8: false

isValid = value

isValid

getUpdates = ->
updates =
requestType: vm.work.requestType
name: vm.work.name
summary: vm.work.summary
devices: []
orientations: []
operatingSystems: []

vm.type.devices.forEach (device) ->
if device.selected
updates.devices.push
id: device.id

vm.type.orientations.forEach (orientation) ->
if orientation.selected
updates.orientations.push
id: orientation.id

vm.type.operatingSystems.forEach (operatingSystem) ->
if operatingSystem.selected
updates.operatingSystems.push
id: operatingSystem.id

updates

onChange = ->
if SubmitWorkService.work
# TODO: remove mock data
SubmitWorkService.work.devices = [id:'1234']
SubmitWorkService.work.orientations = [id:'1235']
SubmitWorkService.work.operatingSystems = [id:'1235']

vm.work =
name: SubmitWorkService.work.name
requestType: SubmitWorkService.work.requestType
summary : SubmitWorkService.work.summary
devices: SubmitWorkService.work.devices
orientations: SubmitWorkService.work.orientations
operatingSystems: SubmitWorkService.work.operatingSystems
else
vm.work =
name: null
requestType: null
summary : null
devices: []
orientations: []
operatingSystems: []

vm.loading = false

unless vm.type
vm.type = config
# set already selected choices to selected on vm
vm.work.devices.forEach (device) ->
vm.type.devices.forEach (vmDevice) ->
if device.id == vmDevice.id
vmDevice.selected = true

vm.work.orientations.forEach (orientation) ->
vm.type.orientations.forEach (vmOrientation) ->
if orientation.id == vmOrientation.id
vmOrientation.selected = true

vm.work.operatingSystems.forEach (os) ->
vm.type.operatingSystems.forEach (vmOs) ->
if os.id == vmOs.id
vmOs.selected = true

updates = getUpdates()

activate = ->
if vm.workId
params =
id: vm.workId

resource = SubmitWorkAPIService.get params
destroyWorkListener = $rootScope.$on "SubmitWorkService.work:changed", ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets avoid using $rootScope.$on, an alternative option is to use $scope.$watch

onChange()

resource.$promise.then (response) ->
vm.work = response
#TODO: remove once all properties are in payload
mockify vm.work
$scope.$on '$destroy', ->
destroyWorkListener()

resource.$promise.catch (response) ->
# TODO: add error handling

resource.$promise.finally ->
vm.loading = false
if vm.workId
SubmitWorkService.fetch(vm.workId)
else
vm.loading = false
onChange()

vm

activate()

SubmitWorkTypeController.$inject = ['$scope', 'SubmitWorkAPIService']
SubmitWorkTypeController.$inject = ['$scope', '$rootScope', 'Optimist', 'SubmitWorkService']

angular.module('appirio-tech-ng-submit-work').controller 'SubmitWorkTypeController', SubmitWorkTypeController
47 changes: 15 additions & 32 deletions src/views/submit-work-type.directive.jade
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,45 @@ form(ng-submit="vm.save()")
h4 Devices

ul
li
checkbox(label="iPhone 5c" ng-model="vm.work.devices.iPhone5c")

li
checkbox(label="iPhone 5s" ng-model="vm.work.devices.iPhone5s")
li(ng-repeat="device in vm.type.devices")
checkbox(label="{{device.name}}" ng-model="device.selected")

li
h4 Orientation

ul
li
checkbox(label="Landscape" ng-model="vm.work.orientation.landscape")
li(ng-repeat="orientation in vm.type.orientations")
checkbox(label="{{orientation.name}}" ng-model="orientation.selected")

li
checkbox(label="Portrait" ng-model="vm.work.orientation.portrait")
li
h4 OS

ul
li
checkbox(label="iOS 7" ng-model="vm.work.os.iOS7")

li
checkbox(label="iOS 8" ng-model="vm.work.os.iOS8")
li(ng-repeat="os in vm.type.operatingSystems")
checkbox(label="{{os.name}}" ng-model="os.selected")

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

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

li
h4 Design
li(ng-repeat="requestType in vm.type.requestTypes")
h4 {{requestType.name}}

p Lorem ipsum dolor sit amet, consectetur adipiscing elit.

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

selected-button(
img(ng-if="requestType.name == 'Design & Development'" src="http://pbwebdev.com/blog/wp-content/uploads/2014/06/developer.jpg")

button(
type="button"
value="vm.requestTypes.design"
ng-model="vm.work.requestTypes"
selectable="true"
ng-model="vm.work.requestType"
value="requestType.name"
) Design

li
h4 Development

p Lorem ipsum dolor sit amet, consectetur adipiscing elit.

img(src="http://pbwebdev.com/blog/wp-content/uploads/2014/06/developer.jpg")

selected-button(
type="button"
value="vm.requestTypes.code"
ng-model="vm.work.requestTypes"
) Development

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

Expand Down
40 changes: 25 additions & 15 deletions tests/specs/type.controller.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ saveSpy = null

describe 'SubmitWorkTypeController', ->
beforeEach ->
bard.inject this, '$rootScope', '$q', '$controller', 'SubmitWorkAPIService'
bard.inject this, '$rootScope', '$q', '$controller', 'SubmitWorkService'
scope = $rootScope.$new()

work =
Expand All @@ -17,11 +17,13 @@ describe 'SubmitWorkTypeController', ->
promise = $q.when work
_default = $promise: promise

bard.mockService SubmitWorkAPIService, _default: _default
bard.mockService SubmitWorkService,
_default: _default
save: $q.when

controller = $controller 'SubmitWorkTypeController', $scope: scope
scope.vm = controller
saveSpy = sinon.spy controller, 'save'
saveSpy = sinon.spy SubmitWorkService, 'save'

afterEach ->
saveSpy.restore()
Expand All @@ -31,8 +33,8 @@ describe 'SubmitWorkTypeController', ->
expect(controller).to.be.defined

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

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

it 'should call API service with put to save project', ->
it 'should call service to save project', ->
controller.workId = '123'
controller.work.name = 'abc'
controller.work.requestTypes = ['Design', 'Code']
controller.work.summary = 'abc'
controller.type.requestTypes = [
name: 'Design'
selected: true
]
controller.type.devices = [
name: 'iphone'
selected: true
]
controller.type.orientations = [
name: 'landscape'
selected: true
]
controller.type.operatingSystems = [
name: 'ios'
selected: true
]
controller.save()
expect(SubmitWorkAPIService.put.called).to.be.ok

it 'should call API service with post to create new project', ->
controller.work.name = 'abc'
controller.work.requestTypes = ['Design']
controller.work.summary = 'abc'
controller.save()
expect(SubmitWorkAPIService.post.called).to.be.ok
expect(SubmitWorkService.save.called).to.be.ok

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