@@ -7,7 +7,7 @@ import { not, equal, alias } from '@ember/object/computed';
77import { isArray } from '@ember/array' ;
88import { isBlank } from '@ember/utils' ;
99import { dasherize } from '@ember/string' ;
10- import { later , next } from '@ember/runloop' ;
10+ import { next } from '@ember/runloop' ;
1111import { task } from 'ember-concurrency-decorators' ;
1212import { OSRMv1 , Control as RoutingControl } from '@fleetbase/leaflet-routing-machine' ;
1313import { debug } from '@ember/debug' ;
@@ -295,7 +295,7 @@ export default class OperationsOrdersIndexNewController extends BaseController {
295295 } ) ;
296296 }
297297
298- @action createOrder ( ) {
298+ @action async createOrder ( ) {
299299 if ( ! this . isValid ) {
300300 return ;
301301 }
@@ -347,34 +347,24 @@ export default class OperationsOrdersIndexNewController extends BaseController {
347347 this . isCreatingOrder = true ;
348348
349349 try {
350- return order
351- . save ( )
352- . then ( ( order ) => {
353- // trigger event that fleet-ops created an order
354- this . universe . trigger ( 'fleet-ops.order.created' , order ) ;
355-
356- // transition to order view
357- return this . hostRouter . transitionTo ( `console.fleet-ops.operations.orders.index.view` , order ) . then ( ( ) => {
358- this . notifications . success ( this . intl . t ( 'fleet-ops.operations.orders.index.new.success-message' , { orderId : order . public_id } ) ) ;
359- this . loader . removeLoader ( ) ;
360- this . resetForm ( ) ;
361- later (
362- this ,
363- ( ) => {
364- this . hostRouter . refresh ( ) ;
365- } ,
366- 100
367- ) ;
368- } ) ;
369- } )
370- . catch ( ( error ) => {
371- this . isCreatingOrder = false ;
372- this . notifications . serverError ( error ) ;
373- this . loader . removeLoader ( ) ;
374- } ) ;
350+ const createdOrder = await order . save ( ) ;
351+
352+ // trigger event that fleet-ops created an order
353+ this . universe . trigger ( 'fleet-ops.order.created' , createdOrder ) ;
354+
355+ // transition to order view
356+ await this . hostRouter . transitionTo ( 'console.fleet-ops.operations.orders.index.view' , createdOrder ) ;
357+
358+ // order created successfully
359+ this . notifications . success ( this . intl . t ( 'fleet-ops.operations.orders.index.new.success-message' , { orderId : createdOrder . public_id } ) ) ;
360+ this . loader . removeLoader ( ) ;
361+ this . resetForm ( ) ;
375362 } catch ( error ) {
376- this . notifications . error ( error . message ) ;
363+ debug ( `Error Creating Order : ${ error . message } ` ) ;
364+ this . notifications . serverError ( error ) ;
377365 this . loader . removeLoader ( ) ;
366+ } finally {
367+ this . isCreatingOrder = false ;
378368 }
379369 }
380370
@@ -601,8 +591,8 @@ export default class OperationsOrdersIndexNewController extends BaseController {
601591 setProperties ( payload , { type : this . order . type } ) ;
602592 }
603593
604- this . fetch
605- . post ( 'service-quotes/preliminary' , {
594+ try {
595+ const serviceQuotes = await this . fetch . post ( 'service-quotes/preliminary' , {
606596 payload : this . _getSerializedPayload ( payload ) ,
607597 distance,
608598 time,
@@ -611,20 +601,18 @@ export default class OperationsOrdersIndexNewController extends BaseController {
611601 facilitator,
612602 scheduled_at,
613603 is_route_optimized,
614- } )
615- . then ( ( serviceQuotes ) => {
616- set ( this , 'serviceQuotes' , isArray ( serviceQuotes ) ? serviceQuotes : [ ] ) ;
617-
618- if ( this . serviceQuotes . length && this . isUsingIntegratedVendor ) {
619- set ( this , 'selectedServiceQuote' , this . serviceQuotes . firstObject ?. uuid ) ;
620- }
621- } )
622- . catch ( ( ) => {
623- this . notifications . warning ( this . intl . t ( 'fleet-ops.operations.orders.index.new.service-warning' ) ) ;
624- } )
625- . finally ( ( ) => {
626- this . isFetchingQuotes = false ;
627604 } ) ;
605+
606+ set ( this , 'serviceQuotes' , isArray ( serviceQuotes ) ? serviceQuotes : [ ] ) ;
607+
608+ if ( this . serviceQuotes . length && this . isUsingIntegratedVendor ) {
609+ set ( this , 'selectedServiceQuote' , this . serviceQuotes . firstObject ?. uuid ) ;
610+ }
611+ } catch {
612+ this . notifications . warning ( this . intl . t ( 'fleet-ops.operations.orders.index.new.service-warning' ) ) ;
613+ } finally {
614+ this . isFetchingQuotes = false ;
615+ }
628616 }
629617
630618 _getSerializedPayload ( payload ) {
@@ -1096,6 +1084,9 @@ export default class OperationsOrdersIndexNewController extends BaseController {
10961084 const selectedServiceRate = undefined ;
10971085 const selectedServiceQuote = undefined ;
10981086 const servicable = false ;
1087+ const customFieldGroups = [ ] ;
1088+ const customFields = [ ] ;
1089+ const customFieldValues = { } ;
10991090
11001091 this . removeRoutingControlPreview ( ) ;
11011092 this . removeOptimizedRoute ( ) ;
@@ -1115,6 +1106,9 @@ export default class OperationsOrdersIndexNewController extends BaseController {
11151106 selectedServiceQuote,
11161107 selectedServiceRate,
11171108 servicable,
1109+ customFieldGroups,
1110+ customFields,
1111+ customFieldValues,
11181112 } ) ;
11191113 this . resetInterface ( ) ;
11201114 }
@@ -1139,6 +1133,8 @@ export default class OperationsOrdersIndexNewController extends BaseController {
11391133 * @task
11401134 */
11411135 @task * loadCustomFields ( orderConfig ) {
1136+ this . store . unloadAll ( 'custom-field' ) ;
1137+
11421138 this . customFieldGroups = yield this . store . query ( 'category' , { owner_uuid : orderConfig . id , for : 'custom_field_group' } ) ;
11431139 this . customFields = yield this . store . query ( 'custom-field' , { subject_uuid : orderConfig . id } ) ;
11441140 this . groupCustomFields ( ) ;
@@ -1149,15 +1145,20 @@ export default class OperationsOrdersIndexNewController extends BaseController {
11491145 * Organizes custom fields into their respective groups.
11501146 */
11511147 groupCustomFields ( ) {
1148+ const customFieldGroups = [ ] ;
11521149 for ( let i = 0 ; i < this . customFieldGroups . length ; i ++ ) {
11531150 const group = this . customFieldGroups [ i ] ;
11541151 group . set (
11551152 'customFields' ,
11561153 this . customFields . filter ( ( customField ) => {
1154+ customField . set ( 'value' , null ) ;
11571155 return customField . category_uuid === group . id ;
11581156 } )
11591157 ) ;
1158+ customFieldGroups . push ( group ) ;
11601159 }
1160+
1161+ this . customFieldGroups = customFieldGroups ;
11611162 }
11621163
11631164 @action setCustomFieldValue ( value , customField ) {
@@ -1192,19 +1193,21 @@ export default class OperationsOrdersIndexNewController extends BaseController {
11921193 // this.order.set('facilitator_type', `fleet-ops:${model.facilitator_type}`);
11931194 this . order . set ( 'driver' , null ) ;
11941195
1195- this . isUsingIntegratedVendor = model . isIntegratedVendor ;
1196- this . servicable = model . isIntegratedVendor ;
1196+ this . isUsingIntegratedVendor = model ? .isIntegratedVendor ;
1197+ this . servicable = model ? .isIntegratedVendor ;
11971198
1198- if ( model . service_types ?. length ) {
1199+ if ( model ? .service_types ?. length ) {
11991200 this . integratedVendorServiceType = model . service_types . firstObject . key ;
12001201 }
12011202
1202- if ( model . isIntegratedVendor ) {
1203+ if ( model ? .isIntegratedVendor ) {
12031204 this . getQuotes ( ) ;
12041205 }
12051206
12061207 if ( model ) {
12071208 this . driversQuery = { facilitator : model . id } ;
1209+ } else {
1210+ this . driversQuery = { } ;
12081211 }
12091212 }
12101213
@@ -1525,12 +1528,17 @@ export default class OperationsOrdersIndexNewController extends BaseController {
15251528 confirm : async ( modal ) => {
15261529 modal . startLoading ( ) ;
15271530
1528- const pendingFileUpload = modal . getOption ( 'pendingFileUpload' ) ;
1529- return entity . save ( ) . then ( ( ) => {
1531+ try {
1532+ const pendingFileUpload = modal . getOption ( 'pendingFileUpload' ) ;
1533+ const savedEntity = await entity . save ( ) ;
15301534 if ( pendingFileUpload ) {
15311535 return modal . invoke ( 'uploadNewPhoto' , pendingFileUpload ) ;
15321536 }
1533- } ) ;
1537+
1538+ return savedEntity ;
1539+ } catch ( error ) {
1540+ this . notifications . serverError ( error ) ;
1541+ }
15341542 } ,
15351543 } ) ;
15361544 }
0 commit comments