refactor: Modal in own component

This commit is contained in:
eneller
2026-08-02 10:37:20 +02:00
parent b47cc5e90e
commit 64fa5eb54d
10 changed files with 98 additions and 16 deletions

View File

@@ -0,0 +1,7 @@
<div class="modal-header">
<h4 class="modal-title" id="modal-basic-title">{{ title }}</h4>
<button type="button" class="btn-close" aria-label="Close" (click)="activeModal.dismiss()"></button>
</div>
<div class="modal-body text-center">
<ng-container *ngTemplateOutlet="body"></ng-container>
</div>

View File

@@ -0,0 +1,22 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Modal } from './modal';
describe('Modal', () => {
let component: Modal;
let fixture: ComponentFixture<Modal>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [Modal],
}).compileComponents();
fixture = TestBed.createComponent(Modal);
component = fixture.componentInstance;
await fixture.whenStable();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -0,0 +1,19 @@
import { NgTemplateOutlet } from '@angular/common';
import { Component, Input, TemplateRef } from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'app-modal',
imports: [NgTemplateOutlet],
templateUrl: './modal.html',
styleUrl: './modal.less',
})
export class Modal {
@Input({required: true}) title!: string;
@Input({required: true}) body!: TemplateRef<any>;
constructor(
public activeModal: NgbActiveModal
){}
}

View File

@@ -38,19 +38,13 @@
<!-- Share Button -->
<ng-template #content let-modal>
<div class="modal-header">
<h4 class="modal-title" id="modal-basic-title">Pay {{ amount}} to {{ this.api.currentUser.id }}</h4>
<button type="button" class="btn-close" aria-label="Close" (click)="modal.dismiss()"></button>
</div>
<div class="modal-body text-center">
<qr-code [value]="shareableLink"
size="300"
errorCorrectionLevel="M" />
</div>
<ng-template #qrTemplate>
<qr-code [value]="shareableLink"
size="300"
errorCorrectionLevel="M" />
</ng-template>
<div class="d-grid gap-2">
<button class="btn btn-primary btn-lg" (click)="open(content)">
<button class="btn btn-primary btn-lg" (click)="openModal()">
<i class="bi bi-qr-code-scan me-2"></i> Show QR Code
</button>
</div>

View File

@@ -1,8 +1,9 @@
import { Component, inject, TemplateRef } from '@angular/core';
import { Component, inject, TemplateRef, ViewChild } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { QrCodeComponent } from 'ng-qrcode';
import { APIService } from '../../services/api';
import { Modal } from '../../components/modal/modal';
@Component({
selector: 'app-screen-receive',
@@ -13,6 +14,7 @@ import { APIService } from '../../services/api';
export class ScreenReceive {
private modalService = inject(NgbModal);
api = inject(APIService);
@ViewChild('qrTemplate') qrTemplate !: TemplateRef<any>;
amount: number = 0;
get shareableLink(): string {
@@ -23,7 +25,9 @@ export class ScreenReceive {
copyLink() {
navigator.clipboard.writeText(this.shareableLink);
}
open(content: TemplateRef<any>) {
this.modalService.open(content)
openModal() {
const modalRef = this.modalService.open(Modal);
modalRef.componentInstance.title = `Pay ${this.amount} to ${this.api.currentUser.id}`
modalRef.componentInstance.body = this.qrTemplate;
}
}

View File

@@ -31,6 +31,7 @@
placeholder="username"
[(ngModel)]="recipient"
/>
<button (click)="openScanner()" class="btn btn-primary"><i class="bi bi-qr-code-scan"></i></button>
</div>
</div>
@@ -55,3 +56,7 @@
</div>
</div>
<ng-template #qrScanner>
Abc
</ng-template>

View File

@@ -1,11 +1,14 @@
import { Component } from '@angular/core';
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { APIService } from '../../services/api';
import { NotificationService } from '../../services/notification';
import QrScanner from 'qr-scanner';
import { NgbModal, NgbSlide } from '@ng-bootstrap/ng-bootstrap';
import { Modal } from '../../components/modal/modal';
@Component({
selector: 'app-screen-send',
imports: [FormsModule],
imports: [FormsModule, NgbSlide],
templateUrl: './screen-send.html',
styleUrl: './screen-send.less',
})
@@ -13,10 +16,13 @@ export class ScreenSend {
amount: number = 0;
recipient: string = '';
reference: string = '';
scanner!: QrScanner;
@ViewChild('qrScanner') templScanner!: TemplateRef<any>;
constructor(
private api: APIService,
private notify: NotificationService,
private modalService: NgbModal,
){}
sendMoney() {
@@ -44,4 +50,12 @@ export class ScreenSend {
this.recipient = '';
this.reference = '';
}
openScanner(){
const modalRef = this.modalService.open(Modal);
modalRef.componentInstance.title = 'Scan a QR Code';
modalRef.componentInstance.body = this.templScanner;
}
closeScanner(){}
}