1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
1
2
import { Component , OnInit , ViewEncapsulation } from '@angular/core' ;
2
3
import { NgIf } from '@angular/common' ;
3
4
import { FormControl , FormGroup , Validators , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
@@ -11,7 +12,7 @@ import {ProgressSpinnerModule} from 'primeng/progressspinner';
11
12
import { AppConfig } from '@configs' ;
12
13
import { AuthService } from '@core/auth' ;
13
14
import { LoadStatus } from '@core/enums' ;
14
- import { CreateLoad } from '@core/models' ;
15
+ import { CreateLoad , Customer } from '@core/models' ;
15
16
import { ApiService , ToastService } from '@core/services' ;
16
17
import { DistanceConverter } from '@shared/utils' ;
17
18
import {
@@ -20,7 +21,7 @@ import {
20
21
RouteChangedEvent ,
21
22
SelectedAddressEvent ,
22
23
} from '@shared/components' ;
23
- import { TruckData , TruckHelper } from '../shared ' ;
24
+ import { SearchCustomerComponent , SearchTruckComponent , TruckData } from '../components ' ;
24
25
25
26
26
27
@Component ( {
@@ -42,27 +43,33 @@ import {TruckData, TruckHelper} from '../shared';
42
43
RouterLink ,
43
44
AddressAutocompleteComponent ,
44
45
DirectionsMapComponent ,
46
+ SearchCustomerComponent ,
47
+ SearchTruckComponent ,
45
48
] ,
46
49
} )
47
50
export class AddLoadComponent implements OnInit {
48
51
public readonly accessToken : string ;
49
52
private distanceMeters : number ;
50
53
51
- public isBusy : boolean ;
54
+ public isLoading : boolean ;
52
55
public form : FormGroup ;
53
- public suggestedDrivers : TruckData [ ] ;
56
+ public selectedTruck : TruckData | null ;
57
+ public selectedCustomer : Customer | null ;
58
+ public suggestedCustomers : Customer [ ] ;
54
59
public originCoords ?: [ number , number ] | null ;
55
60
public destinationCoords ?: [ number , number ] | null ;
56
61
57
62
constructor (
58
- private authService : AuthService ,
59
- private apiService : ApiService ,
60
- private toastService : ToastService ,
61
- private router : Router )
63
+ private readonly authService : AuthService ,
64
+ private readonly apiService : ApiService ,
65
+ private readonly toastService : ToastService ,
66
+ private readonly router : Router )
62
67
{
63
68
this . accessToken = AppConfig . mapboxToken ;
64
- this . isBusy = false ;
65
- this . suggestedDrivers = [ ] ;
69
+ this . isLoading = false ;
70
+ this . selectedTruck = null ;
71
+ this . selectedCustomer = null ;
72
+ this . suggestedCustomers = [ ] ;
66
73
this . distanceMeters = 0 ;
67
74
68
75
this . form = new FormGroup ( {
@@ -76,7 +83,6 @@ export class AddLoadComponent implements OnInit {
76
83
distance : new FormControl ( 0 , Validators . required ) ,
77
84
dispatcherName : new FormControl ( '' , Validators . required ) ,
78
85
dispatcherId : new FormControl ( '' , Validators . required ) ,
79
- assignedTruck : new FormControl ( '' , Validators . required ) ,
80
86
status : new FormControl ( LoadStatus . Dispatched , Validators . required ) ,
81
87
} ) ;
82
88
}
@@ -85,21 +91,6 @@ export class AddLoadComponent implements OnInit {
85
91
this . fetchCurrentDispatcher ( ) ;
86
92
}
87
93
88
- searchTruck ( event : any ) {
89
- TruckHelper . findTruckDrivers ( this . apiService , event . query ) . subscribe ( ( drivers ) => this . suggestedDrivers = drivers ) ;
90
- }
91
-
92
- submit ( ) {
93
- const assignedTruck = this . form . value . assignedTruck ;
94
-
95
- if ( ! assignedTruck ) {
96
- this . toastService . showError ( 'Select a truck' ) ;
97
- return ;
98
- }
99
-
100
- this . createLoad ( ) ;
101
- }
102
-
103
94
updateOrigin ( eventData : SelectedAddressEvent ) {
104
95
this . originCoords = eventData . center ;
105
96
this . form . patchValue ( {
@@ -120,9 +111,12 @@ export class AddLoadComponent implements OnInit {
120
111
this . form . patchValue ( { distance : distanceMiles } ) ;
121
112
}
122
113
123
- private createLoad ( ) {
124
- this . isBusy = true ;
114
+ createLoad ( ) {
115
+ if ( ! this . isValid ( ) ) {
116
+ return ;
117
+ }
125
118
119
+ this . isLoading = true ;
126
120
const command : CreateLoad = {
127
121
name : this . form . value . name ,
128
122
originAddress : this . form . value . orgAddress ,
@@ -134,18 +128,44 @@ export class AddLoadComponent implements OnInit {
134
128
deliveryCost : this . form . value . deliveryCost ,
135
129
distance : this . distanceMeters ,
136
130
assignedDispatcherId : this . form . value . dispatcherId ,
137
- assignedTruckId : this . form . value . assignedTruck . truckId ,
131
+ assignedTruckId : this . selectedTruck ! . truckId ,
132
+ customerId : this . form . value . customer . id
138
133
} ;
134
+
139
135
140
136
this . apiService . createLoad ( command )
141
- . subscribe ( ( result ) => {
142
- if ( result . isSuccess ) {
143
- this . toastService . showSuccess ( 'A new load has been created successfully' ) ;
144
- this . router . navigateByUrl ( '/load/list' ) ;
145
- }
146
-
147
- this . isBusy = false ;
148
- } ) ;
137
+ . subscribe ( ( result ) => {
138
+ if ( result . isSuccess ) {
139
+ this . toastService . showSuccess ( 'A new load has been created successfully' ) ;
140
+ this . router . navigateByUrl ( '/loads' ) ;
141
+ }
142
+
143
+ this . isLoading = false ;
144
+ } ) ;
145
+ }
146
+
147
+ private isValid ( ) : boolean {
148
+ if ( ! this . selectedTruck ) {
149
+ this . toastService . showError ( 'Please select the truck' ) ;
150
+ return false ;
151
+ }
152
+
153
+ if ( ! this . selectedCustomer ) {
154
+ this . toastService . showError ( 'Please select the customer' ) ;
155
+ return false ;
156
+ }
157
+
158
+ if ( ! this . form . value . orgAddress ) {
159
+ this . toastService . showError ( 'Please select the origin address' ) ;
160
+ return false ;
161
+ }
162
+
163
+ if ( ! this . form . value . dstAddress ) {
164
+ this . toastService . showError ( 'Please select the destination address' ) ;
165
+ return false ;
166
+ }
167
+
168
+ return true ;
149
169
}
150
170
151
171
private fetchCurrentDispatcher ( ) {
0 commit comments