Filter Panel

PHOTO EMBED

Tue Jun 22 2021 14:58:33 GMT+0000 (UTC)

Saved by @alexUnnamed #angular

  ngAfterViewInit(): void {
    const vendor = this.activatedRoute.snapshot.params.vendor_id;

    combineLatest([
      this.filterChanged$.asObservable().pipe(
        startWith({}),
        tap(() => this.pagination.reset())
      ),
      this.sort.sortChange.pipe(
        startWith({}),
        tap(() => this.pagination.reset())
      ),
      this.pagination.pageChanged.pipe(startWith({})),
    ])
      .pipe(
        tap(() => (this.isLoading = true)),
        map(([filterData]) => {
          let orderFilter: OrderFilterInput = {
            vendor,
          };

          if ('dateFrom' in filterData && 'dateTo' in filterData && 'city' in filterData) {
            const { dateFrom, dateTo, city } = filterData;

            orderFilter = {
              ...orderFilter,
              ...(
                dateFrom && dateTo && {
                  createdRange: {
                    gte: dateFrom,
                    lte: dateTo,
                  }
                }
              ),
              city
            };
          }

          return orderFilter;
        }),
        switchMap((orderFilter) =>
          merge(
            this.updateOrders$
              .asObservable()
              .pipe(map(() => ({ callCount: 1, orderFilter }))),
            timer(0, 30 * 1000).pipe(
              map((callCount) => ({ callCount, orderFilter }))
            )
          )
        ),
        switchMap(({ callCount, orderFilter }) => {
          this.newOrderCheckerService.makeFetch();

          const fetchOrders$ = this.orderService.findAll(
            this.getSortData(),
            callCount === 0
              ? this.pagination.getPaginationData()
              : this.pagination.getCachedPaginationData(),
            orderFilter
          );

          return PaginationComponent.paginationSetter$(this.pagination)(
            () => fetchOrders$
          );
        }),
        tap(({ orders }) => {
          this.orders = orders;
          this.data = this.getGroupedOrders(orders);
          this.isLoading = false;
        }),
        filter(() => this.isAdmin()),
        tap(({ orders }) => {
          if (
            !orders.some((order) => order.cancellationReason?.blinkForAdmin)
          ) {
            return;
          }

          this.audioService.play(AvailableAudio.NOTIFICATION);
        }),
        untilDestroyed(this)
      )
      .subscribe();
  }
content_copyCOPY