File

src/app/transfer/transfer.page.ts

Implements

OnInit

Metadata

selector app-transfer
styleUrls ./transfer.page.scss
templateUrl ./transfer.page.html

Index

Properties
Methods

Constructor

constructor(scanner: BarcodeScanner, swal: SwalService, bookService: BookService, userService: UserService)
Parameters :
Name Type Optional
scanner BarcodeScanner No
swal SwalService No
bookService BookService No
userService UserService No

Methods

Async ngOnInit
ngOnInit()
Returns : any

Properties

acceptanceStatus
Type : object
Default value : { img_url: '', title: 'Rejected' }
borrowings
Type : Borrowing[]
dataSource
Type : MatTableDataSource<Borrowing>
displayedColumns
Type : string[]
Default value : ['title', 'status']
getTransferDocument
Default value : () => {...}
moveHeader
Default value : () => {...}

Move the stepper to next level

scanForQR
Default value : () => {...}
stepper
Type : MatStepper
Decorators :
@ViewChild('stepper')
subscribeToTransferPage
Default value : () => {...}
transferReqeustDocument
import { Component, OnInit, ViewChild } from '@angular/core';
import { BarcodeScanner } from '@ionic-native/barcode-scanner/ngx';
import { SwalService } from '../services/swal/swal.service';
import { BookService } from '../services/book/book.service';
import { MatStepper } from '@angular/material/stepper';
import { UserService } from '../services/user/user.service';
import { Borrowing } from '../models/Borrowings';
import { MatTableDataSource } from '@angular/material/table';

@Component({
  selector: 'app-transfer',
  templateUrl: './transfer.page.html',
  styleUrls: ['./transfer.page.scss']
})
export class TransferPage implements OnInit {
  @ViewChild('stepper') stepper: MatStepper;
  borrowings: Borrowing[];
  displayedColumns: string[] = ['title', 'status'];
  dataSource: MatTableDataSource<Borrowing>;
  transferReqeustDocument;
  acceptanceStatus = {
    img_url: '',
    title: 'Rejected'
  };

  constructor(
    private scanner: BarcodeScanner,
    private swal: SwalService,
    private bookService: BookService,
    private userService: UserService
  ) {}

  async ngOnInit() {
    await this.userService.getUserBorrowings();
    this.borrowings = this.userService.getAllBorrowings().map((borrow: Borrowing) => {
      const { date_due } = borrow;
      if (new Date(date_due) <= new Date()) {
        borrow.status = 'overdue';
      }
      return borrow;
    });
    this.userService.getCurrentTransferDetails().subscribe((data: Borrowing[]) => {
      this.dataSource = new MatTableDataSource(data);
    });
  }

  getTransferDocument = () => {
    console.log('hjjh');
    const transferDocPath = this.bookService.getCurrentActiveTransfer().ref.path;
    return transferDocPath;
  };

  scanForQR = () => {
    this.scanner
      .scan()
      .then(data => {
        this.bookService.acceptBookTransfer(data.text);
        alert(data.text);
      })
      .catch(error => {
        console.log('error occured while scanning QR', error);
        this.swal.viewErrorMessage(
          'Error',
          'You have to be on your iOS or android version inorder to access this service'
        );
      });
  };

  /**
   * Move the stepper to next level
   */
  moveHeader = () => {
    this.stepper.selectedIndex = this.stepper.selectedIndex + 1;
  };

  subscribeToTransferPage = () => {
    // Setting the transfer request document path to variable for a QR
    this.transferReqeustDocument = this.getTransferDocument();

    // Subscribing for the active transfer document changes and if approved by the user, view will be changed to the next step
    const activeTransferObservable = this.bookService.getCurrentActiveTransfer();
    if (activeTransferObservable !== undefined) {
      this.bookService
        .getCurrentActiveTransfer()
        .valueChanges()
        .subscribe((data: Borrowing) => {
          const { status } = data;

          /**
           * Checking whether the transfer is success or not
           * This means if the library approved the transaction it produce 'approved' state and if rejected it produce 'toBeApproved' state
           */
          if (status === 'toBeApproved') {
            this.moveHeader();
            this.acceptanceStatus.img_url = '../../assets/img/backgrounds/transfer-pending.svg';
            this.acceptanceStatus.title = 'Checking';
          } else if (status === 'approved') {
            this.acceptanceStatus.title = 'Approved';
            this.acceptanceStatus.img_url = '../../assets/img/backgrounds/transfer-success.svg';
          } else if (status === 'rejected') {
            this.acceptanceStatus.title = 'Rejected';
            this.acceptanceStatus.img_url = '../../assets/img/backgrounds/transfer-rejected.svg';
          } else {
            this.acceptanceStatus.img_url = '../../assets/img/backgrounds/transfer-scan.svg';
            this.acceptanceStatus.title = 'Scan QR';
          }
        });
    }
  };
}
<app-menu-title title="Transfer book"></app-menu-title>

<ion-content class="ion-padding">
  <mat-horizontal-stepper linear #stepper>
    <mat-step>
      <ng-template matStepLabel>Select a book for transfer</ng-template>
      <!-- Skeleton view -->
      <div *ngIf="!borrowings">
        <ion-list>
          <ion-item *ngFor="let item of [1, 2, 3, 4, 5, 6, 7, 8, 9]">
            <ion-thumbnail slot="start">
              <ion-skeleton-text animated></ion-skeleton-text>
            </ion-thumbnail>
            <ion-label>
              <h3>
                <ion-skeleton-text animated style="width: 50%"></ion-skeleton-text>
              </h3>
              <p>
                <ion-skeleton-text animated style="width: 80%"></ion-skeleton-text>
              </p>
              <p>
                <ion-skeleton-text animated style="width: 60%"></ion-skeleton-text>
              </p>
            </ion-label>
          </ion-item>
        </ion-list>
      </div>
      <!-- End of skelton view -->
      <item-transfer
        [moveHeader]="moveHeader"
        [subscribeToTransferReq]="subscribeToTransferPage"
        *ngFor="let borrowing of borrowings"
        [borrowDetails]="borrowing"
      ></item-transfer>
    </mat-step>
    <mat-step>
      <ng-template matStepLabel>Done</ng-template>
      <ion-card id="qrcode">
        <qrcode
          [qrdata]="transferReqeustDocument"
          [level]="'M'"
          *ngIf="stepper.selectedIndex == 1"
        ></qrcode>
      </ion-card>
      <div>
        <button mat-button matStepperPrevious>Back</button>
        <button mat-button (click)="stepper.reset()">Reset</button>
        <button mat-button (click)="moveHeader()" type="button">Next</button>
      </div>
    </mat-step>
    <mat-step>
      <ng-template matStepLabel>Done</ng-template>
      <app-animated-text [text]="acceptanceStatus.title"></app-animated-text>
      <img [src]="acceptanceStatus.img_url" alt="" id="transfer-status" />
      <div>
        <button mat-button matStepperPrevious>Back</button>
        <button mat-button (click)="stepper.reset()">Reset</button>
      </div>
    </mat-step>
  </mat-horizontal-stepper>
  <ion-button id="scan-button" color="danger" (click)="scanForQR()" size="large">
    <ion-icon slot="start" name="camera"></ion-icon>
    SCAN
  </ion-button>
  <ion-card>
    <ion-card-header>Transfer History</ion-card-header>
    <ion-card-content>
      <table mat-table [dataSource]="dataSource" matSort style="width: 100%">
        <!-- ID Column -->
        <ng-container matColumnDef="title">
          <th mat-header-cell *matHeaderCellDef mat-sort-header>Title</th>
          <td mat-cell *matCellDef="let row">{{ row.title }}</td>
        </ng-container>

        <!-- Progress Column -->
        <ng-container matColumnDef="status">
          <th mat-header-cell *matHeaderCellDef mat-sort-header>Status</th>
          <td mat-cell *matCellDef="let row">
            <ion-badge
              [color]="
                row.status === 'Approved'
                  ? 'primary'
                  : row.status === 'Rejected'
                  ? 'danger'
                  : 'secondary'
              "
              >{{ row.status }}</ion-badge
            >
          </td>
        </ng-container>

        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
      </table>
    </ion-card-content>
  </ion-card>
</ion-content>

./transfer.page.scss

#qrcode {
  max-width: 400px;
  margin: 0 auto;
}

#scan-button {
  position: absolute;
  right: 10px;
  bottom: 10px;
}

@media only screen and (max-width: 1980px) {
  #transfer-status {
    display: block;
    margin: 0 auto;
    width: 60%;
    height: auto;
  }
}

@media only screen and (max-width: 600px) {
  #transfer-status {
    width: 100%;
    height: auto;
  }
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""