Skip to content
Open
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
2 changes: 1 addition & 1 deletion cypress/e2e/blog.cy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { loginAcc, blogEntry, local_items } from "../support/constants"
import * as moment from 'moment';
import moment from 'moment';

describe('/blog',{
viewportHeight: 800,
Expand Down
2 changes: 1 addition & 1 deletion cypress/e2e/dashboard.cy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { category_launched, init_config, init_stat, local_items, login_token, product_offering, checkHeaderPostLogin, checkHeaderPreLogin, loginAcc } from "../support/constants"
import * as moment from 'moment';
import moment from 'moment';

describe('/dashboard',{
viewportHeight: 800,
Expand Down
2 changes: 1 addition & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Router } from '@angular/router';
import { LoginInfo } from 'src/app/models/interfaces';
import { ApiServiceService } from 'src/app/services/product-service.service';
import { RefreshLoginServiceService } from "src/app/services/refresh-login-service.service";
import * as moment from 'moment';
import moment from 'moment';
import {ThemeService} from "./services/theme.service";
import {environment} from "../environments/environment";
import { filter } from 'rxjs';
Expand Down
2 changes: 1 addition & 1 deletion src/app/guard/auth.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TranslateModule } from '@ngx-translate/core';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
import * as moment from 'moment';
import moment from 'moment';
import { LocalStorageService } from '../services/local-storage.service';
import { AuthGuard } from './auth.guard';

Expand Down
2 changes: 1 addition & 1 deletion src/app/guard/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from
import {LocalStorageService} from "../services/local-storage.service";
import { Observable } from 'rxjs';
import { LoginInfo } from '../models/interfaces';
import * as moment from 'moment';
import moment from 'moment';
import { environment } from 'src/environments/environment';

@Injectable({
Expand Down
2 changes: 1 addition & 1 deletion src/app/interceptors/requests-interceptor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import * as moment from 'moment';
import moment from 'moment';
import { environment } from 'src/environments/environment';
import { PROVIDER_COUNTRY_LIST_URL } from '../models/search-organizations-filters.model';
import { LocalStorageService } from '../services/local-storage.service';
Expand Down
2 changes: 1 addition & 1 deletion src/app/interceptors/requests-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { Observable } from 'rxjs';
import {LocalStorageService} from "../services/local-storage.service";
import { LoginInfo } from '../models/interfaces';
import * as moment from 'moment';
import moment from 'moment';
import { environment } from 'src/environments/environment';

export function shouldAttachAuthHeaders(requestUrl: string, internalBaseUrls: string[]): boolean {
Expand Down
37 changes: 37 additions & 0 deletions src/app/models/formFields/form-field.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
interface BaseFormField {
name: string;
label: string;
required?: boolean;
colSpan?: number;
defaultValue?: any;
}

export interface StringFormField extends BaseFormField {
type: 'string';
maxLength?: number;
placeholder?: string;
}

export interface NumberFormField extends BaseFormField {
type: 'number';
min?: number;
max?: number;
step?: number;
}

export interface SelectOption {
value: string;
label: string;
}

export interface SelectableFormField extends BaseFormField {
type: 'select';
options: SelectOption[];
multiple?: boolean;
}

export interface BooleanFormField extends BaseFormField {
type: 'boolean';
}

export type FormField = StringFormField | NumberFormField | SelectableFormField | BooleanFormField;
196 changes: 196 additions & 0 deletions src/app/models/paginated-list.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import { TestBed } from '@angular/core/testing';
import { PaginatedList } from './paginated-list';

describe('PaginatedList', () => {
const PAGE_SIZE = 10;

function makeItems(count: number, offset = 0): string[] {
return Array.from({ length: count }, (_, i) => `item-${offset + i}`);
}

beforeEach(() => {
TestBed.configureTestingModule({});
});

it('should start with empty items, loading false and hasMore false', () => {
const list = new PaginatedList(() => Promise.resolve([]), PAGE_SIZE);
expect(list.items()).toEqual([]);
expect(list.loading()).toBeFalse();
expect(list.loadingMore()).toBeFalse();
expect(list.hasMore()).toBeFalse();
});

it('load should set loading=true during fetch and false when done', async () => {
const loadingStates: boolean[] = [];
const fetcher = jasmine.createSpy().and.callFake(() => {
loadingStates.push(list.loading());
return Promise.resolve([]);
});
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();
expect(loadingStates[0]).toBeTrue();
expect(list.loading()).toBeFalse();
});

it('load should populate items from the first fetch call', async () => {
const page0 = makeItems(10);
const fetcher = jasmine.createSpy()
.and.returnValues(Promise.resolve(page0), Promise.resolve([]));
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();
expect(list.items()).toEqual(page0);
});

it('load should set hasMore=true when prefetch returns non-null items', async () => {
const fetcher = jasmine.createSpy()
.and.returnValues(Promise.resolve(makeItems(10)), Promise.resolve(makeItems(10, 10)));
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();
expect(list.hasMore()).toBeTrue();
});

it('load should set hasMore=false when prefetch returns empty array', async () => {
const fetcher = jasmine.createSpy()
.and.returnValues(Promise.resolve(makeItems(5)), Promise.resolve([]));
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();
expect(list.hasMore()).toBeFalse();
});

it('load should set hasMore=false when prefetch returns array of nulls', async () => {
const fetcher = jasmine.createSpy()
.and.returnValues(Promise.resolve(makeItems(5)), Promise.resolve([null, null] as any));
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();
expect(list.hasMore()).toBeFalse();
});

it('load should reset items and state on reload', async () => {
const fetcher = jasmine.createSpy()
.and.returnValues(
Promise.resolve(makeItems(10)), // first load: page 0
Promise.resolve(makeItems(10, 10)), // first load: prefetch
Promise.resolve(makeItems(10, 20)), // loadMore: next prefetch
Promise.resolve(makeItems(3, 30)), // second load: page 0
Promise.resolve([]) // second load: prefetch
);
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();
await list.loadMore();
await list.load();
expect(list.items()).toEqual(makeItems(3, 30));
});

it('load should set loading=false even when fetcher rejects', async () => {
const fetcher = jasmine.createSpy().and.returnValue(Promise.reject(new Error('fail')));
const list = new PaginatedList(fetcher, PAGE_SIZE);
try {
await list.load();
} catch {
// expected
}
expect(list.loading()).toBeFalse();
});

it('loadMore should do nothing when hasMore is false', async () => {
const fetcher = jasmine.createSpy()
.and.returnValues(Promise.resolve(makeItems(5)), Promise.resolve([]));
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();
const callCount = fetcher.calls.count();

await list.loadMore();
expect(fetcher.calls.count()).toBe(callCount);
expect(list.items()).toEqual(makeItems(5));
});

it('loadMore should set loadingMore=true during fetch and false when done', async () => {
let resolveSecond!: (v: string[]) => void;
const fetcher = jasmine.createSpy()
.and.returnValues(
Promise.resolve(makeItems(10)),
Promise.resolve(makeItems(10, 10)),
new Promise<string[]>(r => { resolveSecond = r; })
);
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();

const morePromise = list.loadMore();
expect(list.loadingMore()).toBeTrue();
resolveSecond([]);
await morePromise;
expect(list.loadingMore()).toBeFalse();
});

it('loadMore should append prefetched items to existing items', async () => {
const page0 = makeItems(10);
const page1 = makeItems(10, 10);
const fetcher = jasmine.createSpy()
.and.returnValues(
Promise.resolve(page0),
Promise.resolve(page1),
Promise.resolve([])
);
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();
await list.loadMore();
expect(list.items()).toEqual([...page0, ...page1]);
});

it('loadMore should update hasMore based on next prefetch result', async () => {
const fetcher = jasmine.createSpy()
.and.returnValues(
Promise.resolve(makeItems(10)),
Promise.resolve(makeItems(10, 10)),
Promise.resolve(makeItems(5, 20)),
Promise.resolve([])
);
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();
expect(list.hasMore()).toBeTrue();
await list.loadMore();
expect(list.hasMore()).toBeTrue();
await list.loadMore();
expect(list.hasMore()).toBeFalse();
});

it('loadMore should set loadingMore=false even when fetcher rejects', async () => {
const fetcher = jasmine.createSpy()
.and.returnValues(
Promise.resolve(makeItems(10)),
Promise.resolve(makeItems(10, 10)),
Promise.reject(new Error('fail'))
);
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();
try {
await list.loadMore();
} catch {
// expected
}
expect(list.loadingMore()).toBeFalse();
});

it('load should call fetcher with page 0 first, then pageSize', async () => {
const fetcher = jasmine.createSpy()
.and.returnValues(Promise.resolve([]), Promise.resolve([]));
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();
expect(fetcher.calls.argsFor(0)).toEqual([0]);
expect(fetcher.calls.argsFor(1)).toEqual([PAGE_SIZE]);
});

it('loadMore should advance the page cursor on each call', async () => {
const fetcher = jasmine.createSpy()
.and.returnValues(
Promise.resolve(makeItems(10)),
Promise.resolve(makeItems(10, 10)),
Promise.resolve(makeItems(10, 20)),
Promise.resolve([])
);
const list = new PaginatedList(fetcher, PAGE_SIZE);
await list.load();
await list.loadMore();
expect(fetcher.calls.argsFor(2)).toEqual([PAGE_SIZE * 2]);
});
});
49 changes: 49 additions & 0 deletions src/app/models/paginated-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { signal } from '@angular/core';

export class PaginatedList<T> {
readonly items = signal<T[]>([]);
readonly loading = signal(false);
readonly loadingMore = signal(false);
readonly hasMore = signal(false);

private page = 0;
private prefetched: T[] = [];
private readonly fetcher: (page: number) => Promise<T[]>;
private readonly pageSize: number;

constructor(fetcher: (page: number) => Promise<T[]>, pageSize: number) {
this.fetcher = fetcher;
this.pageSize = pageSize;
}

async load(): Promise<void> {
this.loading.set(true);
this.page = 0;
this.items.set([]);
this.prefetched = [];

try {
this.items.set(await this.fetcher(0));
this.page = this.pageSize;
this.prefetched = await this.fetcher(this.page);
this.page += this.pageSize;
this.hasMore.set(this.prefetched.some(item => item != null));
} finally {
this.loading.set(false);
}
}

async loadMore(): Promise<void> {
if (!this.hasMore()) return;
this.loadingMore.set(true);

try {
this.items.set([...this.items(), ...this.prefetched]);
this.prefetched = await this.fetcher(this.page);
this.page += this.pageSize;
this.hasMore.set(this.prefetched.some(item => item != null));
} finally {
this.loadingMore.set(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {EventMessageService} from "src/app/services/event-message.service";
import { LoginInfo } from 'src/app/models/interfaces';
import { initFlowbite } from 'flowbite';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import * as moment from 'moment';
import moment from 'moment';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {EventMessageService} from "src/app/services/event-message.service";
import { LoginInfo } from 'src/app/models/interfaces';
import { initFlowbite } from 'flowbite';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import * as moment from 'moment';
import moment from 'moment';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/catalogs/catalogs.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ <h1 data-cy="cataloguesAllHeader" class="mb-2 text-center mt-4 text-4xl font-ext
</div>
}
} @else {
<div role="status" class="w-full h-full pt-8 flex justify-center align-middle">
<div role="status" data-cy="loadMoreLoading" class="w-full h-full pt-8 flex justify-center align-middle">
<svg aria-hidden="true" class="w-12 h-12 text-gray-200 animate-spin dark:text-gray-600 fill-secondary-400" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
Expand Down
Loading