-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(spa): create core ui library (#1151)
- Loading branch information
1 parent
2607d76
commit fc13451
Showing
45 changed files
with
522 additions
and
430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"extends": [ | ||
"../../../../.eslintrc.json", | ||
"../../../../.eslintrc.angular.json" | ||
], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts"], | ||
"extends": [ | ||
"plugin:@nx/angular", | ||
"plugin:@angular-eslint/template/process-inline-templates" | ||
], | ||
"rules": { | ||
"@angular-eslint/directive-selector": [ | ||
"error", | ||
{ | ||
"type": "attribute", | ||
"prefix": "krd", | ||
"style": "camelCase" | ||
} | ||
], | ||
"@angular-eslint/component-selector": [ | ||
"error", | ||
{ | ||
"type": "element", | ||
"prefix": "krd", | ||
"style": "kebab-case" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"files": ["*.html"], | ||
"extends": ["plugin:@nx/angular-template"], | ||
"rules": {} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Common UI | ||
|
||
This library contains common UI components and some helper services that are | ||
used across multiple domains. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export default { | ||
displayName: 'spa-ui', | ||
preset: '../../../../jest.preset.js', | ||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'], | ||
coverageDirectory: '../../../../coverage/spa/core/ui', | ||
transform: { | ||
'^.+\\.(ts|mjs|js|html)$': [ | ||
'jest-preset-angular', | ||
{ | ||
tsconfig: '<rootDir>/tsconfig.spec.json', | ||
stringifyContentPathRegex: '\\.(html|svg)$', | ||
}, | ||
], | ||
}, | ||
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'], | ||
snapshotSerializers: [ | ||
'jest-preset-angular/build/serializers/no-ng-attributes', | ||
'jest-preset-angular/build/serializers/ng-snapshot', | ||
'jest-preset-angular/build/serializers/html-comment', | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "spa-ui", | ||
"$schema": "../../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "spa/core/ui/src", | ||
"prefix": "krd", | ||
"projectType": "library", | ||
"tags": [], | ||
"targets": { | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/spa/core/ui/jest.config.ts" | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nx/eslint:lint" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './lib/entity-search/index'; | ||
export * from './lib/status-badge.component'; | ||
export * from './lib/status-explanations'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Entity Search | ||
|
||
This library contains components and services to search entities such as units | ||
and alert-groups. Mainly, there are two types of components, selection | ||
components where a selection service manages a state of entities and each entity | ||
can only be selected once and autocomplete components, which is a simple search | ||
component. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export * from './service/match-strategies'; | ||
export * from './service/entity-search.service'; | ||
export * from './service/unit-selection.service'; | ||
export * from './service/alert-group-selection.service'; | ||
export * from './component/unit/unit-selection-option.component'; | ||
export * from './component/unit/units-select.component'; | ||
export * from './component/alert-group/alert-group-selection.component'; | ||
export * from './component/alert-group/alert-group-autocomplete.component'; | ||
export * from './component/alert-group/alert-group-selections.component'; |
49 changes: 49 additions & 0 deletions
49
libs/spa/core/ui/src/lib/entity-search/service/alert-group-selection.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { TypedDocumentNode } from 'apollo-angular'; | ||
|
||
import { AlertGroup, Query } from '@kordis/shared/model'; | ||
import { gql } from '@kordis/spa/core/graphql'; | ||
|
||
import { EntitySearchEngine } from './entity-search.service'; | ||
import { EntitySelectionSearchService } from './entity-selection-search.service'; | ||
import { alertGroupMatchesByName } from './match-strategies'; | ||
|
||
/* | ||
This service handles the selection of alert groups in a context where a unit can only be selected once. | ||
*/ | ||
@Injectable() | ||
export class PossibleAlertGroupSelectionsService extends EntitySelectionSearchService< | ||
AlertGroup, | ||
{ alertGroups: Query['alertGroups'] } | ||
> { | ||
protected override query: TypedDocumentNode<{ | ||
alertGroups: Query['alertGroups']; | ||
}> = gql` | ||
query { | ||
alertGroups { | ||
id | ||
name | ||
currentUnits { | ||
id | ||
name | ||
callSign | ||
status { | ||
status | ||
} | ||
assignment { | ||
__typename | ||
id | ||
name | ||
} | ||
} | ||
assignment { | ||
__typename | ||
id | ||
name | ||
} | ||
} | ||
} | ||
`; | ||
protected queryName = 'alertGroups' as const; | ||
protected searchService = new EntitySearchEngine(alertGroupMatchesByName); | ||
} |
47 changes: 47 additions & 0 deletions
47
libs/spa/core/ui/src/lib/entity-search/service/entity-search.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { EntitySearchEngine } from './entity-search.service'; | ||
|
||
describe('EntitySearchEngine', () => { | ||
let searchEngine: EntitySearchEngine<{ id: string; name: string }>; | ||
const mockMatchStrategy = jest.fn((entity, searchTerm) => | ||
entity.name.includes(searchTerm), | ||
); | ||
|
||
beforeEach(() => { | ||
searchEngine = new EntitySearchEngine(mockMatchStrategy); | ||
}); | ||
|
||
it('should set searchable entities', () => { | ||
const entities = [ | ||
{ id: '1', name: 'Entity One' }, | ||
{ id: '2', name: 'Entity Two' }, | ||
]; | ||
searchEngine.setSearchableEntities(entities); | ||
expect(searchEngine['searchableEntities']).toEqual(entities); | ||
}); | ||
|
||
it('should search entities based on the match strategy', () => { | ||
const entities = [ | ||
{ id: '1', name: 'Entity One' }, | ||
{ id: '2', name: 'Entity Two' }, | ||
{ id: '3', name: 'Another' }, | ||
]; | ||
searchEngine.setSearchableEntities(entities); | ||
|
||
const result = searchEngine.search('Entity'); | ||
expect(result).toEqual([ | ||
{ id: '1', name: 'Entity One' }, | ||
{ id: '2', name: 'Entity Two' }, | ||
]); | ||
}); | ||
|
||
it('should return an empty array if no entities match the search term', () => { | ||
const entities = [ | ||
{ id: '1', name: 'Entity One' }, | ||
{ id: '2', name: 'Entity Two' }, | ||
]; | ||
searchEngine.setSearchableEntities(entities); | ||
|
||
const result = searchEngine.search('Nonexistent'); | ||
expect(result).toEqual([]); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
libs/spa/core/ui/src/lib/entity-search/service/entity-search.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export interface IEntitySearchEngine<Entity> { | ||
search(searchTerm: string): Entity[]; | ||
setSearchableEntities(entities: Entity[]): void; | ||
} | ||
|
||
/* | ||
* A generic search engine for entities which is able to search through a list of entities that can change over time. | ||
*/ | ||
export class EntitySearchEngine<Entity = unknown> | ||
implements IEntitySearchEngine<Entity> | ||
{ | ||
private searchableEntities: Entity[] = []; | ||
|
||
constructor( | ||
private readonly entityMatchStrategy: ( | ||
entity: Entity, | ||
searchTerm: string, | ||
) => boolean, | ||
) {} | ||
|
||
setSearchableEntities(entities: Entity[]): void { | ||
this.searchableEntities = entities; | ||
} | ||
|
||
search(searchTerm: string): Entity[] { | ||
return this.searchableEntities.filter((entity) => | ||
this.entityMatchStrategy(entity, searchTerm), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.