Skip to content

Commit

Permalink
circulation: fix manual fee modal dialog
Browse files Browse the repository at this point in the history
Fix subscription to modal dialog box when adding a manual fee to prevent
unknown component subscription.

Co-Authored-by: Renaud Michotte <renaud.michotte@gmail.com>
  • Loading branch information
zannkukai committed Feb 20, 2023
1 parent 1575280 commit d72ad5b
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,22 @@ import { DateTime } from 'luxon';
})
export class PatronFeeComponent implements OnInit {

// COMPONENT ATTRIBUTES =====================================================
/** Patron pid */
@Input() patronPid: string;

/** Organisation pid */
@Input() organisationPid: string;

/** form */
form: FormGroup = new FormGroup({});

/** form fields */
formFields: FormlyFieldConfig[];

/** model */
model: FeeFormModel;

/** On submit event */
onSubmit: EventEmitter<any> = new EventEmitter();

// CONSTRUCTOR & HOOKS ======================================================
/**
* Constructor
* @param _recordService - RecordService
Expand Down Expand Up @@ -86,6 +84,7 @@ export class PatronFeeComponent implements OnInit {
});
}

// COMPONENT FUNCTIONS ======================================================
/**
* Submit the form
* @param model - Fee model
Expand Down Expand Up @@ -119,48 +118,42 @@ export class PatronFeeComponent implements OnInit {
}

/** Init form model */
private _initForm(properties: any): void
{
this.formFields = [
{
key: 'type',
type: 'selectWithSort',
templateOptions: {
label: 'Type',
required: true,
options: properties.type.form.options
}
},
{
key: 'total_amount',
type: 'input',
templateOptions: {
type: 'number',
label: 'Amount',
required: true,
addonLeft: {
text: getCurrencySymbol(this._organisationService.organisation.default_currency, 'wide')
}
}
},
{
key: 'note',
type: 'input',
templateOptions: {
label: 'Note'
}
},
{
key: 'creation_date',
type: 'dateTimePicker',
wrappers: ['form-field'],
templateOptions: {
label: 'Date',
required: true,
dateFormat: 'yy-mm-dd',
private _initForm(properties: any): void {
this.formFields = [{
key: 'type',
type: 'selectWithSort',
templateOptions: {
label: 'Type',
required: true,
options: properties.type.form.options
}
}, {
key: 'total_amount',
type: 'input',
templateOptions: {
type: 'number',
label: 'Amount',
required: true,
addonLeft: {
text: getCurrencySymbol(this._organisationService.organisation.default_currency, 'wide')
}
}
];
}, {
key: 'note',
type: 'input',
templateOptions: {
label: 'Note'
}
}, {
key: 'creation_date',
type: 'dateTimePicker',
wrappers: ['form-field'],
templateOptions: {
label: 'Date',
required: true,
dateFormat: 'yy-mm-dd',
}
}];

// Default model value
this.model = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ import { PatronTransactionEventFormComponent } from './patron-transaction-event-
export class PatronTransactionsComponent implements OnInit, OnDestroy {

// COMPONENTS ATTRIBUTES ===============================================================
/** Subscription to PatronTransaction.service.ts --> patronTransactionByPatronSubject */
patronTransactionSubscription$: Subscription;
/** all tab reference array */
tabs = {
engagedFees: {
Expand All @@ -58,8 +56,10 @@ export class PatronTransactionsComponent implements OnInit, OnDestroy {

/** Current patron */
private _patron: any = undefined;

/** Modal used for manual fee */
private _modalRef: BsModalRef;
/** Component subscriptions */
private _subscriptions = new Subscription();


// GETTER & SETTER ======================================================================
Expand All @@ -71,6 +71,15 @@ export class PatronTransactionsComponent implements OnInit, OnDestroy {
return this._organisationService.organisation;
}

/**
* Get engaged fees related to the current user library
* @return the list of corresponding transactions.
*/
get myLibraryEngagedFees(): Array<PatronTransaction> {
const libraryPID = this._userService.user.currentLibrary;
return this.tabs.engagedFees.transactions.filter(t => t.library != null && t.library.pid === libraryPID);
}


// CONSTRUCTOR & HOOKS ==================================================================
/**
Expand All @@ -79,6 +88,7 @@ export class PatronTransactionsComponent implements OnInit, OnDestroy {
* @param _organisationService - OrganisationService
* @param _patronTransactionService - PatronTransactionService
* @param _modalService - BsModalService
* @param _userService - UserService
*/
constructor(
private _patronService: PatronService,
Expand All @@ -94,49 +104,30 @@ export class PatronTransactionsComponent implements OnInit, OnDestroy {
if (patron) {
this._patron = patron;
// engaged fees
this.patronTransactionSubscription$ = this._patronTransactionService.patronTransactionsSubject$.subscribe(
(transactions) => {
this.tabs.engagedFees.transactions = transactions;
this.tabs.engagedFees.totalAmount = this._patronTransactionService.computeTotalTransactionsAmount(transactions);
}
this._subscriptions.add(
this._patronTransactionService
.patronTransactionsSubject$
.subscribe((transactions) => {
this.tabs.engagedFees.transactions = transactions;
this.tabs.engagedFees.totalAmount = this._patronTransactionService.computeTotalTransactionsAmount(transactions);
}
)
);
// overdue fees
this._patronService.getOverduesPreview(this._patron.pid).subscribe(
(overdues) => {
this._patronService
.getOverduesPreview(this._patron.pid)
.subscribe((overdues) => {
this.tabs.overduePreviewFees.transactions = overdues;
this.tabs.overduePreviewFees.totalAmount = overdues.reduce((acc, overdue) => acc + overdue.fees.total, 0);
}
);
this.loadEngagedFees();
});
this._reloadEngagedFees();
}
});
}

/** OnDestroy hook */
ngOnDestroy() {
if (this.patronTransactionSubscription$) {
this.patronTransactionSubscription$.unsubscribe();
}
this._modalRef.content.onSubmit.unsubscribe();
}

loadEngagedFees(): void {
this._patronTransactionService.emitPatronTransactionByPatron(this._patron.pid, undefined, 'open');
}

/** Opening a modal to add fees */
addFee(): void {
this._modalRef = this._modalService.show(PatronFeeComponent, {
ignoreBackdropClick: true,
initialState: {
patronPid: this._patron.pid,
organisationPid: this._patron.organisation.pid
}
}
);
this._modalRef.content.onSubmit.pipe(first()).subscribe(() => {
this.loadEngagedFees();
});
this._subscriptions.unsubscribe();
}

// COMPONENT FUNCTIONS ==================================================================
Expand All @@ -152,31 +143,27 @@ export class PatronTransactionsComponent implements OnInit, OnDestroy {
}

/** Allow to pay the total of each pending patron transactions */
public payAllTransactions() {
payAllTransactions() {
const initialState = {
action: 'pay',
mode: 'full',
transactions: this.tabs.engagedFees.transactions
};
this._modalService.show(PatronTransactionEventFormComponent, {initialState});
}
get myLibraryEngagedFees() {
const libraryPID = this._userService.user.currentLibrary;
return this.tabs.engagedFees.transactions.filter(t => t.library != null && t.library.pid === libraryPID);
}

/** Allow to pay the total of each pending patron transactions */
public payAllTransactionsInMyLibrary() {
const initialState = {
action: 'pay',
mode: 'full',
transactions: this.myLibraryEngagedFees
};
this._modalService.show(PatronTransactionEventFormComponent, {initialState});
}
/** Allow to pay the total of each pending patron transactions */
payAllTransactionsInMyLibrary() {
const initialState = {
action: 'pay',
mode: 'full',
transactions: this.myLibraryEngagedFees
};
this._modalService.show(PatronTransactionEventFormComponent, {initialState});
}

/**
* Behavior to perform when user asked to open a 'vertical tab'.
* Event handler when user click on a 'vertical tab'.
* All other tabs will be hidden.
* @param tabToOpen: the tab to open
*/
Expand All @@ -185,4 +172,27 @@ export class PatronTransactionsComponent implements OnInit, OnDestroy {
entry.isOpen = tabToOpen === entry;
}
}

/** Opening a modal to manually add a fee. */
addFee(): void {
this._modalRef = this._modalService.show(PatronFeeComponent, {
ignoreBackdropClick: true,
initialState: {
patronPid: this._patron.pid,
organisationPid: this._patron.organisation.pid
}
}
);
this._subscriptions.add(
this._modalRef.content.onSubmit
.pipe(first())
.subscribe(() => this._reloadEngagedFees())
);
}

// PRIVATE COMPONENTS FUNCTIONS =============================================
/** Notify than engaged fees for the current patron should be reloaded. */
private _reloadEngagedFees(): void {
this._patronTransactionService.emitPatronTransactionByPatron(this._patron.pid, undefined, 'open');
}
}
Loading

0 comments on commit d72ad5b

Please sign in to comment.