Implement functionality for sending books via email
This commit is contained in:
committed by
Aditya Chandel
parent
a3d9d57d6d
commit
c957f56b86
@@ -28,7 +28,7 @@
|
||||
<div class="book-info">
|
||||
<div class="book-title-container">
|
||||
<h4 class="book-title">{{ book.metadata?.title }}</h4>
|
||||
<p-menu #menu [model]="items" [popup]="true" appendTo="body"></p-menu>
|
||||
<p-tieredmenu #menu [model]="items" [popup]="true" appendTo="body"></p-tieredmenu>
|
||||
<p-button
|
||||
size="small"
|
||||
[text]="true"
|
||||
|
||||
@@ -2,7 +2,7 @@ import {Component, inject, Input, OnInit} from '@angular/core';
|
||||
import {Book} from '../../../model/book.model';
|
||||
import {Button} from 'primeng/button';
|
||||
import {MenuModule} from 'primeng/menu';
|
||||
import {MenuItem} from 'primeng/api';
|
||||
import {MenuItem, MessageService} from 'primeng/api';
|
||||
import {DialogService} from 'primeng/dynamicdialog';
|
||||
import {ShelfAssignerComponent} from '../../shelf-assigner/shelf-assigner.component';
|
||||
import {BookService} from '../../../service/book.service';
|
||||
@@ -16,12 +16,15 @@ import {UrlHelperService} from '../../../../utilities/service/url-helper.service
|
||||
import {NgClass, NgIf} from '@angular/common';
|
||||
import {UserService} from '../../../../user.service';
|
||||
import {filter} from 'rxjs';
|
||||
import {EmailService} from '../../../../settings/email/email.service';
|
||||
import {TieredMenu, TieredMenuModule} from 'primeng/tieredmenu';
|
||||
import {BookSenderComponent} from '../../book-sender/book-sender.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-card',
|
||||
templateUrl: './book-card.component.html',
|
||||
styleUrls: ['./book-card.component.scss'],
|
||||
imports: [Button, MenuModule, CheckboxModule, FormsModule, NgIf, NgClass],
|
||||
imports: [Button, MenuModule, CheckboxModule, FormsModule, NgIf, NgClass, TieredMenu],
|
||||
standalone: true
|
||||
})
|
||||
export class BookCardComponent implements OnInit {
|
||||
@@ -37,8 +40,11 @@ export class BookCardComponent implements OnInit {
|
||||
private bookService = inject(BookService);
|
||||
private dialogService = inject(DialogService);
|
||||
private metadataDialogService = inject(MetadataDialogService);
|
||||
protected urlHelper = inject(UrlHelperService);
|
||||
private userService = inject(UserService);
|
||||
private emailService = inject(EmailService);
|
||||
private messageService = inject(MessageService);
|
||||
|
||||
protected urlHelper = inject(UrlHelperService);
|
||||
|
||||
private userPermissions: any;
|
||||
|
||||
@@ -71,21 +77,16 @@ export class BookCardComponent implements OnInit {
|
||||
private initMenu() {
|
||||
this.items = [
|
||||
{
|
||||
label: 'Options',
|
||||
items: [
|
||||
{
|
||||
label: 'Assign Shelves',
|
||||
icon: 'pi pi-folder',
|
||||
command: () => this.openShelfDialog()
|
||||
},
|
||||
{
|
||||
label: 'View Details',
|
||||
icon: 'pi pi-info-circle',
|
||||
command: () => this.metadataDialogService.openBookMetadataCenterDialog(this.book.id, 'view'),
|
||||
},
|
||||
...this.getPermissionBasedMenuItems(),
|
||||
],
|
||||
label: 'Assign Shelves',
|
||||
icon: 'pi pi-folder',
|
||||
command: () => this.openShelfDialog()
|
||||
},
|
||||
{
|
||||
label: 'View Details',
|
||||
icon: 'pi pi-info-circle',
|
||||
command: () => this.metadataDialogService.openBookMetadataCenterDialog(this.book.id, 'view'),
|
||||
},
|
||||
...this.getPermissionBasedMenuItems(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -139,6 +140,56 @@ export class BookCardComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
|
||||
if (this.hasEmailBookPermission()) {
|
||||
items.push(
|
||||
{
|
||||
label: 'Send Book',
|
||||
icon: 'pi pi-envelope',
|
||||
items: [{
|
||||
label: 'Quick Send',
|
||||
icon: 'pi pi-envelope',
|
||||
command: () => {
|
||||
this.emailService.emailBookQuick(this.book.id).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({
|
||||
severity: 'info',
|
||||
summary: 'Success',
|
||||
detail: 'The book sending has been scheduled.',
|
||||
});
|
||||
},
|
||||
error: (err) => {
|
||||
const errorMessage = err?.error?.message || 'An error occurred while sending the book.';
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: errorMessage,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Custom Send',
|
||||
icon: 'pi pi-envelope',
|
||||
command: () => {
|
||||
this.dialogService.open(BookSenderComponent, {
|
||||
header: 'Send Book to Email',
|
||||
modal: true,
|
||||
closable: true,
|
||||
style: {
|
||||
position: 'absolute',
|
||||
top: '15%',
|
||||
},
|
||||
data: {
|
||||
book: this.book,
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
@@ -170,4 +221,8 @@ export class BookCardComponent implements OnInit {
|
||||
private hasDownloadPermission(): boolean {
|
||||
return this.userPermissions?.canDownload ?? false;
|
||||
}
|
||||
|
||||
private hasEmailBookPermission(): boolean {
|
||||
return this.userPermissions?.canEmailBook ?? false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<div class="flex flex-col gap-10 p-4 items-center justify-center min-w-[500px] max-w-[700px] w-full">
|
||||
|
||||
<p-select
|
||||
[options]="emailProviders"
|
||||
optionLabel="label"
|
||||
placeholder="Select Email Provider"
|
||||
[(ngModel)]="selectedProvider"
|
||||
class="w-full">
|
||||
</p-select>
|
||||
|
||||
<p-select
|
||||
[options]="emailRecipients"
|
||||
optionLabel="label"
|
||||
placeholder="Select Book Recipient"
|
||||
[(ngModel)]="selectedRecipient"
|
||||
[disabled]="!selectedProvider"
|
||||
class="w-full">
|
||||
</p-select>
|
||||
|
||||
<p-button
|
||||
icon="pi pi-envelope"
|
||||
label="Send Book"
|
||||
[disabled]="!selectedProvider || !selectedRecipient"
|
||||
(onClick)="sendBook()">
|
||||
</p-button>
|
||||
|
||||
</div>
|
||||
@@ -0,0 +1,114 @@
|
||||
import {Component, inject, OnInit} from '@angular/core';
|
||||
import {Button} from 'primeng/button';
|
||||
import {Select} from 'primeng/select';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {EmailProviderService} from '../../../settings/email/email-provider/email-provider.service';
|
||||
import {EmailRecipientService} from '../../../settings/email/email-recipient/email-recipient.service';
|
||||
import {EmailProvider} from '../../../settings/email/email-provider/email-provider.model';
|
||||
import {EmailRecipient} from '../../../settings/email/email-recipient/email-recipient.model';
|
||||
import {EmailService} from '../../../settings/email/email.service';
|
||||
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {Book} from '../../model/book.model';
|
||||
import {MessageService} from 'primeng/api';
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-sender',
|
||||
imports: [
|
||||
Button,
|
||||
Select,
|
||||
FormsModule
|
||||
],
|
||||
templateUrl: './book-sender.component.html',
|
||||
styleUrls: ['./book-sender.component.scss']
|
||||
})
|
||||
export class BookSenderComponent implements OnInit {
|
||||
|
||||
private emailProviderService = inject(EmailProviderService);
|
||||
private emailRecipientService = inject(EmailRecipientService);
|
||||
private emailService = inject(EmailService);
|
||||
private messageService = inject(MessageService);
|
||||
private dynamicDialogRef = inject(DynamicDialogRef);
|
||||
private dynamicDialogConfig = inject(DynamicDialogConfig);
|
||||
|
||||
book: Book = this.dynamicDialogConfig.data.book;
|
||||
|
||||
emailProviders: { label: string, value: EmailProvider }[] = [];
|
||||
emailRecipients: { label: string, value: EmailRecipient }[] = [];
|
||||
selectedProvider?: any;
|
||||
selectedRecipient?: any;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.emailProviderService.getEmailProviders().subscribe({
|
||||
next: (emailProviders: EmailProvider[]) => {
|
||||
this.emailProviders = emailProviders.map(provider => ({
|
||||
label: `${provider.name} | ${provider.host}`,
|
||||
value: provider
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
this.emailRecipientService.getRecipients().subscribe({
|
||||
next: (emailRecipients: EmailRecipient[]) => {
|
||||
this.emailRecipients = emailRecipients.map(recipient => ({
|
||||
label: `${recipient.name} | ${recipient.email}`,
|
||||
value: recipient
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
sendBook() {
|
||||
if (this.selectedProvider && this.selectedRecipient && this.book) {
|
||||
const bookId = this.book.id;
|
||||
const recipientId = this.selectedRecipient.value.id;
|
||||
const providerId = this.selectedProvider.value.id;
|
||||
|
||||
const emailRequest = {
|
||||
bookId,
|
||||
providerId,
|
||||
recipientId: recipientId,
|
||||
};
|
||||
|
||||
this.emailService.emailBook(emailRequest).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Email Scheduled',
|
||||
detail: 'The book has been successfully scheduled for sending.'
|
||||
});
|
||||
this.dynamicDialogRef.close(true);
|
||||
},
|
||||
error: (error) => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Sending Failed',
|
||||
detail: 'There was an issue while scheduling the book for sending. Please try again later.'
|
||||
});
|
||||
console.error('Error sending book:', error);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (!this.selectedProvider) {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Email Provider Missing',
|
||||
detail: 'Please select an email provider to proceed.'
|
||||
});
|
||||
}
|
||||
if (!this.selectedRecipient) {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Recipient Missing',
|
||||
detail: 'Please select a recipient to send the book.'
|
||||
});
|
||||
}
|
||||
if (!this.book) {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Book Not Selected',
|
||||
detail: 'Please select a book to send.'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user