Browse Source

Pager to local store.

pull/450/head
Sebastian 6 years ago
parent
commit
907733f445
  1. 11
      frontend/app/features/administration/state/users.state.spec.ts
  2. 11
      frontend/app/features/administration/state/users.state.ts
  3. 25
      frontend/app/framework/utils/pager.spec.ts
  4. 18
      frontend/app/framework/utils/pager.ts
  5. 2
      frontend/app/shared/services/autosave.service.spec.ts
  6. 2
      frontend/app/shared/state/clients.state.spec.ts
  7. 2
      frontend/app/shared/state/clients.state.ts
  8. 22
      frontend/app/shared/state/contents.state.ts
  9. 6
      frontend/app/shared/state/contributors.state.spec.ts
  10. 17
      frontend/app/shared/state/contributors.state.ts
  11. 2
      frontend/app/shared/state/patterns.state.spec.ts
  12. 4
      frontend/app/shared/state/patterns.state.ts
  13. 2
      frontend/app/shared/state/roles.state.spec.ts
  14. 4
      frontend/app/shared/state/roles.state.ts
  15. 7
      frontend/app/shared/state/rule-events.state.spec.ts
  16. 11
      frontend/app/shared/state/rule-events.state.ts
  17. 2
      frontend/app/shared/state/workflows.state.spec.ts
  18. 4
      frontend/app/shared/state/workflows.state.ts

11
frontend/app/features/administration/state/users.state.spec.ts

@ -8,7 +8,11 @@
import { of, throwError } from 'rxjs'; import { of, throwError } from 'rxjs';
import { IMock, It, Mock, Times } from 'typemoq'; import { IMock, It, Mock, Times } from 'typemoq';
import { DialogService, Pager } from '@app/shared'; import {
DialogService,
LocalStoreService,
Pager
} from '@app/shared';
import { import {
UserDto, UserDto,
@ -29,14 +33,17 @@ describe('UsersState', () => {
const newUser = createUser(3); const newUser = createUser(3);
let dialogs: IMock<DialogService>; let dialogs: IMock<DialogService>;
let localStore: IMock<LocalStoreService>;
let usersService: IMock<UsersService>; let usersService: IMock<UsersService>;
let usersState: UsersState; let usersState: UsersState;
beforeEach(() => { beforeEach(() => {
dialogs = Mock.ofType<DialogService>(); dialogs = Mock.ofType<DialogService>();
localStore = Mock.ofType<LocalStoreService>();
usersService = Mock.ofType<UsersService>(); usersService = Mock.ofType<UsersService>();
usersState = new UsersState(dialogs.object, usersService.object); usersState = new UsersState(dialogs.object, localStore.object, usersService.object);
}); });
afterEach(() => { afterEach(() => {

11
frontend/app/features/administration/state/users.state.ts

@ -13,6 +13,7 @@ import '@app/framework/utils/rxjs-extensions';
import { import {
DialogService, DialogService,
LocalStoreService,
Pager, Pager,
shareSubscribed, shareSubscribed,
State State
@ -67,9 +68,17 @@ export class UsersState extends State<Snapshot> {
constructor( constructor(
private readonly dialogs: DialogService, private readonly dialogs: DialogService,
private readonly localStore: LocalStoreService,
private readonly usersService: UsersService private readonly usersService: UsersService
) { ) {
super({ users: [], usersPager: Pager.DEFAULT }); super({
users: [],
usersPager: Pager.fromLocalStore('users', localStore)
});
this.usersPager.subscribe(pager => {
pager.saveTo('users', this.localStore);
});
} }
public select(id: string | null): Observable<UserDto | null> { public select(id: string | null): Observable<UserDto | null> {

25
frontend/app/framework/utils/pager.spec.ts

@ -5,8 +5,12 @@
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/ */
import { Mock, Times } from 'typemoq';
import { Pager } from './pager'; import { Pager } from './pager';
import { LocalStoreService } from './../services/local-store.service';
describe('Pager', () => { describe('Pager', () => {
it('should init with default values', () => { it('should init with default values', () => {
const pager_1 = new Pager(0); const pager_1 = new Pager(0);
@ -211,4 +215,25 @@ describe('Pager', () => {
canGoPrev: false canGoPrev: false
}); });
}); });
it('should create pager from local store', () => {
const localStore = Mock.ofType<LocalStoreService>();
localStore.setup(x => x.getInt('my.pageSize', 15))
.returns((() => 25));
const pager = Pager.fromLocalStore('my', localStore.object, 15);
expect(pager.pageSize).toBe(25);
});
it('should save pager to local store', () => {
const localStore = Mock.ofType<LocalStoreService>();
const pager = new Pager(0, 0, 25);
pager.saveTo('my', localStore.object);
localStore.verify(x => x.setInt('my.pageSize', 25), Times.once());
});
}); });

18
frontend/app/framework/utils/pager.ts

@ -5,9 +5,9 @@
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/ */
export class Pager { import { LocalStoreService } from './../services/local-store.service';
public static readonly DEFAULT = new Pager(0);
export class Pager {
public canGoNext = false; public canGoNext = false;
public canGoPrev = false; public canGoPrev = false;
@ -36,6 +36,20 @@ export class Pager {
this.skip = page * pageSize; this.skip = page * pageSize;
} }
public static fromLocalStore(name: string, localStore: LocalStoreService, size = 10) {
let pageSize = localStore.getInt(`${name}.pageSize`, size);
if (pageSize < 0 || pageSize > 100) {
pageSize = size;
}
return new Pager(0, 0, pageSize);
}
public saveTo(name: string, localStore: LocalStoreService) {
localStore.setInt(`${name}.pageSize`, this.pageSize);
}
public goNext(): Pager { public goNext(): Pager {
if (!this.canGoNext) { if (!this.canGoNext) {
return this; return this;

2
frontend/app/shared/services/autosave.service.spec.ts

@ -19,7 +19,7 @@ describe('AutoSaveService', () => {
let autoSaveService: AutoSaveService; let autoSaveService: AutoSaveService;
beforeEach(() => { beforeEach(() => {
localStore = Mock.ofType(LocalStoreService); localStore = Mock.ofType<LocalStoreService>();
autoSaveService = new AutoSaveService(localStore.object); autoSaveService = new AutoSaveService(localStore.object);
}); });

2
frontend/app/shared/state/clients.state.spec.ts

@ -38,7 +38,7 @@ describe('ClientsState', () => {
dialogs = Mock.ofType<DialogService>(); dialogs = Mock.ofType<DialogService>();
clientsService = Mock.ofType<ClientsService>(); clientsService = Mock.ofType<ClientsService>();
clientsState = new ClientsState(clientsService.object, appsState.object, dialogs.object); clientsState = new ClientsState(appsState.object, clientsService.object, dialogs.object);
}); });
afterEach(() => { afterEach(() => {

2
frontend/app/shared/state/clients.state.ts

@ -54,8 +54,8 @@ export class ClientsState extends State<Snapshot> {
this.project(x => x.canCreate === true); this.project(x => x.canCreate === true);
constructor( constructor(
private readonly clientsService: ClientsService,
private readonly appsState: AppsState, private readonly appsState: AppsState,
private readonly clientsService: ClientsService,
private readonly dialogs: DialogService private readonly dialogs: DialogService
) { ) {
super({ clients: [], version: Version.EMPTY }); super({ clients: [], version: Version.EMPTY });

22
frontend/app/shared/state/contents.state.ts

@ -12,6 +12,7 @@ import { catchError, switchMap, tap } from 'rxjs/operators';
import { import {
DialogService, DialogService,
ErrorDto, ErrorDto,
LocalStoreService,
Pager, Pager,
shareSubscribed, shareSubscribed,
State, State,
@ -95,9 +96,18 @@ export abstract class ContentsStateBase extends State<Snapshot> {
constructor( constructor(
private readonly appsState: AppsState, private readonly appsState: AppsState,
private readonly contentsService: ContentsService, private readonly contentsService: ContentsService,
private readonly dialogs: DialogService private readonly dialogs: DialogService,
private readonly localStore: LocalStoreService
) { ) {
super({ contents: [], contentsPager: Pager.DEFAULT, contentsQueryJson: '' }); super({
contents: [],
contentsPager: Pager.fromLocalStore('contents', localStore),
contentsQueryJson: ''
});
this.contentsPager.subscribe(pager => {
pager.saveTo('contents', this.localStore);
});
} }
public select(id: string | null): Observable<ContentDto | null> { public select(id: string | null): Observable<ContentDto | null> {
@ -349,10 +359,10 @@ export abstract class ContentsStateBase extends State<Snapshot> {
@Injectable() @Injectable()
export class ContentsState extends ContentsStateBase { export class ContentsState extends ContentsStateBase {
constructor(appsState: AppsState, contentsService: ContentsService, dialogs: DialogService, constructor(appsState: AppsState, contentsService: ContentsService, dialogs: DialogService, localStore: LocalStoreService,
private readonly schemasState: SchemasState private readonly schemasState: SchemasState
) { ) {
super(appsState, contentsService, dialogs); super(appsState, contentsService, dialogs, localStore);
} }
protected get schemaId() { protected get schemaId() {
@ -365,9 +375,9 @@ export class ManualContentsState extends ContentsStateBase {
public schema: SchemaDto; public schema: SchemaDto;
constructor( constructor(
appsState: AppsState, contentsService: ContentsService, dialogs: DialogService appsState: AppsState, contentsService: ContentsService, dialogs: DialogService, localStore: LocalStoreService
) { ) {
super(appsState, contentsService, dialogs); super(appsState, contentsService, dialogs, localStore);
} }
protected get schemaId() { protected get schemaId() {

6
frontend/app/shared/state/contributors.state.spec.ts

@ -14,6 +14,7 @@ import {
ContributorsService, ContributorsService,
ContributorsState, ContributorsState,
DialogService, DialogService,
LocalStoreService,
Pager, Pager,
versioned versioned
} from '@app/shared/internal'; } from '@app/shared/internal';
@ -41,12 +42,15 @@ describe('ContributorsState', () => {
let dialogs: IMock<DialogService>; let dialogs: IMock<DialogService>;
let contributorsService: IMock<ContributorsService>; let contributorsService: IMock<ContributorsService>;
let contributorsState: ContributorsState; let contributorsState: ContributorsState;
let localStore: IMock<LocalStoreService>;
beforeEach(() => { beforeEach(() => {
dialogs = Mock.ofType<DialogService>(); dialogs = Mock.ofType<DialogService>();
localStore = Mock.ofType<LocalStoreService>();
contributorsService = Mock.ofType<ContributorsService>(); contributorsService = Mock.ofType<ContributorsService>();
contributorsState = new ContributorsState(contributorsService.object, appsState.object, dialogs.object); contributorsState = new ContributorsState(appsState.object, contributorsService.object, dialogs.object, localStore.object);
contributorsService.setup(x => x.getContributors(app)) contributorsService.setup(x => x.getContributors(app))
.returns(() => of(versioned(version, oldContributors))).verifiable(); .returns(() => of(versioned(version, oldContributors))).verifiable();

17
frontend/app/shared/state/contributors.state.ts

@ -12,6 +12,7 @@ import { catchError, tap } from 'rxjs/operators';
import { import {
DialogService, DialogService,
ErrorDto, ErrorDto,
LocalStoreService,
Pager, Pager,
shareMapSubscribed, shareMapSubscribed,
shareSubscribed, shareSubscribed,
@ -87,11 +88,21 @@ export class ContributorsState extends State<Snapshot> {
this.projectFrom2(this.contributorsPager, this.filtered, (p, c) => getPagedContributors(c, p)); this.projectFrom2(this.contributorsPager, this.filtered, (p, c) => getPagedContributors(c, p));
constructor( constructor(
private readonly contributorsService: ContributorsService,
private readonly appsState: AppsState, private readonly appsState: AppsState,
private readonly dialogs: DialogService private readonly contributorsService: ContributorsService,
private readonly dialogs: DialogService,
private readonly localStore: LocalStoreService
) { ) {
super({ contributors: [], contributorsPager: Pager.DEFAULT, maxContributors: -1, version: Version.EMPTY }); super({
contributors: [],
contributorsPager: Pager.fromLocalStore('contributors', localStore),
maxContributors: -1,
version: Version.EMPTY
});
this.contributorsPager.subscribe(pager => {
pager.saveTo('contributors', this.localStore);
});
} }
public load(isReload = false): Observable<any> { public load(isReload = false): Observable<any> {

2
frontend/app/shared/state/patterns.state.spec.ts

@ -38,7 +38,7 @@ describe('PatternsState', () => {
dialogs = Mock.ofType<DialogService>(); dialogs = Mock.ofType<DialogService>();
patternsService = Mock.ofType<PatternsService>(); patternsService = Mock.ofType<PatternsService>();
patternsState = new PatternsState(patternsService.object, appsState.object, dialogs.object); patternsState = new PatternsState(appsState.object, dialogs.object, patternsService.object);
}); });
afterEach(() => { afterEach(() => {

4
frontend/app/shared/state/patterns.state.ts

@ -54,9 +54,9 @@ export class PatternsState extends State<Snapshot> {
this.project(x => x.canCreate === true); this.project(x => x.canCreate === true);
constructor( constructor(
private readonly patternsService: PatternsService,
private readonly appsState: AppsState, private readonly appsState: AppsState,
private readonly dialogs: DialogService private readonly dialogs: DialogService,
private readonly patternsService: PatternsService
) { ) {
super({ patterns: [], version: Version.EMPTY }); super({ patterns: [], version: Version.EMPTY });
} }

2
frontend/app/shared/state/roles.state.spec.ts

@ -38,7 +38,7 @@ describe('RolesState', () => {
dialogs = Mock.ofType<DialogService>(); dialogs = Mock.ofType<DialogService>();
rolesService = Mock.ofType<RolesService>(); rolesService = Mock.ofType<RolesService>();
rolesState = new RolesState(rolesService.object, appsState.object, dialogs.object); rolesState = new RolesState(appsState.object, dialogs.object, rolesService.object);
}); });
describe('Loading', () => { describe('Loading', () => {

4
frontend/app/shared/state/roles.state.ts

@ -54,9 +54,9 @@ export class RolesState extends State<Snapshot> {
this.project(x => x.canCreate === true); this.project(x => x.canCreate === true);
constructor( constructor(
private readonly rolesService: RolesService,
private readonly appsState: AppsState, private readonly appsState: AppsState,
private readonly dialogs: DialogService private readonly dialogs: DialogService,
private readonly rolesService: RolesService
) { ) {
super({ roles: [], version: Version.EMPTY }); super({ roles: [], version: Version.EMPTY });
} }

7
frontend/app/shared/state/rule-events.state.spec.ts

@ -10,6 +10,7 @@ import { IMock, It, Mock, Times } from 'typemoq';
import { import {
DialogService, DialogService,
LocalStoreService,
Pager, Pager,
RuleEventsDto, RuleEventsDto,
RuleEventsState, RuleEventsState,
@ -34,16 +35,18 @@ describe('RuleEventsState', () => {
let dialogs: IMock<DialogService>; let dialogs: IMock<DialogService>;
let rulesService: IMock<RulesService>; let rulesService: IMock<RulesService>;
let ruleEventsState: RuleEventsState; let ruleEventsState: RuleEventsState;
let localStore: IMock<LocalStoreService>;
beforeEach(() => { beforeEach(() => {
dialogs = Mock.ofType<DialogService>(); dialogs = Mock.ofType<DialogService>();
rulesService = Mock.ofType<RulesService>(); localStore = Mock.ofType<LocalStoreService>();
rulesService = Mock.ofType<RulesService>();
rulesService.setup(x => x.getEvents(app, 10, 0, undefined)) rulesService.setup(x => x.getEvents(app, 10, 0, undefined))
.returns(() => of(new RuleEventsDto(200, oldRuleEvents))); .returns(() => of(new RuleEventsDto(200, oldRuleEvents)));
ruleEventsState = new RuleEventsState(appsState.object, dialogs.object, rulesService.object); ruleEventsState = new RuleEventsState(appsState.object, dialogs.object, localStore.object, rulesService.object);
ruleEventsState.load().subscribe(); ruleEventsState.load().subscribe();
}); });

11
frontend/app/shared/state/rule-events.state.ts

@ -11,6 +11,7 @@ import { tap } from 'rxjs/operators';
import { import {
DialogService, DialogService,
LocalStoreService,
Pager, Pager,
shareSubscribed, shareSubscribed,
State State
@ -48,9 +49,17 @@ export class RuleEventsState extends State<Snapshot> {
constructor( constructor(
private readonly appsState: AppsState, private readonly appsState: AppsState,
private readonly dialogs: DialogService, private readonly dialogs: DialogService,
private readonly localStore: LocalStoreService,
private readonly rulesService: RulesService private readonly rulesService: RulesService
) { ) {
super({ ruleEvents: [], ruleEventsPager: Pager.DEFAULT }); super({
ruleEvents: [],
ruleEventsPager: Pager.fromLocalStore('rule-events', localStore)
});
this.ruleEventsPager.subscribe(pager => {
pager.saveTo('rule-events', this.localStore);
});
} }
public load(isReload = false): Observable<any> { public load(isReload = false): Observable<any> {

2
frontend/app/shared/state/workflows.state.spec.ts

@ -38,7 +38,7 @@ describe('WorkflowsState', () => {
dialogs = Mock.ofType<DialogService>(); dialogs = Mock.ofType<DialogService>();
workflowsService = Mock.ofType<WorkflowsService>(); workflowsService = Mock.ofType<WorkflowsService>();
workflowsState = new WorkflowsState(workflowsService.object, appsState.object, dialogs.object); workflowsState = new WorkflowsState(appsState.object, dialogs.object, workflowsService.object);
}); });
afterEach(() => { afterEach(() => {

4
frontend/app/shared/state/workflows.state.ts

@ -56,9 +56,9 @@ export class WorkflowsState extends State<Snapshot> {
this.project(x => x.canCreate === true); this.project(x => x.canCreate === true);
constructor( constructor(
private readonly workflowsService: WorkflowsService,
private readonly appsState: AppsState, private readonly appsState: AppsState,
private readonly dialogs: DialogService private readonly dialogs: DialogService,
private readonly workflowsService: WorkflowsService
) { ) {
super({ errors: [], workflows: [], version: Version.EMPTY }); super({ errors: [], workflows: [], version: Version.EMPTY });
} }

Loading…
Cancel
Save