diff --git a/src/app/store/actions/bigOrderTable.actions.ts b/src/app/store/actions/bigOrderTable.actions.ts index 3c68a1393a..07930b7f3b 100644 --- a/src/app/store/actions/bigOrderTable.actions.ts +++ b/src/app/store/actions/bigOrderTable.actions.ts @@ -1,12 +1,13 @@ import { createAction, props } from '@ngrx/store'; -import { GetLocations } from 'src/app/store/actions/tariff.actions'; import { IBigOrderTable, + IBigOrderTableOrderInfo, IBigOrderTableParams, IFilter, IFilters, ILocationDetails, - IOrdersViewParameters + IOrdersViewParameters, + NotTakenOutReasonImages } from 'src/app/ubs/ubs-admin/models/ubs-admin.interface'; export enum BigOrderTableActions { @@ -21,6 +22,8 @@ export enum BigOrderTableActions { ChangingOrderData = '[BigOrderTable] Changing Order Data', ChangingOrderPaymentStatus = '[BigOrderTable] Changing Order Payment Status', ChangingOrderDataSuccess = '[BigOrderTable] Changing Order Data Success', + UpdateOrderInfo = '[BigOrderTable] Update Order Info', + UpdateOrderInfoSuccess = '[BigOrderTable] Update Order Info Success', ReceivedFailure = '[BigOrderTable] Received Failure', AddFilters = '[BigOrderTable] Add Filters', @@ -83,6 +86,21 @@ export const ChangingOrderDataSuccess = createAction( props<{ orderId: number[]; columnName: string; newValue: string }>() ); +export const UpdateOrderInfo = createAction( + BigOrderTableActions.UpdateOrderInfo, + props<{ + orderId: number; + updatedOrder: IBigOrderTableOrderInfo; + currentLanguage: string; + notTakenOutReasonImages?: NotTakenOutReasonImages[]; + }>() +); + +export const UpdateOrderInfoSuccess = createAction( + BigOrderTableActions.UpdateOrderInfoSuccess, + props<{ updatedOrder: IBigOrderTableOrderInfo }>() +); + export const AddFiltersAction = createAction(BigOrderTableActions.AddFilters, props<{ filters: IFilters; fetchTable: boolean }>()); export const AddFilterMultiAction = createAction(BigOrderTableActions.AddFilterMulti, props<{ filter: IFilter; fetchTable: boolean }>()); export const RemoveFilter = createAction(BigOrderTableActions.RemoveFilter, props<{ filter: IFilter; fetchTable: boolean }>()); diff --git a/src/app/store/effects/bigOrderTable.effects.ts b/src/app/store/effects/bigOrderTable.effects.ts index 922c275c23..a1fe102e88 100644 --- a/src/app/store/effects/bigOrderTable.effects.ts +++ b/src/app/store/effects/bigOrderTable.effects.ts @@ -5,8 +5,10 @@ import { select, Store } from '@ngrx/store'; import { of } from 'rxjs'; import { catchError, concatMap, map, mergeMap, switchMap, tap, withLatestFrom } from 'rxjs/operators'; import { filtersSelector } from 'src/app/store/selectors/big-order-table.selectors'; +import { MatSnackBarComponent } from '@global-errors/mat-snack-bar/mat-snack-bar.component'; import { IBigOrderTable, + IBigOrderTableOrderInfo, IBigOrderTableParams, ILocationDetails, IOrdersViewParameters @@ -33,7 +35,9 @@ import { RemoveFilter, SaveFiltersAction, SetColumnToDisplay, - SetColumnToDisplaySuccess + SetColumnToDisplaySuccess, + UpdateOrderInfo, + UpdateOrderInfoSuccess } from '../actions/bigOrderTable.actions'; @Injectable() @@ -43,7 +47,8 @@ export class BigOrderTableEffects { private adminTableService: AdminTableService, private orderService: OrderService, private localStorageService: LocalStorageService, - private store: Store + private store: Store, + private snackBar: MatSnackBarComponent ) {} getColumnToDisplay = createEffect(() => { @@ -129,6 +134,29 @@ export class BigOrderTableEffects { ); }); + updateOrderInfo = createEffect(() => + this.actions.pipe( + ofType(UpdateOrderInfo), + mergeMap((action) => + this.orderService.updateOrderInfo(action.orderId, action.currentLanguage, action.updatedOrder, action.notTakenOutReasonImages).pipe( + map((response) => { + if (response.body) { + this.snackBar.openSnackBar('changesSaved'); + return UpdateOrderInfoSuccess({ updatedOrder: response.body }); + } else { + this.snackBar.openSnackBar('error'); + return ReceivedFailure({ error: 'Failed to update order' }); + } + }), + catchError((error) => { + this.snackBar.openSnackBar('error'); + return of(ReceivedFailure({ error })); + }) + ) + ) + ) + ); + addFilter = createEffect( () => this.actions.pipe( diff --git a/src/app/store/reducers/bigOrderTable.reducer.ts b/src/app/store/reducers/bigOrderTable.reducer.ts index c1c0edd2ab..ffa89a3803 100644 --- a/src/app/store/reducers/bigOrderTable.reducer.ts +++ b/src/app/store/reducers/bigOrderTable.reducer.ts @@ -12,8 +12,8 @@ import { RemoveFilter, AddFilterMultiAction, ClearFilters, - GetLocationsDetails, - GetLocationsDetailsSuccess + GetLocationsDetailsSuccess, + UpdateOrderInfoSuccess } from '../actions/bigOrderTable.actions'; import { createReducer, on } from '@ngrx/store'; import { IFilters } from 'src/app/ubs/ubs-admin/models/ubs-admin.interface'; @@ -57,6 +57,21 @@ export const bigOrderTableReducer = createReducer( } })), + on(UpdateOrderInfoSuccess, (state, action) => { + if (!state.bigOrderTable || !state.bigOrderTable.content) { + return state; + } + return { + ...state, + bigOrderTable: { + ...state.bigOrderTable, + content: state.bigOrderTable.content.map((orderData) => + orderData.id === action.updatedOrder.id ? { ...action.updatedOrder } : orderData + ) + } + }; + }), + on(ChangingOrderPaymentStatus, (state, action) => ({ ...state, bigOrderTable: { diff --git a/src/app/ubs/ubs-admin/components/ubs-admin-order-history/ubs-admin-order-history.component.ts b/src/app/ubs/ubs-admin/components/ubs-admin-order-history/ubs-admin-order-history.component.ts index 4618f4f96c..99107757cd 100644 --- a/src/app/ubs/ubs-admin/components/ubs-admin-order-history/ubs-admin-order-history.component.ts +++ b/src/app/ubs/ubs-admin/components/ubs-admin-order-history/ubs-admin-order-history.component.ts @@ -65,7 +65,7 @@ export class UbsAdminOrderHistoryComponent implements OnDestroy, OnChanges, OnIn ngOnChanges(changes: SimpleChanges): void { const orderID = this.orderInfo.generalOrderInfo.id; - if (changes.orderInfo) { + if (changes.orderInfo && this.currentLanguage) { this.getOrderHistory(orderID); this.getNotTakenOutReason(orderID); this.getOrderCancelReason(orderID); diff --git a/src/app/ubs/ubs-admin/components/ubs-admin-order/ubs-admin-order.component.ts b/src/app/ubs/ubs-admin/components/ubs-admin-order/ubs-admin-order.component.ts index 3fdb02945b..6477956cf5 100644 --- a/src/app/ubs/ubs-admin/components/ubs-admin-order/ubs-admin-order.component.ts +++ b/src/app/ubs/ubs-admin/components/ubs-admin-order/ubs-admin-order.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit, OnDestroy, AfterContentChecked, ChangeDetectorRef, ViewChild, HostListener } from '@angular/core'; +import { Component, OnInit, OnDestroy, AfterContentChecked, ChangeDetectorRef, HostListener } from '@angular/core'; import { FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; import { ActivatedRoute, Params, Router } from '@angular/router'; import { formatDate } from '@angular/common'; @@ -7,19 +7,16 @@ import { Observable, Subject } from 'rxjs'; import { take, takeUntil } from 'rxjs/operators'; import { Store } from '@ngrx/store'; import { TranslateService } from '@ngx-translate/core'; -import { MatSnackBarComponent } from '@global-errors/mat-snack-bar/mat-snack-bar.component'; import { UbsAdminCancelModalComponent } from '../ubs-admin-cancel-modal/ubs-admin-cancel-modal.component'; import { OrderService } from '../../services/order.service'; import { LocalStorageService } from '@global-service/localstorage/local-storage.service'; import { - IAddressExportDetails, IEmployee, IExportDetails, IGeneralOrderInfo, IOrderDetails, IOrderInfo, IOrderStatusInfo, - IPaymentInfo, IResponsiblePersons, IUpdateResponsibleEmployee, IUserInfo, @@ -30,16 +27,15 @@ import { ReturnMoneyOrBonuses } from '../../models/ubs-admin.interface'; import { IAppState } from 'src/app/store/state/app.state'; -import { ChangingOrderData } from 'src/app/store/actions/bigOrderTable.actions'; +import { UpdateOrderInfo, UpdateOrderInfoSuccess } from 'src/app/store/actions/bigOrderTable.actions'; import { Patterns } from 'src/assets/patterns/patterns'; -import { GoogleScript } from 'src/assets/google-script/google-script'; import { PhoneNumberValidator } from 'src/app/shared/phone-validator/phone.validator'; -import { OrderStatus, PaymentEnrollment } from 'src/app/ubs/ubs/order-status.enum'; +import { OrderStatus } from 'src/app/ubs/ubs/order-status.enum'; import { UbsAdminEmployeeService } from '../../services/ubs-admin-employee.service'; import { AdminTableService } from '../../services/admin-table.service'; -import { TableKeys } from '../../services/table-keys.enum'; import { UnsavedChangesGuard } from '@ubs/ubs-admin/unsaved-changes-guard.guard'; import { Address } from '@ubs/ubs/models/ubs.interface'; +import { Actions, ofType } from '@ngrx/effects'; @Component({ selector: 'app-ubs-admin-order', @@ -52,7 +48,6 @@ export class UbsAdminOrderComponent implements OnInit, OnDestroy, AfterContentCh private destroy$: Subject = new Subject(); isOrderCancelledAfterFormed: boolean; orderForm: FormGroup; - isDataLoaded = false; orderId: number; orderInfo: IOrderInfo; generalInfo: IGeneralOrderInfo; @@ -96,18 +91,17 @@ export class UbsAdminOrderComponent implements OnInit, OnDestroy, AfterContentCh return this.orderForm.controls.addressExportDetailsDto as FormControl; } constructor( - private translate: TranslateService, - private localStorageService: LocalStorageService, - private adminTableService: AdminTableService, - private fb: FormBuilder, - private dialog: MatDialog, - private route: ActivatedRoute, - private router: Router, - private changeDetector: ChangeDetectorRef, - private store: Store, - private orderService: OrderService, - private matSnackBar: MatSnackBarComponent, - private googleScript: GoogleScript, + private readonly translate: TranslateService, + private readonly localStorageService: LocalStorageService, + private readonly adminTableService: AdminTableService, + private readonly fb: FormBuilder, + private readonly dialog: MatDialog, + private readonly route: ActivatedRoute, + private readonly router: Router, + private readonly changeDetector: ChangeDetectorRef, + private readonly store: Store, + private readonly actions$: Actions, + private readonly orderService: OrderService, public ubsAdminEmployeeService: UbsAdminEmployeeService, public unsavedChangesGuard: UnsavedChangesGuard ) {} @@ -508,66 +502,20 @@ export class UbsAdminOrderComponent implements OnInit, OnDestroy, AfterContentCh this.addIdForUserAndAdress(changedValues); - this.orderService - .updateOrderInfo(this.orderId, this.currentLanguage, changedValues, this.notTakenOutReasonImages) - .pipe(takeUntil(this.destroy$)) - .subscribe((response) => { - this.matSnackBar.openSnackBar(response.ok ? 'changesSaved' : 'error'); - if (response.ok) { - this.getOrderInfo(this.orderId); - if (changedValues?.generalOrderInfo) { - Object.keys(changedValues?.generalOrderInfo).forEach((key: string) => { - if (changedValues.generalOrderInfo[key]) { - this.postDataItem([this.orderId], key, changedValues.generalOrderInfo[key]); - } - }); - } - this.updateExportDataInState(changedValues); - this.updateResponsibleEmployeeInState(changedValues); - } - }); - this.statusCanceledOrDone(); - } - - private updateExportDataInState(changedValues: IOrderInfo) { - if (changedValues?.exportDetailsDto) { - if (this.dateExport) { - this.postDataItem([this.orderId], TableKeys.dateOfExport, this.dateExport); - } - if (this.receivingStationId) { - this.postDataItem([this.orderId], TableKeys.receivingStation, String(this.receivingStationId)); - } - if (this.timeDeliveryFrom && this.timeDeliveryTo) { - this.postDataItem([this.orderId], TableKeys.timeOfExport, [this.timeDeliveryFrom, this.timeDeliveryTo].join('-')); - } - } - } + this.store.dispatch( + UpdateOrderInfo({ + orderId: this.orderId, + updatedOrder: changedValues, + currentLanguage: this.currentLanguage, + notTakenOutReasonImages: this.notTakenOutReasonImages + }) + ); - private updateResponsibleEmployeeInState(changedValues: IOrderInfo) { - if (changedValues?.updateResponsibleEmployeeDto) { - changedValues?.updateResponsibleEmployeeDto.forEach((key) => { - switch (key.positionId) { - case 2: - this.postDataItem([this.orderId], TableKeys.responsibleCaller, String(key.employeeId)); - break; - case 3: - this.postDataItem([this.orderId], TableKeys.responsibleLogicMan, String(key.employeeId)); - break; - case 4: - this.postDataItem([this.orderId], TableKeys.responsibleNavigator, String(key.employeeId)); - break; - case 5: - this.postDataItem([this.orderId], TableKeys.responsibleDriver, String(key.employeeId)); - break; - default: - break; - } - }); - } - } + this.actions$.pipe(ofType(UpdateOrderInfoSuccess), take(1)).subscribe(() => { + this.getOrderInfo(this.orderId); + }); - private postDataItem(orderId: number[], columnName: string, newValue: string): void { - this.store.dispatch(ChangingOrderData({ orderData: [{ orderId, columnName, newValue }] })); + this.statusCanceledOrDone(); } private getUpdates(formItem: FormGroup | FormArray | FormControl, changedValues: IOrderInfo, name?: string) { @@ -582,8 +530,9 @@ export class UbsAdminOrderComponent implements OnInit, OnDestroy, AfterContentCh for (const formControlName in formItem.controls) { if (Object.prototype.hasOwnProperty.call(formItem.controls, formControlName)) { const formControl = formItem.controls[formControlName]; - - if (formControl instanceof FormControl) { + if (formControlName === 'userInfoDto' && formControl.dirty) { + changedValues[formControlName] = formControl.value; + } else if (formControl instanceof FormControl) { this.getUpdates(formControl, changedValues, formControlName); } else if (formControl instanceof FormArray && formControl.dirty && formControl.controls.length > 0) { changedValues[formControlName] = []; diff --git a/src/app/ubs/ubs-admin/components/ubs-admin-table/ubs-admin-table.component.ts b/src/app/ubs/ubs-admin/components/ubs-admin-table/ubs-admin-table.component.ts index 2314bdb8f5..73f00fcb4e 100644 --- a/src/app/ubs/ubs-admin/components/ubs-admin-table/ubs-admin-table.component.ts +++ b/src/app/ubs/ubs-admin/components/ubs-admin-table/ubs-admin-table.component.ts @@ -767,6 +767,7 @@ export class UbsAdminTableComponent implements OnInit, AfterViewChecked, OnDestr } openOrder(id: number): void { + this.adminTableService.blockOrders([id]).subscribe(); this.router .navigate(['ubs-admin', 'order', `${id}`]) .then(() => {}) diff --git a/src/app/ubs/ubs-admin/services/order.service.ts b/src/app/ubs/ubs-admin/services/order.service.ts index 05e1987bbb..d6e77ee9d8 100644 --- a/src/app/ubs/ubs-admin/services/order.service.ts +++ b/src/app/ubs/ubs-admin/services/order.service.ts @@ -1,4 +1,4 @@ -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { @@ -10,7 +10,8 @@ import { ResponsibleEmployee, INotTakenOutReason, NotTakenOutReasonImages, - IOrderInfo + IOrderInfo, + IBigOrderTableOrderInfo } from '../models/ubs-admin.interface'; import { environment } from '@environment/environment'; import { IViolation } from '../models/violation.model'; @@ -97,7 +98,12 @@ export class OrderService { return this.http.get(`${this.backend}/management/get-data-for-order/${orderId}`); } - updateOrderInfo(orderId: number, lang: string, data: object, images?: NotTakenOutReasonImages[]) { + updateOrderInfo( + orderId: number, + lang: string, + data: object, + images?: NotTakenOutReasonImages[] + ): Observable> { const formData: FormData = new FormData(); formData.append('updateOrderPageAdminDto', JSON.stringify(data)); @@ -107,9 +113,13 @@ export class OrderService { }); } - return this.http.patch(`${this.backend}/management/update-order-page-admin-info/${orderId}?language=${lang}`, formData, { - observe: 'response' - }); + return this.http.patch( + `${this.backend}/management/update-order-page-admin-info/${orderId}?language=${lang}`, + formData, + { + observe: 'response' + } + ); } getIsOrderCancelledAfterFormed(orderId: number): Observable {