Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Created June 24, 2015 17:42
Show Gist options
  • Save alexhawkins/b83e3c1e9595a4f9fd6a to your computer and use it in GitHub Desktop.
Save alexhawkins/b83e3c1e9595a4f9fd6a to your computer and use it in GitHub Desktop.
Shipment Details Module, Controller, Routes, Services with ES6
/************************** MODULE ***********************************************************************/
'use strict';
/*jshint esnext: true */
import ShipmentDetailsRoute from './shipment-details.routes';
import ShipmentDetailsCtrl from './shipment-details.controller';
ShipmentDetailsRoute.$inject = ['$stateProvider'];
ShipmentDetailsCtrl.$inject = ['shipmentObj', 'trackingArray', 'notesArray', 'Shipment', '$mdToast', '$stateParams'];
export default angular.module('shiphawkDashboard.shipmentDetails', [])
.config(ShipmentDetailsRoute)
.controller('ShipmentDetailsCtrl', ShipmentDetailsCtrl);
/**************************CONTROLLER***********************************************************************/
export default class ShipmentDetailsCtrl {
constructor(shipmentObj, trackingArray, notesArray, Shipment, $mdToast, $stateParams) {
//------------ expose shipment details to page display ---------//
let tracking = trackingArray.tracking;
let notes = notesArray.notes;
this.shimpentService = Shipment;
this.$mdToast = $mdToast;
this.shipment = shipmentObj.shipment;
this.pickup = this.shipment.pickup;
this.delivery = this.shipment.delivery;
this.billing = this.shipment.billing;
this.packing = this.shipment.packing;
this.shipmentDetails = this.shipment.details;
this.shipmentTracking = tracking.status_updates;
this.shipmentNotes = notes.customer_notes;
this.packages = this.packing.packages;
//--------- set edit mode to be on default false --------//
this.editMode = false;
this.shipmentStatuses = ['Ordered', 'Scheduled for Pickup', 'Confirmed', 'In Prep', 'In Transit', 'Delivered', 'Exception', 'Cancelled'];
/* toast methods and vars */
this.toastPosition = {
bottom: false,
top: true,
center: true
};
//--------- start of initializing functions ---------//
this.activate();
}
activate() {
return this.shimpentService.getBol(this.shipmentDetails.id)
.then((response) => this.handleBolResponse(response));
}
handleBolResponse(response) {
if (response) {
this.shipmentDocuments = [{
name: 'Shipment BOL',
url: response.data.url
}];
} else {
console.debug('error', response);
this.shipmentDocuments = [];
}
}
saveShipment() {
let shipment = this.shipment;
return this.shimpentService.updateStatuses([this.shipment.details.id], this.convertStatus(this.shipmentDetails.status))
.then((response) => {
this.editMode = false;
if (response) {
if (shipment.details.status !== response.status) {
console.debug(response);
this.showSimpleToast('success', 'Status successfully activated');
}
} else {
this.showSimpleToast('error', 'Unable to update status');
this.shipmentDetails.status = shipment.details.status;
}
});
}
convertStatus(status) {
var BACKEND_STATUS = {
'Ordered': 'ordered',
'Confirmed': 'confirmed',
'Scheduled for Pickup': 'scheduled_for_pickup',
'In Prep': 'agent_prep',
'In Transit': 'in_transit',
'Delivered': 'delivered',
'Exception': 'exception',
'Cancelled': 'cancelled'
};
return BACKEND_STATUS[status] || this.shipmentDetails.status;
}
getToastPosition() {
return Object.keys(this.toastPosition).filter((pos) => this.toastPosition[pos]).join(' ');
}
showSimpleToast(type, str) {
this.$mdToast.show({
template: '<md-toast class="md-toast ' + type + '"><span>' + str + '</span></md-toast>',
hideDelay: 3000,
position: this.getToastPosition()
});
}
}
/*********************************ROUTES*************************************************************************/
export default function ShipmentDetailsRoute($stateProvider) {
return $stateProvider
.state('home.shipment-details', {
url: 'shipment/details/:shipmentId',
params: {
shipmentId: {
value: 'defaultValue',
squash: false
}
},
templateUrl: 'app/shipment-details/shipment-details.html',
controller: 'ShipmentDetailsCtrl as vm',
title: 'Shipment Details',
data: {
pageTitle: 'Shipment Details'
},
resolve: {
shipmentObj: (Shipment, $stateParams) => Shipment.getShipment($stateParams.shipmentId),
trackingArray: (Shipment, $stateParams) => Shipment.getTrackingUpdate($stateParams.shipmentId),
notesArray:(Shipment, $stateParams) => Shipment.getNotes($stateParams.shipmentId)
}
});
}
/*******************************SERVICES********************************************************************/
export default class Shipment {
constructor($resource, $state, $mdToast) {
bolBackend = $resource('/api/v3/shipments/:id/bol');
shipmentBackend = $resource('/api/v3/shipments/:id');
trackingBackend = $resource('/api/v3/shipments/:id/tracking');
notesBackend = $resource('/api/v3/shipments/:id/notes');
statusBackend = $resource('/api/v3/shipments/status', null, {
'update': {
method: 'PUT'
}
});
this.$state = $state;
this.$mdToast = $mdToast;
}
getBol(shipment){
console.log('shipment blo', shipment );
var data = shipment ? { 'id': shipment } : null;
if(data){
return bolBackend.get(data).$promise.then((response) => {
return response;
}).catch((error) => {
return false;
});
} else {
return false;
}
}
getShipment(shipment) {
var data = shipment ? { 'id': shipment } : null;
if(data){
return shipmentBackend.get(data).$promise.then((response) => {
return { 'shipment': response };
}).catch((error) => {
console.log('error');
this.$state.go('home.shipment-history');
return { 'shipment': '' };
});
} else {
return { 'shipment': ''};
}
}
getTrackingUpdate(shipment) {
var data = shipment ? { 'id': shipment } : null;
if(data){
return trackingBackend.get(data).$promise.then((response) => {
console.log('track', response);
return { 'tracking': response };
}).catch((error) => {
return { 'tracking': '' };
});
} else {
return { 'tracking': ''};
}
}
getNotes(shipment) {
var data = shipment ? { 'id': shipment } : null;
if(data){
return notesBackend.get(data).$promise.then((response) => {
return { 'notes': response };
}).catch((error) => {
return { 'notes': '' };
});
} else {
return { 'notes': ''};
}
}
postNotes(shipment, notes) {
var data = shipment ? { 'id': shipment } : null;
if(data && notes){
return notesBackend.save(data, notes).$promise.then((response) => {
return { 'notes': response };
}).catch((error) => {
return { 'notes': '' };
});
} else {
return { 'notes': ''};
}
}
cancelShipment(shipment) {
var data = shipment ? { 'id': shipment } : null;
if(data){
return shipmentBackend.delete(data).$promise;
} else {
return { 'shipment': ''};
}
}
updateStatuses(ids, status) {
var data = {
'shipment_ids': ids,
'status': status
};
return statusBackend.update(data).$promise.then((response) => {
return response;
}).catch((error) => {
console.log('error', error);
return false;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment