Skip to content

Commit

Permalink
fix(lib): improved the ngx-mailto.service.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyNahas committed Nov 26, 2020
1 parent ea0e634 commit e692d91
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 27 deletions.
12 changes: 8 additions & 4 deletions src/ngx-mailto/ngx-mailto.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { NgModule } from '@angular/core';
import { NgxMailtoPipe } from './ngx-mailto.pipe';


@NgModule({
declarations: [],
declarations: [NgxMailtoPipe],
exports: [
NgxMailtoPipe
],
imports: [
CommonModule
]
})
export class NgxMailtoModule { }
export class NgxMailtoModule {
}
8 changes: 8 additions & 0 deletions src/ngx-mailto/ngx-mailto.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NgxMailtoPipe } from './ngx-mailto.pipe';

describe('NgxMailtoPipe', () => {
it('create an instance', () => {
const pipe = new NgxMailtoPipe();
expect(pipe).toBeTruthy();
});
});
18 changes: 18 additions & 0 deletions src/ngx-mailto/ngx-mailto.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Pipe, PipeTransform } from '@angular/core';
import { Mailto } from './mailto';
import { NgxMailtoService } from './ngx-mailto.service';

@Pipe({
name: 'mailto'
})
export class NgxMailtoPipe implements PipeTransform {

constructor(private ngxMailtoService: NgxMailtoService) {
}

transform(value: Mailto): string | void {
console.log('transform', value);
return this.ngxMailtoService.compose(value);
}

}
58 changes: 35 additions & 23 deletions src/ngx-mailto/ngx-mailto.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,58 @@ export class NgxMailtoService {
}

compose(value: Mailto): string | void {
console.log('compose -->', value);

let link = 'mailto:';
let properties = [];

if (!value) {
return link;
}
const properties = [];
if (this.isClientSide()) {
if (value.receiver) {
if (typeof value.receiver === 'string') {
link += value.receiver;
if (value?.receiver) {
if (typeof value?.receiver === 'string') {
link += value?.receiver;
} else {
link += value.receiver.join();
link += value?.receiver.join();
}
}
if (value.cc) {
if (typeof value.cc === 'string') {
// link += `?cc=${value.cc}`;
properties.push(`cc=${value.cc}`);
if (value?.cc) {
if (typeof value?.cc === 'string') {
// link += `?cc=${value?.cc}`;
properties.push(`cc=${value?.cc}`);
} else {
// link += `?cc=${value.cc.join()}`;
properties.push(`cc=${value.cc.join()}`);
// link += `?cc=${value?.cc.join()}`;
properties.push(`cc=${value?.cc.join()}`);
}
}
if (value.bcc) {
if (typeof value.bcc === 'string') {
// link += `?bcc=${value.bcc}`;
properties.push(`bcc=${value.bcc}`);
if (value?.bcc) {
if (typeof value?.bcc === 'string') {
// link += `?bcc=${value?.bcc}`;
properties.push(`bcc=${value?.bcc}`);
} else {
// link += `?bcc=${value.bcc.join()}`;
properties.push(`bcc=${value.bcc.join()}`);
// link += `?bcc=${value?.bcc.join()}`;
properties.push(`bcc=${value?.bcc.join()}`);
}
}
if (value.subject) {
// link += `?subject=${value.subject}`;
properties.push(`subject=${value.subject}`);
if (value?.subject) {
// link += `?subject=${value?.subject}`;
properties.push(`subject=${encodeURIComponent(value?.subject)}`);
}
if (value.body) {
// link += `?body=${value.body}`;
properties.push(`body=${value.body}`);
if (value?.body) {
// link += `?body=${value?.body}`;
properties.push(`body=${encodeURIComponent(value?.body)}`);
}
if (properties.length > 0) {
link += `?${properties.join('&')}`;
}
return link;
}
}

open(mailto: Mailto): void {
if (this.isClientSide()) {
window.location.href = this.compose(mailto) as string;
}
}
}

0 comments on commit e692d91

Please sign in to comment.