mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-28 22:42:11 +01:00
feature(crm-data-access): Added check in customer resource if customerId has changed to prevent flickering in the view after Filter changes etc. Ref: #5478
76 lines
1.8 KiB
TypeScript
76 lines
1.8 KiB
TypeScript
import {
|
|
effect,
|
|
inject,
|
|
Injectable,
|
|
resource,
|
|
signal,
|
|
untracked,
|
|
} from '@angular/core';
|
|
import { CrmSearchService, CrmTabMetadataService } from '../services';
|
|
import { TabService } from '@isa/core/tabs';
|
|
import { Customer } from '../schemas';
|
|
|
|
@Injectable()
|
|
export class CustomerResource {
|
|
#customerService = inject(CrmSearchService);
|
|
|
|
protected _params = signal<{
|
|
customerId: number | undefined;
|
|
eagerLoading: number;
|
|
}>({
|
|
customerId: undefined,
|
|
eagerLoading: 3,
|
|
});
|
|
|
|
params(params: { customerId?: number; eagerLoading?: number }) {
|
|
this._params.update((p) => ({ ...p, ...params }));
|
|
}
|
|
|
|
readonly resource = resource({
|
|
params: () => this._params(),
|
|
loader: async ({ params, abortSignal }): Promise<Customer | undefined> => {
|
|
if (!params.customerId) {
|
|
return undefined;
|
|
}
|
|
|
|
const res = await this.#customerService.fetchCustomer(
|
|
{
|
|
customerId: params.customerId,
|
|
eagerLoading: params.eagerLoading,
|
|
},
|
|
abortSignal,
|
|
);
|
|
|
|
return res.result as Customer;
|
|
},
|
|
});
|
|
}
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class SelectedCustomerResource extends CustomerResource {
|
|
#tabId = inject(TabService).activatedTabId;
|
|
#customerMetadata = inject(CrmTabMetadataService);
|
|
|
|
constructor() {
|
|
super();
|
|
effect(() => {
|
|
const tabId = this.#tabId();
|
|
let customerId = undefined;
|
|
|
|
if (tabId) {
|
|
customerId = this.#customerMetadata.selectedCustomerId(tabId);
|
|
}
|
|
|
|
untracked(() => {
|
|
if (this._params().customerId !== customerId) {
|
|
this.params({ customerId });
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
setEagerLoading(eagerLoading: number) {
|
|
this.params({ eagerLoading });
|
|
}
|
|
}
|