Add date range filtering for transactions

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2021-01-04 19:18:46 -07:00
parent 92f93861e9
commit 9622fa47c3
3 changed files with 38 additions and 9 deletions

View file

@ -205,7 +205,8 @@ export class TwigsHttpService implements TwigsService {
budgetId?: string, budgetId?: string,
categoryId?: string, categoryId?: string,
count?: number, count?: number,
from?: Date from?: Date,
to?: Date
): Observable<Transaction[]> { ): Observable<Transaction[]> {
let httpParams = new HttpParams(); let httpParams = new HttpParams();
if (budgetId) { if (budgetId) {
@ -217,6 +218,9 @@ export class TwigsHttpService implements TwigsService {
if (from) { if (from) {
httpParams = httpParams.set('from', from.toISOString()); httpParams = httpParams.set('from', from.toISOString());
} }
if (to) {
httpParams = httpParams.set('to', to.toISOString());
}
const params = { params: httpParams }; const params = { params: httpParams };
return this.http.get<Transaction[]>(`${this.apiUrl}/transactions`, Object.assign(params, this.options)) return this.http.get<Transaction[]>(`${this.apiUrl}/transactions`, Object.assign(params, this.options))
.pipe(map(transactions => { .pipe(map(transactions => {

View file

@ -36,7 +36,8 @@ export interface TwigsService {
budgetId?: string, budgetId?: string,
categoryId?: string, categoryId?: string,
count?: number, count?: number,
from?: Date from?: Date,
to?: Date
): Observable<Transaction[]>; ): Observable<Transaction[]>;
getTransaction(id: string): Observable<Transaction>; getTransaction(id: string): Observable<Transaction>;
createTransaction( createTransaction(

View file

@ -1,6 +1,7 @@
import { Component, OnInit, Input, Inject } from '@angular/core'; import { Component, OnInit, Input, Inject } from '@angular/core';
import { Transaction } from '../transaction'; import { Transaction } from '../transaction';
import { TWIGS_SERVICE, TwigsService } from '../../shared/twigs.service'; import { TWIGS_SERVICE, TwigsService } from '../../shared/twigs.service';
import { ActivatedRoute } from '@angular/router';
@Component({ @Component({
selector: 'app-transaction-list', selector: 'app-transaction-list',
@ -15,6 +16,7 @@ export class TransactionListComponent implements OnInit {
constructor( constructor(
@Inject(TWIGS_SERVICE) private twigsService: TwigsService, @Inject(TWIGS_SERVICE) private twigsService: TwigsService,
private route: ActivatedRoute
) { } ) { }
ngOnInit(): void { ngOnInit(): void {
@ -22,13 +24,35 @@ export class TransactionListComponent implements OnInit {
} }
getTransactions(): void { getTransactions(): void {
let date = new Date(); let fromStr = this.route.snapshot.queryParamMap.get('from');
date.setHours(0); var from;
date.setMinutes(0); if (fromStr) {
date.setSeconds(0); let fromDate = new Date(fromStr);
date.setMilliseconds(0); if (!isNaN(fromDate.getTime())) {
date.setDate(1); from = fromDate;
this.twigsService.getTransactions(this.budgetId, this.categoryId, null, date).subscribe(transactions => { }
}
if (!from) {
let date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
date.setDate(1);
from = date;
}
let toStr = this.route.snapshot.queryParamMap.get('to');
var to;
if (toStr) {
let toDate = new Date(toStr);
if (!isNaN(toDate.getTime())) {
to = toDate;
}
}
this.twigsService.getTransactions(this.budgetId, this.categoryId, null, from, to).subscribe(transactions => {
this.transactions = transactions; this.transactions = transactions;
}); });
} }