Skip to content

Commit d73b2d6

Browse files
author
Gérard Collin
committed
fix: Workflow sort
1 parent c6d1c1e commit d73b2d6

4 files changed

Lines changed: 45 additions & 15 deletions

File tree

libs/dc-workflow/projects/dc-workflow/src/lib/abstract/abstract-dc-workflow.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { DcWorkflow } from '../definition/dc-workflow';
22
import { Component, inject, input, InputSignal } from '@angular/core';
3-
import { DcWorkflowModel } from '../models/dc-workflow-model';
3+
import { DcWorkflowModel, DcWorkflowSortModel, DcWorkflowSortOption } from '../models/dc-workflow-model';
44
import { XtCompositeComponent, XtResolverService } from 'xt-components';
5-
import { XtSignalStore, XtStoreManagerService } from 'xt-store';
5+
import { XtSignalStore, XtSortBy, XtSortByDirection, XtStoreEntityFeatureOptions, XtStoreManagerService } from 'xt-store';
66
import { ManagedData } from 'xt-type';
77

88
/**
@@ -13,7 +13,7 @@ import { ManagedData } from 'xt-type';
1313
imports: [],
1414
template: ''
1515
})
16-
export class AbstractDcWorkflow<T extends ManagedData> extends XtCompositeComponent<T> implements DcWorkflow {
16+
export class AbstractDcWorkflow<T extends ManagedData=ManagedData> extends XtCompositeComponent<T> implements DcWorkflow {
1717
/**
1818
* The workflow config must be provided
1919
* @protected
@@ -27,9 +27,9 @@ export class AbstractDcWorkflow<T extends ManagedData> extends XtCompositeCompon
2727

2828
protected findStore (): XtSignalStore<T> | null {
2929
if (this.store===undefined) {
30-
this.store=this.storeMgr.getStoreFor(this.config().entity, this.resolver.typeResolver);
31-
if (this.store!=null)
32-
this.applyConfigToStore (this.config(), this.store);
30+
const config = this.config();
31+
const storeOptions = this.generateStoreOptions(config);
32+
this.store=this.storeMgr.getStoreFor(config.entity, this.resolver.typeResolver, storeOptions);
3333
}
3434
return (this.store==null)?null:this.store;
3535
}
@@ -40,7 +40,29 @@ export class AbstractDcWorkflow<T extends ManagedData> extends XtCompositeCompon
4040
return ret;
4141
}
4242

43-
protected applyConfigToStore (config: DcWorkflowModel, store:XtSignalStore<T>): void {
43+
protected generateStoreOptions (config: DcWorkflowModel): XtStoreEntityFeatureOptions<T> | undefined {
44+
const options = {sort:[]} as XtStoreEntityFeatureOptions<T>;
45+
if( config.data?.sort!=null) {
46+
for (const sortKey in config.data.sort) {
47+
options.sort?.push(this.toSortOption(sortKey, config.data.sort[sortKey]));
48+
}
49+
}
4450

51+
return (options.sort!.length>0)?options:undefined;
52+
}
53+
54+
protected toSortOption (name:string, sort:DcWorkflowSortOption): XtSortBy<T> {
55+
const ret = { by:name } as XtSortBy<T>;
56+
57+
if (sort==="ascending" || sort==="descending" ) {
58+
ret.direction=(sort==="ascending")?XtSortByDirection.Ascending:XtSortByDirection.None;
59+
} else if (sort==null){
60+
ret.direction=XtSortByDirection.None;
61+
} else if (sort.type=='metadata') {
62+
throw new Error ("Metadata sort is not supported yet for element "+name);
63+
} else {
64+
ret.direction=(sort.direction==="ascending")?XtSortByDirection.Ascending:XtSortByDirection.Descending;
65+
}
66+
return ret;
4567
}
4668
}

libs/dc-workflow/projects/dc-workflow/src/lib/models/dc-workflow-model.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ export type DcWorkflowModel = {
99
}
1010

1111
export type DcWorkflowSortModel = {
12-
[key in DcMetadataTypes|string]: 'ascending' | 'descending' | {
12+
[key in DcMetadataTypes|string]: DcWorkflowSortOption
13+
}
14+
15+
export type DcWorkflowSortOption = 'ascending' | 'descending' | {
1316
direction: 'ascending' | 'descending',
1417
type: 'metadata'|'field',
1518
}
16-
}
1719

1820
export type DcWorkflowDisplayModel = {
1921
fields?: {

libs/xt-store/projects/store/src/store-entity/store-entity-feature.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ export type XtSignalStore<T> = {
6262
* @param storeProvider
6363
* @param storeMgr
6464
* @param typeRegistry
65+
* @param options
6566
*/
66-
export function withXtStoreProvider<T extends ManagedData = ManagedData> (entityName:string, storeProvider?:XtStoreProvider<T>, storeMgr?:XtStoreManager, typeRegistry?: XtTypeResolver, options?: XtStoreEntityFeatureOptions) {
67+
export function withXtStoreProvider<T extends ManagedData = ManagedData> (entityName:string, storeProvider?:XtStoreProvider<T>, storeMgr?:XtStoreManager, typeRegistry?: XtTypeResolver, options?: XtStoreEntityFeatureOptions<T>) {
6768
return signalStoreFeature(
68-
withState ({ entityName, loading:false, sort:options?.sort, filter:options?.filter} as StoreState),
69+
withState ({ entityName, loading:false, sort:options?.sort, filter:options?.filter} as StoreState<T>),
6970
withEntities(xtStoreEntityConfig<T> ()),
7071
withProps ( () => ({
7172
_storeProvider:storeProvider??storeMgr!.getProviderSafe<T>(entityName),
@@ -165,6 +166,11 @@ export function withXtStoreProvider<T extends ManagedData = ManagedData> (entity
165166
}
166167
},
167168

169+
async updateStoreOptions (option:XtStoreEntityFeatureOptions<T>):Promise<void> {
170+
patchState(store, {sort:option.sort, filter:option.filter});
171+
await this.fetchEntities();
172+
},
173+
168174
/**
169175
* Detects and replace all referenced objects by the key value that will be stored.
170176
* @param toClear

libs/xt-store/projects/store/src/store-manager-service/xt-store-manager.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
22
import { signalStore } from '@ngrx/signals';
33
import { ManagedData, XtTypeResolver } from 'xt-type';
44
import { xtStoreManager } from '../store-manager/xt-store-manager';
5-
import { withXtStoreProvider, XtSignalStore } from '../store-entity/store-entity-feature';
5+
import { withXtStoreProvider, XtSignalStore, XtStoreEntityFeatureOptions } from '../store-entity/store-entity-feature';
66
import { XtStoreProvider } from '../store-provider/xt-store-provider';
77

88
@Injectable({
@@ -16,7 +16,7 @@ export class XtStoreManagerService {
1616
constructor() {
1717
}
1818

19-
getStoreFor<T extends ManagedData>(entityName: string, typeMgr?:XtTypeResolver): XtSignalStore<T> {
19+
getStoreFor<T extends ManagedData>(entityName: string, typeMgr?:XtTypeResolver, options?:XtStoreEntityFeatureOptions<T>): XtSignalStore<T> {
2020
let store = this.entityToStoreMap.get(entityName);
2121
if (store == null) {
2222
const provider = this.storeManager.getProvider<T>(entityName);
@@ -25,13 +25,13 @@ export class XtStoreManagerService {
2525
} else {
2626
if( typeMgr==null) {
2727
const res = signalStore(
28-
withXtStoreProvider(entityName, provider)
28+
withXtStoreProvider(entityName, provider, undefined, undefined, options)
2929
);
3030
store= new res();
3131
} else {
3232
// We have a type mgr, so let's use it in our store
3333
const res = signalStore(
34-
withXtStoreProvider(entityName, provider, this.storeManager, typeMgr)
34+
withXtStoreProvider(entityName, provider, this.storeManager, typeMgr, options)
3535
);
3636
store= new res();
3737

0 commit comments

Comments
 (0)