Projekt

Obecné

Profil

Stáhnout (2.21 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
    addToPath: boolean, // whether to add the point to the path
21
    catalogItem: CatalogItemDto,
22
    type: MapPointType
23
    hidden?: boolean // if true the point will not be displayed on the map
24
}
25

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

    
29
/**
30
 * Cartesian product of two arrays
31
 * @param sets
32
 * @returns
33
 */
34
const cartesianProduct = (sets: CatalogItemDto[][]): CatalogItemDto[][] =>
35
    sets.reduce<CatalogItemDto[][]>(
36
        (results, ids) =>
37
            results
38
                .map((result) => ids.map((id) => [...result, id]))
39
                .reduce((nested, result) => [...nested, ...result]),
40
        [[]]
41
    )
42

    
43
/**
44
 * Builds a list of all possible path variants from pathDto
45
 * @param pathDto
46
 * @returns
47
 */
48
export const buildPathVariants = (pathDto: PathDto, mapPointType: MapPointType = MapPointType.LocalCatalog): PathVariant[] => {
49
    if (!pathDto.foundCatalogItems) {
50
        return []
51
    }
52

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

    
71
export default buildPathVariants
(3-3/3)