Skip to content

Commit

Permalink
[NINC-595][NINC-2329] Added a spinner to the register/update invoice …
Browse files Browse the repository at this point in the history
…button
  • Loading branch information
AyrtonOuriques2 authored and augustoicaro committed Oct 31, 2024
1 parent 63e8f22 commit 9ea9160
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2552,11 +2552,13 @@
</form>
<button
(click)="editing ? confirmationDialog() : registerInvoice()"
[nbSpinner]="isLoading"
nbButton
fullWidth
status="primary"
size="large"
[disabled]="!form.valid || isNotEdited()"
[ngStyle]="{ height: isLoading ? '48px' : 'auto' }"
[disabled]="!form.valid || isNotEdited() || isLoading"
>
{{ editing ? 'Atualizar' : 'Cadastrar' }}
{{ isLoading ? '' : (editing ? 'Atualizar' : 'Cadastrar') }}
</button>
47 changes: 28 additions & 19 deletions src/app/pages/invoices/invoice-item/invoice-item.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export class InvoiceItemComponent implements OnInit, OnDestroy, AfterViewInit {
revision = 0;
validation = invoice_validation as any;
oldStatus: INVOICE_STATOOS = INVOICE_STATOOS.EM_ANALISE;
isLoading = false;

currentUser: User = new User();
isInputDisabled$ = new BehaviorSubject<boolean>(true);
Expand Down Expand Up @@ -378,7 +379,13 @@ export class InvoiceItemComponent implements OnInit, OnDestroy, AfterViewInit {
: null;
}

emitAndStopSpinner(): void {
this.isLoading = false;
this.submit.emit();
}

registerInvoice(): void {
this.isLoading = true;
if (this.editing) {
if (!isEqual(this.iInvoice, this.tempInvoice)) {
if (!this.tempInvoice.team.map((member) => member.distribution).every((distribution) => distribution !== ''))
Expand All @@ -393,37 +400,40 @@ export class InvoiceItemComponent implements OnInit, OnDestroy, AfterViewInit {
start: this.tempInvoice.lastUpdate,
});
}
this.invoiceService.editInvoice(this.tempInvoice);
if (this.oldStatus !== this.tempInvoice.status) {
if (this.tempInvoice.status === INVOICE_STATOOS.FECHADO) {
this.contractService.saveContract(this.tempInvoice);
this.notifyInvoiceTeam();
this.notifyAllUsers();
}
}
this.invoiceService.editInvoice(
this.tempInvoice,
(() => {
if (this.oldStatus !== this.tempInvoice.status && this.tempInvoice.status === INVOICE_STATOOS.FECHADO) {
this.contractService.saveContract(this.tempInvoice, this.emitAndStopSpinner.bind(this));
this.notifyInvoiceTeam();
this.notifyAllUsers();
} else this.emitAndStopSpinner();
}).bind(this)
);
this.updateObjVersion.emit();
if (this.tempInvoice.__v !== undefined) {
this.tempInvoice.__v += 1;
}
this.iInvoice = cloneDeep(this.tempInvoice);
this.isFormDirty.next(false);
}
} else {
this.tempInvoice.lastUpdate = new Date();
this.tempInvoice.statusHistory.push({
status: this.tempInvoice.status,
start: this.tempInvoice.created,
});
this.invoiceService.saveInvoice(this.tempInvoice, (savedInvoice: Invoice) => {
if (savedInvoice.status === INVOICE_STATOOS.FECHADO) {
this.contractService.saveContract(savedInvoice);
this.notifyInvoiceTeam();
this.notifyAllUsers();
}
});
this.isFormDirty.next(false);
this.submit.emit();
this.invoiceService.saveInvoice(
this.tempInvoice,
((savedInvoice: Invoice) => {
if (savedInvoice.status === INVOICE_STATOOS.FECHADO) {
this.contractService.saveContract(savedInvoice, this.emitAndStopSpinner.bind(this));
this.notifyInvoiceTeam();
this.notifyAllUsers();
} else this.emitAndStopSpinner();
}).bind(this)
);
}
this.isFormDirty.next(false);
}

updateLastValues(): void {
Expand Down Expand Up @@ -531,7 +541,6 @@ export class InvoiceItemComponent implements OnInit, OnDestroy, AfterViewInit {
.subscribe((response) => {
if (response) {
this.registerInvoice();
this.submit.emit();
}
this.isDialogBlocked.next(false);
});
Expand Down
7 changes: 5 additions & 2 deletions src/app/shared/services/contract.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class ContractService implements OnDestroy {
this.destroy$.complete();
}

saveContract(invoice: Invoice): void {
saveContract(invoice: Invoice, callback?: () => void): void {
const contract = new Contract();
contract.statusHistory.push({
status: contract.status,
Expand All @@ -163,7 +163,10 @@ export class ContractService implements OnDestroy {
this.http
.post('/api/contract/', req)
.pipe(take(1))
.subscribe(() => this.onedrive.copyModelFolder(invoice));
.subscribe(() => {
this.onedrive.copyModelFolder(invoice);
if (callback) callback();
});
}

isEqual(c1: string | Contract | undefined, c2: string | Contract | undefined): boolean {
Expand Down
9 changes: 7 additions & 2 deletions src/app/shared/services/invoice.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ export class InvoiceService implements OnDestroy {
});
}

editInvoice(invoice: Invoice): void {
editInvoice(invoice: Invoice, callback?: () => void): void {
const req = {
invoice: invoice,
};
this.http.post('/api/invoice/update', req).pipe(take(1)).subscribe();
this.http
.post('/api/invoice/update', req)
.pipe(take(1))
.subscribe(() => {
if (callback) callback();
});
this.submittedToEdit$.next();
}

Expand Down

0 comments on commit 9ea9160

Please sign in to comment.