1 |
91fd3fa6
|
Vaclav Honzik
|
// Business logic for tracking tool
|
2 |
|
|
|
3 |
812b9f90
|
Vaclav Honzik
|
import { CatalogItemDto, PathDto } from '../../../swagger/data-contracts'
|
4 |
|
|
import generateUuid from '../../../utils/id/uuidGenerator'
|
5 |
91fd3fa6
|
Vaclav Honzik
|
|
6 |
|
|
// For more comprehensive code alias CatalogItemDto[] as path variant
|
7 |
de12c6be
|
Vaclav Honzik
|
export type PathVariant = MapPoint[]
|
8 |
91fd3fa6
|
Vaclav Honzik
|
|
9 |
812b9f90
|
Vaclav Honzik
|
export enum MapPointType {
|
10 |
|
|
LocalCatalog, // Fetched from local catalog
|
11 |
|
|
ExternalCatalog, // Fetched from external catalog
|
12 |
|
|
GeoJson, // From GeoJSON file
|
13 |
|
|
FromCoordinates, // From coordinates
|
14 |
|
|
}
|
15 |
|
|
|
16 |
|
|
// Represents a point on the map - wrapper for CatalogItemDto to make it easier to work with
|
17 |
a7ae217f
|
Vaclav Honzik
|
export interface MapPoint {
|
18 |
812b9f90
|
Vaclav Honzik
|
id: string // unique id for react
|
19 |
a7ae217f
|
Vaclav Honzik
|
idx: number,
|
20 |
|
|
active: boolean,
|
21 |
|
|
catalogItem: CatalogItemDto,
|
22 |
812b9f90
|
Vaclav Honzik
|
type: MapPointType
|
23 |
de12c6be
|
Vaclav Honzik
|
}
|
24 |
|
|
|
25 |
a7ae217f
|
Vaclav Honzik
|
export const isMapPointDisplayable = (mapPoint: MapPoint): boolean =>
|
26 |
|
|
!!mapPoint.catalogItem.latitude && !!mapPoint.catalogItem.longitude
|
27 |
|
|
|
28 |
de12c6be
|
Vaclav Honzik
|
/**
|
29 |
|
|
* Cartesian product of two arrays
|
30 |
|
|
* @param sets
|
31 |
|
|
* @returns
|
32 |
|
|
*/
|
33 |
91fd3fa6
|
Vaclav Honzik
|
const cartesianProduct = (sets: CatalogItemDto[][]): CatalogItemDto[][] =>
|
34 |
|
|
sets.reduce<CatalogItemDto[][]>(
|
35 |
|
|
(results, ids) =>
|
36 |
|
|
results
|
37 |
|
|
.map((result) => ids.map((id) => [...result, id]))
|
38 |
|
|
.reduce((nested, result) => [...nested, ...result]),
|
39 |
|
|
[[]]
|
40 |
|
|
)
|
41 |
|
|
|
42 |
de12c6be
|
Vaclav Honzik
|
/**
|
43 |
|
|
* Builds a list of all possible path variants from pathDto
|
44 |
|
|
* @param pathDto
|
45 |
|
|
* @returns
|
46 |
|
|
*/
|
47 |
812b9f90
|
Vaclav Honzik
|
export const buildPathVariants = (pathDto: PathDto, mapPointType: MapPointType = MapPointType.LocalCatalog): PathVariant[] => {
|
48 |
91fd3fa6
|
Vaclav Honzik
|
if (!pathDto.foundCatalogItems) {
|
49 |
|
|
return []
|
50 |
|
|
}
|
51 |
|
|
|
52 |
de12c6be
|
Vaclav Honzik
|
return (
|
53 |
|
|
pathDto.foundCatalogItems.length === 1
|
54 |
|
|
? pathDto.foundCatalogItems
|
55 |
|
|
: cartesianProduct(pathDto.foundCatalogItems)
|
56 |
|
|
).map((variant, _) =>
|
57 |
|
|
variant.map(
|
58 |
a7ae217f
|
Vaclav Honzik
|
(catalogItem, idx) => (
|
59 |
|
|
{
|
60 |
812b9f90
|
Vaclav Honzik
|
id: generateUuid(),
|
61 |
de12c6be
|
Vaclav Honzik
|
idx,
|
62 |
a7ae217f
|
Vaclav Honzik
|
active: !!catalogItem.latitude && !!catalogItem.longitude,
|
63 |
812b9f90
|
Vaclav Honzik
|
catalogItem,
|
64 |
|
|
type: mapPointType,
|
65 |
a7ae217f
|
Vaclav Honzik
|
})
|
66 |
de12c6be
|
Vaclav Honzik
|
)
|
67 |
|
|
)
|
68 |
91fd3fa6
|
Vaclav Honzik
|
}
|
69 |
|
|
|
70 |
|
|
export default buildPathVariants
|