ToCsvService to escape the fields during exportCsv.

Auth Interceptor to check for logged out user.
This commit is contained in:
tanshu 2018-06-11 22:14:04 +05:30
parent a811a121cc
commit 4530052a22
6 changed files with 84 additions and 34 deletions

View File

@ -5,6 +5,7 @@ import {ClosingStock} from './closing-stock';
import {ActivatedRoute, Router} from '@angular/router'; import {ActivatedRoute, Router} from '@angular/router';
import * as moment from 'moment'; import * as moment from 'moment';
import {FormBuilder, FormGroup} from '@angular/forms'; import {FormBuilder, FormGroup} from '@angular/forms';
import {ToCsvService} from '../shared/to-csv.service';
@Component({ @Component({
selector: 'app-closing-stock', selector: 'app-closing-stock',
@ -20,7 +21,7 @@ export class ClosingStockComponent implements OnInit {
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */ /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['product', 'group', 'quantity', 'amount']; displayedColumns = ['product', 'group', 'quantity', 'amount'];
constructor(private route: ActivatedRoute, private router: Router, private fb: FormBuilder) { constructor(private route: ActivatedRoute, private router: Router, private fb: FormBuilder, private toCsv: ToCsvService) {
this.createForm(); this.createForm();
} }
@ -53,13 +54,16 @@ export class ClosingStockComponent implements OnInit {
date: moment(formModel.date).format('DD-MMM-YYYY') date: moment(formModel.date).format('DD-MMM-YYYY')
}; };
} }
exportCsv() {
const data: string[] = ['Product, Group, Quantity, Amount'];
data.push(
...this.dataSource.data.map(x => '' + x.product + ', ' + x.group + ', ' + x.quantity + ', ' + x.amount)
);
const csvData = new Blob([data.join('\n')], {type: 'text/csv;charset=utf-8;'}); exportCsv() {
const headers = {
Product: 'product',
Group: 'group',
Quantity: 'quantity',
Amount: 'amount'
};
const csvData = new Blob([this.toCsv.toCsv(headers, this.dataSource.data)], {type: 'text/csv;charset=utf-8;'});
const link = document.createElement('a'); const link = document.createElement('a');
link.href = window.URL.createObjectURL(csvData); link.href = window.URL.createObjectURL(csvData);
link.setAttribute('download', 'closing-stock.csv'); link.setAttribute('download', 'closing-stock.csv');

View File

@ -20,8 +20,19 @@ export class HttpAuthInterceptor implements HttpInterceptor {
.pipe( .pipe(
catchError((err: any, caught: Observable<any>): ObservableInput<{}> => { catchError((err: any, caught: Observable<any>): ObservableInput<{}> => {
if (err.status === 401) { if (err.status === 401) {
this.router.navigate(['login']);
this.toaster.show('Danger', 'User has been logged out'); this.toaster.show('Danger', 'User has been logged out');
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: {
title: 'Logged out!',
content: 'You have been logged out.\nYou can press Cancel to stay on page and login in another tab to resume here, or you can press Ok to navigate to the login page.'
}
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.router.navigate(['login']);
}
});
} }
return observableOf(caught); return observableOf(caught);
} }

View File

@ -10,6 +10,7 @@ import {Ledger} from './ledger';
import {Account} from '../account/account'; import {Account} from '../account/account';
import {LedgerService} from './ledger.service'; import {LedgerService} from './ledger.service';
import {AccountService} from '../account/account.service'; import {AccountService} from '../account/account.service';
import {ToCsvService} from '../shared/to-csv.service';
@Component({ @Component({
selector: 'app-ledger', selector: 'app-ledger',
@ -36,6 +37,7 @@ export class LedgerComponent implements OnInit, AfterViewInit {
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
private fb: FormBuilder, private fb: FormBuilder,
private toCsv: ToCsvService,
private ser: LedgerService, private ser: LedgerService,
private accountSer: AccountService private accountSer: AccountService
) { ) {
@ -140,13 +142,17 @@ export class LedgerComponent implements OnInit, AfterViewInit {
} }
exportCsv() { exportCsv() {
const data: string[] = ['Date , Name , Type , Narration , Debit , Credit , Running , Posted']; const headers = {
data.push( Date: 'date',
...this.dataSource.data.map(x => x.date + ', ' + x.name + ', ' + x.type + ', ' + Name: 'name',
x.narration + ', ' + x.debit + ', ' + x.credit + ', ' + x.running + ', ' + x.posted) Type: 'type',
); Narration: 'narration',
Debit: 'debit',
const csvData = new Blob([data.join('\n')], {type: 'text/csv;charset=utf-8;'}); Credit: 'credit',
Running: 'running',
Posted: 'posted'
};
const csvData = new Blob([this.toCsv.toCsv(headers, this.dataSource.data)], {type: 'text/csv;charset=utf-8;'});
const link = document.createElement('a'); const link = document.createElement('a');
link.href = window.URL.createObjectURL(csvData); link.href = window.URL.createObjectURL(csvData);
link.setAttribute('download', 'ledger.csv'); link.setAttribute('download', 'ledger.csv');

View File

@ -7,6 +7,7 @@ import * as moment from 'moment';
import {RawMaterialCostDataSource} from './raw-material-cost-datasource'; import {RawMaterialCostDataSource} from './raw-material-cost-datasource';
import {RawMaterialCost} from './raw-material-cost'; import {RawMaterialCost} from './raw-material-cost';
import {Product} from '../product/product'; import {Product} from '../product/product';
import {ToCsvService} from '../shared/to-csv.service';
@Component({ @Component({
selector: 'app-raw-material-cost', selector: 'app-raw-material-cost',
@ -32,11 +33,7 @@ export class RawMaterialCostComponent implements OnInit {
products: Observable<Product[]>; products: Observable<Product[]>;
constructor( constructor(private route: ActivatedRoute, private router: Router, private fb: FormBuilder, private toCsv: ToCsvService) {
private route: ActivatedRoute,
private router: Router,
private fb: FormBuilder
) {
this.createForm(); this.createForm();
} }
@ -80,21 +77,20 @@ export class RawMaterialCostComponent implements OnInit {
} }
exportCsv() { exportCsv() {
const data: string[] = []; const headers = (this.info.id) ? {
if (this.info.id) { Name: 'name',
data.push( Group: 'group',
'Name, Group, Quantity, Net, Gross', Quantity: 'quantity',
...this.dataSource.data.map(x => x.name + ', ' + x.group + ', ' + x.quantity + ', ' + x.net + ', ' + x.gross) Net: 'net',
); Gross: 'gross'
} else { } : {
data.push( Name: 'name',
'Name, Issue, Sale, RMC', Issue: 'issue',
...this.dataSource.data.map(x => x.name + ', ' + x.issue + ', ' + x.sale + ', ' + x.rmc), Sale: 'sale',
this.info.footer.name + ', ' + this.info.footer.issue + ', ' + this.info.footer.sale + ', ' + this.info.footer.rmc RMC: 'rmc'
); };
}
const csvData = new Blob([data.join('\n')], {type: 'text/csv;charset=utf-8;'}); const csvData = new Blob([this.toCsv.toCsv(headers, this.dataSource.data)], {type: 'text/csv;charset=utf-8;'});
const link = document.createElement('a'); const link = document.createElement('a');
link.href = window.URL.createObjectURL(csvData); link.href = window.URL.createObjectURL(csvData);
link.setAttribute('download', 'raw-material-cost.csv'); link.setAttribute('download', 'raw-material-cost.csv');

View File

@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { ToCsvService } from './to-csv.service';
describe('ToCsvService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ToCsvService]
});
});
it('should be created', inject([ToCsvService], (service: ToCsvService) => {
expect(service).toBeTruthy();
}));
});

View File

@ -0,0 +1,18 @@
import {Injectable} from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ToCsvService {
constructor() {
}
toCsv(headers: any, data: any[]): string {
const header = Object.keys(headers);
const replacer = (key, value) => value === null ? '' : value;
const csv = data.map(row => header.map(fieldName => JSON.stringify(row[headers[fieldName]], replacer)).join(','));
csv.unshift(header.join(','));
return csv.join('\r\n');
}
}