Skip to content

Instantly share code, notes, and snippets.

@tamebadger
Last active September 16, 2015 12:10
Show Gist options
  • Save tamebadger/cbeeda332378b0239957 to your computer and use it in GitHub Desktop.
Save tamebadger/cbeeda332378b0239957 to your computer and use it in GitHub Desktop.
Before refactoring promises, and also making logic more concise, getting code closer to spec
import Ember from 'ember';
const { Controller, computed, inject, RSVP } = Ember;
import Constants from '../../../utils/constants';
const { checklistTypes } = Constants;
export default Controller.extend({
bottomBarSubmit: computed('model.checklist.checklistType',function(){
return this.get('model.checklist.checklistType') == checklistTypes.PLANT ? true : false;
}),
plant: inject.controller('plant/edit'),
topPlant: inject.controller('plant'),
filteredItems: computed('[email protected]',function(){
let items = this.get('model.items');
if(items){
return items
.map((item)=>{
item.selectedValue = '';
return item;
})
.filter(x => x.get('checklist') == this.get('model.checklist_id'));
} else{
return Ember.A();
}
}),
saveAllChecklistItems(plant){
return RSVP.Promise.all(
this.get('filteredItems')
.map((item)=>{
return this.store.createRecord('plant-checklist',{
plant: plant.get('id'),
checkListItem: item.get('id'),
checkListValue: item.get('selectedValue')
}).save();
})
);
},
acceptTransfer(){
this.get('topPlant').set('transferPending',false);
let currentRequest = this.get('topPlant.currentRequest');
currentRequest.set('transferAccepted',true);
return currentRequest.save()
},
rejectTransfer(){
let currentRequest = this.get('topPlant.currentRequest');
currentRequest.set('transferAccepted',false);
return currentRequest.save()
},
transferPlantToSite(plant){
plant.set('site',this.get('session.site.id'));
return plant.save();
},
allocate(plant){
return this.store
.createRecord('allocation',{
plant: plant,
site: this.get('session.site'),
fromUser: null,
toUser: this.get('session.secure.user'),
odoMeter: plant.get('odoMeter'),
hourMeter: plant.get('hourMeter'),
timestamp: Date.now()
})
.save();
},
handleError(error){
console.log('error:',error);
this.navService.openDialog({message:error});
this.transitionAway();
},
transitionAway(){
this.navService.stopLoader();
this.transitionTo('plant.edit');
},
actions:{
accept(){
this.navService.startLoader();
let plant = this.get('topPlant.currentRequest.plant');
saveAllChecklistItems(plant)
.then(()=>{
return this.acceptTransfer();
})
.then(()=>{
return this.transferPlantToSite(plant);
})
.then(()=>{
return this.allocate(plant);
})
.then(()=>{
this.transitionAway();
})
.catch(this.handleError);
},
reject(){
this.navService.startLoader();
this.rejectTransfer()
.then(()=>{this.transitionAway()})
.catch(this.handleError);
},
submit(){
this.navService.startLoader();
this.saveAllChecklistItems(this.get('plant.model'))
.then(()=>{this.transitionAway()})
.catch(this.handleError);
}
}
});
import Ember from 'ember';
const { Controller, computed, inject, RSVP } = Ember;
import Constants from '../../../utils/constants';
const { checklistTypes } = Constants;
export default Controller.extend({
bottomBarSubmit: computed('model.checklist.checklistType',function(){
return this.get('model.checklist.checklistType') == checklistTypes.PLANT ? true : false;
}),
plant: inject.controller('plant/edit'),
topPlant: inject.controller('plant'),
transfer: inject.controller('plant/edit/transfer'),
filteredItems: computed('[email protected]',function(){
let items = this.get('model.items');
if(items){
let mapped = items.map((item)=>{
item.selectedValue = '';
return item;
});
return mapped.filter(x => x.get('checklist')== this.get('model.checklist_id'));
} else{
return Ember.A();
}
}),
actions:{
accept(){
let currentRequest = this.get('transfer.currentRequest');
let plant = currentRequest.get('plant');
RSVP.Promise.all(
this.get('filteredItems')
.map((item)=>{
return this.store.createRecord('plant-checklist',{
plant: plant.get('id'),
checkListItem: item.get('id'),
checkListValue: item.get('selectedValue')
}).save();
})
)
.then((results)=>{
this.navService.startLoader();
currentRequest.set('transferAccepted',true);
currentRequest.save().then(()=>{
//create new allocation here
//on accepting transfer create new Allocations record and SET Allocations->fromUserId = NULL && toUserId = currentUserId
//&& Plant->SiteId = currentSiteId
let currentSite = this.get('session.site');
plant.set('site',currentSite.get('id'));
plant.save()
.then(()=>{
this.store.createRecord('allocation',{
plant: plant,
site: currentSite,
fromUser: null,
toUser: this.get('session.secure.user'),
odoMeter: plant.get('odoMeter'),
hourMeter: plant.get('hourMeter'),
timestamp: Date.now()
})
.save()
.then(()=>{
this.navService.stopLoader();
this.get('topPlant').set('transferPending',false);
this.transitionTo('plant.edit');
})
.catch((error)=>{
this.navService.stopLoader();
console.log('error:',error);
this.navService.openDialog({message:error});
this.transitionTo('plant.edit');
});
}).catch((error)=>{
this.navService.stopLoader();
console.log('error:',error);
this.navService.openDialog({message:error});
this.transitionTo('plant.edit');
});
});
})
.catch((error)=>{
this.navService.stopLoader();
console.log('saved, error:',error);
this.navService.openDialog({message:error});
this.transitionTo('plant.edit');
});
},
reject(){
this.navService.startLoader();
let currentRequest = this.get('transfer.currentRequest');
currentRequest.set('transferAccepted',false);
currentRequest.save()
.then(()=>{this.transitionTo('plant.edit');})
.catch((error)=>{
console.log('error:',error);
this.navService.openDialog({message:error});
this.transitionTo('plant.edit');
});
},
submit(){
let plant = this.get('plant.model');
RSVP.Promise.all(
this.get('filteredItems')
.map((item)=>{
return this.store.createRecord('plant-checklist',{
plant: plant.get('id'),
checkListItem: item.get('id'),
checkListValue: item.get('selectedValue')
}).save();
})
)
.then(()=>{
this.transitionTo('plant.edit');
}).catch((error)=>{
this.navService.openDialog({message:error});
console.log('saved, error:',error);
this.transitionTo('plant.edit');
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment