Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NINC-595][NINC-2329] Added a spinner to the register/update invoice button #638

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
}

editContract(contract: Contract): void {
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
Loading