Projekt

Obecné

Profil

Stáhnout (2.06 KB) Statistiky
| Větev: | Tag: | Revize:
1
// Business logic for tracking tool
2

    
3
import { CatalogItemDto, PathDto } from '../../../swagger/data-contracts'
4
import generateUuid from '../../../utils/id/uuidGenerator'
5

    
6
// For more comprehensive code alias CatalogItemDto[] as path variant
7
export type PathVariant = MapPoint[]
8

    
9
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
export interface MapPoint {
18
    id: string // unique id for react
19
    idx: number,
20
    active: boolean,
21
    catalogItem: CatalogItemDto,
22
    type: MapPointType
23
}
24

    
25
export const isMapPointDisplayable = (mapPoint: MapPoint): boolean =>
26
    !!mapPoint.catalogItem.latitude && !!mapPoint.catalogItem.longitude
27

    
28
/**
29
 * Cartesian product of two arrays
30
 * @param sets
31
 * @returns
32
 */
33
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
/**
43
 * Builds a list of all possible path variants from pathDto
44
 * @param pathDto
45
 * @returns
46
 */
47
export const buildPathVariants = (pathDto: PathDto, mapPointType: MapPointType = MapPointType.LocalCatalog): PathVariant[] => {
48
    if (!pathDto.foundCatalogItems) {
49
        return []
50
    }
51

    
52
    return (
53
        pathDto.foundCatalogItems.length === 1
54
            ? pathDto.foundCatalogItems
55
            : cartesianProduct(pathDto.foundCatalogItems)
56
    ).map((variant, _) =>
57
        variant.map(
58
            (catalogItem, idx) => (
59
                {
60
                    id: generateUuid(),
61
                    idx,
62
                    active: !!catalogItem.latitude && !!catalogItem.longitude,
63
                    catalogItem,
64
                    type: mapPointType,
65
                })
66
        )
67
    )
68
}
69

    
70
export default buildPathVariants
(4-4/4)