Feature: Lazy loading
Lazy loaded everything TODO: The cash flow module when clicking on sub-links, it reloads the whole page, it needs to be diagnosed and fixed, this problem also exists in the other modules TODO: Rename folders and modules such as account to accounts to match the url
This commit is contained in:
97
overlord/src/app/core/voucher.service.ts
Normal file
97
overlord/src/app/core/voucher.service.ts
Normal file
@ -0,0 +1,97 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {catchError} from 'rxjs/operators';
|
||||
import {Observable} from 'rxjs/internal/Observable';
|
||||
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
|
||||
import {ErrorLoggerService} from './error-logger.service';
|
||||
import {Voucher} from './voucher';
|
||||
|
||||
const httpOptions = {
|
||||
headers: new HttpHeaders({'Content-Type': 'application/json'})
|
||||
};
|
||||
|
||||
const url = '/api/voucher';
|
||||
const serviceName = 'VoucherService';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class VoucherService {
|
||||
|
||||
constructor(private http: HttpClient, private log: ErrorLoggerService) {
|
||||
}
|
||||
|
||||
get(id: string): Observable<Voucher> {
|
||||
const options = {params: new HttpParams()};
|
||||
return <Observable<Voucher>>this.http.get<Voucher>(`${url}/${id}`, options)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'list'))
|
||||
);
|
||||
}
|
||||
|
||||
getOfType(type: string, account?: string): Observable<Voucher> {
|
||||
const options = {params: new HttpParams().set('t', type)};
|
||||
if (account !== undefined && account !== null) {
|
||||
options.params = options.params.set('a', account);
|
||||
}
|
||||
return <Observable<Voucher>>this.http.get<Voucher>(url, options)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'list'))
|
||||
);
|
||||
}
|
||||
|
||||
getIncentive(date: string): Observable<Voucher> {
|
||||
const options = {params: new HttpParams().set('t', 'Service Charge').set('d', date)};
|
||||
return <Observable<Voucher>>this.http.get<Voucher>(url, options)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'list'))
|
||||
);
|
||||
}
|
||||
|
||||
post(id: string): Observable<Voucher> {
|
||||
const options = {params: new HttpParams().set('p', '')};
|
||||
return <Observable<Voucher>>this.http.post<Voucher>(`${url}/${id}`, {}, options)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'list'))
|
||||
);
|
||||
}
|
||||
|
||||
delete(id: string): Observable<Voucher> {
|
||||
return <Observable<Voucher>>this.http.delete<Voucher>(`${url}/${id}`, httpOptions)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'delete'))
|
||||
);
|
||||
}
|
||||
|
||||
save(voucher: Voucher): Observable<Voucher> {
|
||||
const options = {
|
||||
params: new HttpParams().set('t', voucher.type)
|
||||
};
|
||||
const fd = new FormData();
|
||||
voucher.files.filter(x => !x.id).forEach((file, index) => {
|
||||
fd.append('f' + index, this.dataURLtoBlob(file.resized));
|
||||
fd.append('t' + index, this.dataURLtoBlob(file.thumbnail));
|
||||
});
|
||||
voucher.files = voucher.files.filter(x => x.id);
|
||||
fd.append('model', JSON.stringify(voucher));
|
||||
const saveUrl: string = (voucher.id === undefined || voucher.id === null) ? url : `${url}/${voucher.id}`;
|
||||
return <Observable<Voucher>>this.http.post<Voucher>(saveUrl, fd, options)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'list'))
|
||||
);
|
||||
}
|
||||
|
||||
dataURLtoBlob(dataURL) {
|
||||
const re = /^data:([\w/\-\.]+);\w+,(.*)$/;
|
||||
const m = dataURL.match(re);
|
||||
const mimeString = m[1];
|
||||
const byteString = atob(m[2]);
|
||||
const ab = new ArrayBuffer(byteString.length);
|
||||
const dw = new DataView(ab);
|
||||
let i;
|
||||
|
||||
for (i = 0; i < byteString.length; i++) {
|
||||
dw.setUint8(i, byteString.charCodeAt(i));
|
||||
}
|
||||
return new Blob([ab], {type: mimeString});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user