Implement functionality for sending books via email
This commit is contained in:
committed by
Aditya Chandel
parent
a3d9d57d6d
commit
c957f56b86
+52
@@ -0,0 +1,52 @@
|
||||
<form [formGroup]="emailProviderForm" (ngSubmit)="createEmailProvider()" class="space-y-6 p-6">
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Provider Name:</label>
|
||||
<input type="text" pInputText formControlName="name" placeholder="Enter provider name..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
<small class="text-red-500 pl-44" *ngIf="emailProviderForm.get('name')?.invalid && (emailProviderForm.get('name')?.dirty || emailProviderForm.get('name')?.touched)">
|
||||
Provider name is required.
|
||||
</small>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Host:</label>
|
||||
<input type="text" pInputText formControlName="host" placeholder="Enter host..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
<small class="text-red-500 pl-44" *ngIf="emailProviderForm.get('host')?.invalid && (emailProviderForm.get('host')?.dirty || emailProviderForm.get('host')?.touched)">
|
||||
Host is required.
|
||||
</small>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Port:</label>
|
||||
<input type="number" pInputText formControlName="port" placeholder="Enter port..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
<small class="text-red-500 pl-44" *ngIf="emailProviderForm.get('port')?.invalid && (emailProviderForm.get('port')?.dirty || emailProviderForm.get('port')?.touched)">
|
||||
Port is required and must be a number.
|
||||
</small>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Username:</label>
|
||||
<input type="text" pInputText formControlName="username" placeholder="Enter username..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Password:</label>
|
||||
<input type="password" pInputText formControlName="password" placeholder="Enter password..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Auth:</label>
|
||||
<p-checkbox formControlName="auth" [binary]="true"></p-checkbox>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">StartTLS:</label>
|
||||
<p-checkbox formControlName="startTls" [binary]="true"></p-checkbox>
|
||||
</div>
|
||||
|
||||
<div class="flex justify-end">
|
||||
<p-button label="Create Email Provider" [disabled]="emailProviderForm.invalid" type="submit">
|
||||
</p-button>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
import { Component, inject, OnInit } from '@angular/core';
|
||||
import { Button } from 'primeng/button';
|
||||
import { Checkbox } from 'primeng/checkbox';
|
||||
import { InputText } from 'primeng/inputtext';
|
||||
import { NgIf } from '@angular/common';
|
||||
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
|
||||
import { EmailProviderService } from '../email-provider/email-provider.service';
|
||||
import { MessageService } from 'primeng/api';
|
||||
import { DynamicDialogRef } from 'primeng/dynamicdialog';
|
||||
|
||||
@Component({
|
||||
selector: 'app-create-email-provider-dialog',
|
||||
imports: [
|
||||
Button,
|
||||
Checkbox,
|
||||
InputText,
|
||||
NgIf,
|
||||
ReactiveFormsModule
|
||||
],
|
||||
templateUrl: './create-email-provider-dialog.component.html',
|
||||
styleUrl: './create-email-provider-dialog.component.scss'
|
||||
})
|
||||
export class CreateEmailProviderDialogComponent implements OnInit {
|
||||
emailProviderForm!: FormGroup;
|
||||
|
||||
private fb = inject(FormBuilder);
|
||||
private emailProviderService = inject(EmailProviderService);
|
||||
private messageService = inject(MessageService);
|
||||
private ref = inject(DynamicDialogRef);
|
||||
|
||||
ngOnInit() {
|
||||
this.emailProviderForm = this.fb.group({
|
||||
name: ['', [Validators.required, Validators.minLength(3)]],
|
||||
host: ['', Validators.required],
|
||||
port: [null, [Validators.required, Validators.min(1)]],
|
||||
username: [''],
|
||||
password: [''],
|
||||
auth: [false],
|
||||
startTls: [false]
|
||||
});
|
||||
}
|
||||
|
||||
createEmailProvider() {
|
||||
if (this.emailProviderForm.invalid) {
|
||||
this.messageService.add({
|
||||
severity: 'warn',
|
||||
summary: 'Validation Error',
|
||||
detail: 'Please correct errors before submitting.'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const emailProviderData = this.emailProviderForm.value;
|
||||
|
||||
this.emailProviderService.createEmailProvider(emailProviderData).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Email Provider Created',
|
||||
detail: 'The email provider has been successfully created.'
|
||||
});
|
||||
this.ref.close(true);
|
||||
},
|
||||
error: (err) => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Email Provider Creation Failed',
|
||||
detail: err?.error?.message
|
||||
? `Unable to create email provider: ${err.error.message}`
|
||||
: 'An unexpected error occurred while creating the email provider. Please try again later.'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
<form [formGroup]="emailRecipientForm" (ngSubmit)="createEmailRecipient()" class="space-y-6 p-6">
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Recipient Name:</label>
|
||||
<input type="text" pInputText formControlName="name" placeholder="Enter recipient name..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
<small class="text-red-500 pl-44" *ngIf="emailRecipientForm.get('name')?.invalid && (emailRecipientForm.get('name')?.dirty || emailRecipientForm.get('name')?.touched)">
|
||||
Recipient name is required.
|
||||
</small>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Email Address:</label>
|
||||
<input type="email" pInputText formControlName="email" placeholder="Enter email address..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
<small class="text-red-500 pl-44" *ngIf="emailRecipientForm.get('email')?.invalid && (emailRecipientForm.get('email')?.dirty || emailRecipientForm.get('email')?.touched)">
|
||||
Valid email is required.
|
||||
</small>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Default Recipient:</label>
|
||||
<p-checkbox formControlName="defaultRecipient" [binary]="true"></p-checkbox>
|
||||
</div>
|
||||
|
||||
<div class="flex justify-end">
|
||||
<p-button label="Add Recipient" [disabled]="emailRecipientForm.invalid" type="submit">
|
||||
</p-button>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
import { Component, inject } from '@angular/core';
|
||||
import { FormBuilder, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
|
||||
import { MessageService } from 'primeng/api';
|
||||
import { DynamicDialogRef } from 'primeng/dynamicdialog';
|
||||
import { Checkbox } from 'primeng/checkbox';
|
||||
import { Button } from 'primeng/button';
|
||||
import { InputText } from 'primeng/inputtext';
|
||||
import { NgIf } from '@angular/common';
|
||||
import { EmailRecipientService } from '../email-recipient/email-recipient.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-create-email-recipient-dialog',
|
||||
imports: [
|
||||
Checkbox,
|
||||
ReactiveFormsModule,
|
||||
Button,
|
||||
InputText,
|
||||
NgIf
|
||||
],
|
||||
templateUrl: './create-email-recipient-dialog.component.html',
|
||||
styleUrls: ['./create-email-recipient-dialog.component.scss']
|
||||
})
|
||||
export class CreateEmailRecipientDialogComponent {
|
||||
emailRecipientForm: FormGroup;
|
||||
private fb = inject(FormBuilder);
|
||||
private emailRecipientService = inject(EmailRecipientService);
|
||||
private messageService = inject(MessageService);
|
||||
private ref = inject(DynamicDialogRef);
|
||||
|
||||
constructor() {
|
||||
this.emailRecipientForm = this.fb.group({
|
||||
name: ['', Validators.required],
|
||||
email: ['', [Validators.required, Validators.email]],
|
||||
defaultRecipient: [false]
|
||||
});
|
||||
}
|
||||
|
||||
createEmailRecipient(): void {
|
||||
if (this.emailRecipientForm.invalid) {
|
||||
this.messageService.add({
|
||||
severity: 'warn',
|
||||
summary: 'Validation Error',
|
||||
detail: 'Please correct errors before submitting.'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const emailRecipientData = this.emailRecipientForm.value;
|
||||
|
||||
this.emailRecipientService.createRecipient(emailRecipientData).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Recipient Added',
|
||||
detail: `${emailRecipientData.name} has been successfully added.`
|
||||
});
|
||||
this.ref.close(true);
|
||||
},
|
||||
error: (err) => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Recipient Creation Failed',
|
||||
detail: err?.error?.message
|
||||
? `Unable to create recipient: ${err.error.message}`
|
||||
: 'An unexpected error occurred while adding the recipient. Please try again later.'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<div class="flex justify-between items-center pb-2">
|
||||
<h2 class="text-xl flex items-center gap-2">
|
||||
Email Providers
|
||||
<i class="pi pi-info-circle text-sky-600"
|
||||
pTooltip="Configure email-sending services like Gmail, Outlook, or custom SMTP servers for sending books via email. The default email provider will be used for 'Quick Book Send' located in the Book Card menu"
|
||||
tooltipPosition="right"
|
||||
style="cursor: pointer;"></i>
|
||||
</h2>
|
||||
<p-button outlined="true" label="Create New Provider" (onClick)="openCreateProviderDialog()"></p-button>
|
||||
</div>
|
||||
|
||||
<p-table [value]="emailProviders">
|
||||
<ng-template pTemplate="header">
|
||||
<tr>
|
||||
<th style="width: 5%;">Default</th>
|
||||
<th style="width: 15%;">Name</th>
|
||||
<th style="width: 15%;">Host</th>
|
||||
<th style="width: 10%;">Port</th>
|
||||
<th style="width: 15%;">Username</th>
|
||||
<th style="width: 15%;">Password</th>
|
||||
<th style="width: 5%;">Auth</th>
|
||||
<th style="width: 5%;">StartTLS</th>
|
||||
<th style="width: 10%;">Edit</th>
|
||||
<th style="width: 10%;">Delete</th>
|
||||
</tr>
|
||||
</ng-template>
|
||||
|
||||
<ng-template pTemplate="body" let-provider>
|
||||
<tr>
|
||||
<td class="text-center">
|
||||
<p-radioButton
|
||||
name="defaultProvider"
|
||||
[value]="provider.id"
|
||||
[(ngModel)]="defaultProviderId"
|
||||
(onClick)="setDefaultProvider(provider)">
|
||||
</p-radioButton>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<input *ngIf="provider.isEditing" type="text" [(ngModel)]="provider.name" class="p-inputtext w-full"/>
|
||||
<span *ngIf="!provider.isEditing">{{ provider.name }}</span>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<input *ngIf="provider.isEditing" type="text" [(ngModel)]="provider.host" class="p-inputtext w-full"/>
|
||||
<span *ngIf="!provider.isEditing">{{ provider.host }}</span>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<input *ngIf="provider.isEditing" type="number" [(ngModel)]="provider.port" class="p-inputtext w-full"/>
|
||||
<span *ngIf="!provider.isEditing">{{ provider.port }}</span>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<input *ngIf="provider.isEditing" type="text" [(ngModel)]="provider.username" class="p-inputtext w-full"/>
|
||||
<span *ngIf="!provider.isEditing">{{ provider.username }}</span>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<input *ngIf="provider.isEditing" [(ngModel)]="provider.password" class="p-inputtext w-full"/>
|
||||
<span *ngIf="!provider.isEditing" class="text-gray-400 italic">Hidden</span>
|
||||
</td>
|
||||
|
||||
<td class="text-center">
|
||||
<p-checkbox
|
||||
[(ngModel)]="provider.auth"
|
||||
[binary]="true"
|
||||
[disabled]="!provider.isEditing">
|
||||
</p-checkbox>
|
||||
</td>
|
||||
|
||||
<td class="text-center">
|
||||
<p-checkbox
|
||||
[(ngModel)]="provider.startTls"
|
||||
[binary]="true"
|
||||
[disabled]="!provider.isEditing">
|
||||
</p-checkbox>
|
||||
</td>
|
||||
|
||||
<td class="text-center">
|
||||
<p-button *ngIf="!provider.isEditing" icon="pi pi-pencil" outlined="true" severity="info" (onClick)="toggleEdit(provider)"></p-button>
|
||||
<p-button *ngIf="provider.isEditing" icon="pi pi-check" outlined="true" severity="success" (onClick)="saveProvider(provider)"></p-button>
|
||||
<p-button *ngIf="provider.isEditing" icon="pi pi-times" outlined="true" severity="danger" (onClick)="toggleEdit(provider)"></p-button>
|
||||
</td>
|
||||
|
||||
<td class="text-center">
|
||||
<p-button icon="pi pi-trash" outlined="true" severity="danger" (onClick)="deleteProvider(provider)"></p-button>
|
||||
</td>
|
||||
</tr>
|
||||
</ng-template>
|
||||
</p-table>
|
||||
@@ -0,0 +1,140 @@
|
||||
import {Component, inject, OnInit} from '@angular/core';
|
||||
import {Button} from 'primeng/button';
|
||||
import {Checkbox} from 'primeng/checkbox';
|
||||
import {NgIf} from '@angular/common';
|
||||
import {MessageService, PrimeTemplate} from 'primeng/api';
|
||||
import {RadioButton} from 'primeng/radiobutton';
|
||||
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
|
||||
import {TableModule} from 'primeng/table';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {EmailProvider} from './email-provider.model';
|
||||
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {EmailProviderService} from './email-provider.service';
|
||||
import {CreateEmailProviderDialogComponent} from '../create-email-provider-dialog/create-email-provider-dialog.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-email-provider',
|
||||
imports: [
|
||||
Button,
|
||||
Checkbox,
|
||||
NgIf,
|
||||
PrimeTemplate,
|
||||
RadioButton,
|
||||
ReactiveFormsModule,
|
||||
TableModule,
|
||||
Tooltip,
|
||||
FormsModule
|
||||
],
|
||||
templateUrl: './email-provider.component.html',
|
||||
styleUrl: './email-provider.component.scss'
|
||||
})
|
||||
export class EmailProviderComponent implements OnInit {
|
||||
emailProviders: EmailProvider[] = [];
|
||||
editingProviderIds: number[] = [];
|
||||
ref: DynamicDialogRef | undefined;
|
||||
private dialogService = inject(DialogService);
|
||||
private emailProvidersService = inject(EmailProviderService);
|
||||
private messageService = inject(MessageService);
|
||||
defaultProviderId: any;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.loadEmailProviders();
|
||||
}
|
||||
|
||||
loadEmailProviders(): void {
|
||||
this.emailProvidersService.getEmailProviders().subscribe({
|
||||
next: (emailProviders: EmailProvider[]) => {
|
||||
this.emailProviders = emailProviders.map((provider) => ({
|
||||
...provider,
|
||||
isEditing: false,
|
||||
}));
|
||||
const defaultProvider = emailProviders.find((provider) => provider.defaultProvider);
|
||||
this.defaultProviderId = defaultProvider ? defaultProvider.id : null;
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'Failed to load Email Providers',
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
toggleEdit(provider: EmailProvider): void {
|
||||
provider.isEditing = !provider.isEditing;
|
||||
if (provider.isEditing) {
|
||||
this.editingProviderIds.push(provider.id);
|
||||
} else {
|
||||
this.editingProviderIds = this.editingProviderIds.filter((id) => id !== provider.id);
|
||||
}
|
||||
}
|
||||
|
||||
saveProvider(provider: EmailProvider): void {
|
||||
this.emailProvidersService.updateProvider(provider).subscribe({
|
||||
next: () => {
|
||||
provider.isEditing = false;
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Success',
|
||||
detail: 'Provider updated successfully',
|
||||
});
|
||||
this.loadEmailProviders();
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'Failed to update provider',
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
deleteProvider(provider: EmailProvider): void {
|
||||
if (confirm(`Are you sure you want to delete provider "${provider.name}"?`)) {
|
||||
this.emailProvidersService.deleteProvider(provider.id).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Success',
|
||||
detail: `Provider "${provider.name}" deleted successfully`,
|
||||
});
|
||||
this.loadEmailProviders();
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'Failed to delete provider',
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
openCreateProviderDialog() {
|
||||
this.ref = this.dialogService.open(CreateEmailProviderDialogComponent, {
|
||||
header: 'Create New Email Provider',
|
||||
modal: true,
|
||||
closable: true,
|
||||
style: { position: 'absolute', top: '15%' },
|
||||
});
|
||||
this.ref.onClose.subscribe((result) => {
|
||||
if (result) {
|
||||
this.loadEmailProviders();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setDefaultProvider(provider: EmailProvider) {
|
||||
this.emailProvidersService.setDefaultProvider(provider.id).subscribe(() => {
|
||||
this.defaultProviderId = provider.id;
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Default Provider Set',
|
||||
detail: `${provider.name} is now the default email provider.`
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
export interface EmailProvider {
|
||||
isEditing: boolean;
|
||||
id: number;
|
||||
name: string;
|
||||
host: string;
|
||||
port: number;
|
||||
username: string;
|
||||
password: string;
|
||||
auth: boolean;
|
||||
startTls: boolean;
|
||||
defaultProvider: boolean;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { EmailProvider } from './email-provider.model';
|
||||
import { API_CONFIG } from '../../../config/api-config';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class EmailProviderService {
|
||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/email/providers`;
|
||||
|
||||
private http = inject(HttpClient);
|
||||
|
||||
getEmailProviders(): Observable<EmailProvider[]> {
|
||||
return this.http.get<EmailProvider[]>(this.url);
|
||||
}
|
||||
|
||||
createEmailProvider(provider: EmailProvider): Observable<EmailProvider> {
|
||||
return this.http.post<EmailProvider>(this.url, provider);
|
||||
}
|
||||
|
||||
updateProvider(provider: EmailProvider): Observable<EmailProvider> {
|
||||
return this.http.put<EmailProvider>(`${this.url}/${provider.id}`, provider);
|
||||
}
|
||||
|
||||
deleteProvider(id: number): Observable<void> {
|
||||
return this.http.delete<void>(`${this.url}/${id}`);
|
||||
}
|
||||
|
||||
setDefaultProvider(id: number): Observable<void> {
|
||||
return this.http.patch<void>(`${this.url}/${id}/set-default`, {});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<div class="flex justify-between items-center mb-4">
|
||||
<h2 class="text-xl flex items-center gap-2">
|
||||
Recipient Emails
|
||||
<i class="pi pi-info-circle text-sky-600"
|
||||
pTooltip="Manage the list of recipients who will receive books via email. The 'Default' recipient will be used for 'Quick Book Send,' located in the Book Card menu."
|
||||
tooltipPosition="right"
|
||||
style="cursor: pointer;"></i>
|
||||
</h2>
|
||||
<p-button outlined="true" label="Add New Recipient" (onClick)="openAddRecipientDialog()"></p-button>
|
||||
</div>
|
||||
|
||||
<p-table [value]="recipientEmails">
|
||||
<ng-template pTemplate="header">
|
||||
<tr>
|
||||
<th style="width: 5%;">Default</th>
|
||||
<th style="width: 40%;">Email Address</th>
|
||||
<th style="width: 40%;">Name</th>
|
||||
<th style="width: 15%;">Edit</th>
|
||||
<th style="width: 5%;">Delete</th>
|
||||
</tr>
|
||||
</ng-template>
|
||||
|
||||
<ng-template pTemplate="body" let-recipient>
|
||||
<tr>
|
||||
<td class="text-center">
|
||||
<p-radioButton
|
||||
name="defaultRecipient"
|
||||
[value]="recipient.id"
|
||||
[(ngModel)]="defaultRecipientId"
|
||||
(onClick)="setDefaultRecipient(recipient)">
|
||||
</p-radioButton>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<input *ngIf="recipient.isEditing" type="email" [(ngModel)]="recipient.email" class="p-inputtext w-full"/>
|
||||
<span *ngIf="!recipient.isEditing">{{ recipient.email }}</span>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<input *ngIf="recipient.isEditing" type="text" [(ngModel)]="recipient.name" class="p-inputtext w-full"/>
|
||||
<span *ngIf="!recipient.isEditing">{{ recipient.name }}</span>
|
||||
</td>
|
||||
|
||||
<td class="text-center">
|
||||
<p-button *ngIf="!recipient.isEditing" icon="pi pi-pencil" outlined="true" severity="info" (onClick)="toggleEditRecipient(recipient)"></p-button>
|
||||
<p-button *ngIf="recipient.isEditing" icon="pi pi-check" outlined="true" severity="success" (onClick)="saveRecipient(recipient)"></p-button>
|
||||
<p-button *ngIf="recipient.isEditing" icon="pi pi-times" outlined="true" severity="danger" (onClick)="toggleEditRecipient(recipient)"></p-button>
|
||||
</td>
|
||||
|
||||
<td class="text-center">
|
||||
<p-button icon="pi pi-trash" outlined="true" severity="danger" (onClick)="deleteRecipient(recipient)"></p-button>
|
||||
</td>
|
||||
</tr>
|
||||
</ng-template>
|
||||
</p-table>
|
||||
@@ -0,0 +1,141 @@
|
||||
import {Component, inject, OnInit} from '@angular/core';
|
||||
import {Button} from 'primeng/button';
|
||||
import {NgIf} from '@angular/common';
|
||||
import {MessageService, PrimeTemplate} from 'primeng/api';
|
||||
import {RadioButton} from 'primeng/radiobutton';
|
||||
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
|
||||
import {TableModule} from 'primeng/table';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {EmailProvider} from '../email-provider/email-provider.model';
|
||||
import {EmailRecipient} from './email-recipient.model';
|
||||
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {EmailProviderService} from '../email-provider/email-provider.service';
|
||||
import {EmailRecipientService} from './email-recipient.service';
|
||||
import {CreateEmailProviderDialogComponent} from '../create-email-provider-dialog/create-email-provider-dialog.component';
|
||||
import {CreateEmailRecipientDialogComponent} from '../create-email-recipient-dialog/create-email-recipient-dialog.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-email-recipient',
|
||||
imports: [
|
||||
Button,
|
||||
NgIf,
|
||||
PrimeTemplate,
|
||||
RadioButton,
|
||||
ReactiveFormsModule,
|
||||
TableModule,
|
||||
Tooltip,
|
||||
FormsModule
|
||||
],
|
||||
templateUrl: './email-recipient.component.html',
|
||||
styleUrl: './email-recipient.component.scss'
|
||||
})
|
||||
export class EmailRecipientComponent implements OnInit {
|
||||
recipientEmails: EmailRecipient[] = [];
|
||||
editingRecipientIds: number[] = [];
|
||||
ref: DynamicDialogRef | undefined;
|
||||
private dialogService = inject(DialogService);
|
||||
private emailRecipientService = inject(EmailRecipientService);
|
||||
private messageService = inject(MessageService);
|
||||
defaultRecipientId: any;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.loadRecipientEmails();
|
||||
}
|
||||
|
||||
loadRecipientEmails(): void {
|
||||
this.emailRecipientService.getRecipients().subscribe({
|
||||
next: (recipients: EmailRecipient[]) => {
|
||||
this.recipientEmails = recipients.map((recipient) => ({
|
||||
...recipient,
|
||||
isEditing: false,
|
||||
}));
|
||||
const defaultRecipient = recipients.find((recipient) => recipient.defaultRecipient);
|
||||
this.defaultRecipientId = defaultRecipient ? defaultRecipient.id : null;
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'Failed to load recipient emails',
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
toggleEditRecipient(recipient: EmailRecipient): void {
|
||||
recipient.isEditing = !recipient.isEditing;
|
||||
if (recipient.isEditing) {
|
||||
this.editingRecipientIds.push(recipient.id);
|
||||
} else {
|
||||
this.editingRecipientIds = this.editingRecipientIds.filter((id) => id !== recipient.id);
|
||||
}
|
||||
}
|
||||
|
||||
saveRecipient(recipient: EmailRecipient): void {
|
||||
this.emailRecipientService.updateRecipient(recipient).subscribe({
|
||||
next: () => {
|
||||
recipient.isEditing = false;
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Success',
|
||||
detail: 'Recipient updated successfully',
|
||||
});
|
||||
this.loadRecipientEmails();
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'Failed to update recipient',
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
deleteRecipient(recipient: EmailRecipient): void {
|
||||
if (confirm(`Are you sure you want to delete recipient "${recipient.email}"?`)) {
|
||||
this.emailRecipientService.deleteRecipient(recipient.id).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Success',
|
||||
detail: `Recipient "${recipient.email}" deleted successfully`,
|
||||
});
|
||||
this.loadRecipientEmails();
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'Failed to delete recipient',
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
openAddRecipientDialog() {
|
||||
this.ref = this.dialogService.open(CreateEmailRecipientDialogComponent, {
|
||||
header: 'Add New Recipient',
|
||||
modal: true,
|
||||
closable: true,
|
||||
style: {position: 'absolute', top: '15%'},
|
||||
});
|
||||
this.ref.onClose.subscribe((result) => {
|
||||
if (result) {
|
||||
this.loadRecipientEmails();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setDefaultRecipient(recipient: EmailRecipient) {
|
||||
this.emailRecipientService.setDefaultRecipient(recipient.id).subscribe(() => {
|
||||
this.defaultRecipientId = recipient.id;
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Default Recipient Set',
|
||||
detail: `${recipient.email} is now the default recipient.`
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export interface EmailRecipient {
|
||||
id: number;
|
||||
email: string;
|
||||
name: string;
|
||||
defaultRecipient: boolean;
|
||||
isEditing: boolean;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { API_CONFIG } from '../../../config/api-config';
|
||||
import {EmailRecipient} from './email-recipient.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class EmailRecipientService {
|
||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/email/recipients`;
|
||||
|
||||
private http = inject(HttpClient);
|
||||
|
||||
getRecipients(): Observable<EmailRecipient[]> {
|
||||
return this.http.get<EmailRecipient[]>(this.url);
|
||||
}
|
||||
|
||||
createRecipient(recipient: EmailRecipient): Observable<EmailRecipient> {
|
||||
return this.http.post<EmailRecipient>(this.url, recipient);
|
||||
}
|
||||
|
||||
updateRecipient(recipient: EmailRecipient): Observable<EmailRecipient> {
|
||||
return this.http.put<EmailRecipient>(`${this.url}/${recipient.id}`, recipient);
|
||||
}
|
||||
|
||||
deleteRecipient(id: number): Observable<void> {
|
||||
return this.http.delete<void>(`${this.url}/${id}`);
|
||||
}
|
||||
|
||||
setDefaultRecipient(id: number): Observable<void> {
|
||||
return this.http.patch<void>(`${this.url}/${id}/set-default`, {});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<div class="w-full h-[calc(100vh-12.1rem)] overflow-y-auto border rounded-lg p-4 enclosing-container">
|
||||
|
||||
<div class="pt-4">
|
||||
<app-email-provider></app-email-provider>
|
||||
</div>
|
||||
|
||||
<div class="pb-12 pt-12">
|
||||
<app-email-recipient></app-email-recipient>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
@@ -0,0 +1,3 @@
|
||||
.enclosing-container {
|
||||
border-color: var(--p-content-border-color);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import {Component} from '@angular/core';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {TableModule} from 'primeng/table';
|
||||
import {EmailProviderComponent} from './email-provider/email-provider.component';
|
||||
import {EmailRecipientComponent} from './email-recipient/email-recipient.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-email',
|
||||
imports: [
|
||||
FormsModule,
|
||||
TableModule,
|
||||
EmailProviderComponent,
|
||||
EmailRecipientComponent
|
||||
],
|
||||
templateUrl: './email.component.html',
|
||||
styleUrls: ['./email.component.scss'],
|
||||
})
|
||||
export class EmailComponent {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {API_CONFIG} from '../../config/api-config';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class EmailService {
|
||||
|
||||
private readonly apiUrl = `${API_CONFIG.BASE_URL}/api/v1/emails`;
|
||||
|
||||
private http = inject(HttpClient);
|
||||
|
||||
emailBook(request: { bookId: number, providerId: number, recipientId: number }): Observable<void> {
|
||||
return this.http.post<void>(`${this.apiUrl}/send-book`, request);
|
||||
}
|
||||
|
||||
emailBookQuick(bookId: number): Observable<void> {
|
||||
return this.http.post<void>(`${this.apiUrl}/send-book/${bookId}`, {});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user