import { ToastService } from '@/core/services/toast.service'; import { EntityState, EntityStore } from '@/core/state'; import { Injectable, InjectionToken } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { ISupplierInvoicesResponse } from '../models/invoices.io'; import { SupplierInvoicesService } from '../services'; export interface SupplierInvoiceState extends EntityState {} export const SUPPLIER_ID = new InjectionToken('SUPPLIER_ID'); @Injectable({ providedIn: 'root', }) export class SupplierInvoiceStore extends EntityStore< ISupplierInvoicesResponse, SupplierInvoiceState > { private readonly _inventoryState = { isRefreshing: false, }; constructor( private activeRoute: ActivatedRoute, private service: SupplierInvoicesService, private toastService: ToastService, ) { super({ loading: false, initialized: false, error: null, isRefreshing: false, entities: {}, selectedId: null, ids: [], }); this.initial(); } supplierId!: string; initial() {} getSingle(invoiceId: string) { if (this.entities()[invoiceId]) { return; } this.patchState({ loading: true }); this.service.getSingle(this.supplierId, invoiceId).subscribe({ next: (res) => { this.patchState({ entities: { [res.id]: res }, loading: false, }); }, error: (err) => { this.patchState({ loading: false, error: err }); this.toastService.error({ text: 'خطا در دریافت اطلاعات فاکتور', }); }, }); } refreshSingle(supplierId: string) {} reset(): void { const queryParams = this.activeRoute.snapshot.queryParams; this.setState({ loading: false, initialized: false, error: null, isRefreshing: false, entities: {}, selectedId: null, ids: [], }); } }