summaryrefslogtreecommitdiff
path: root/src/Actions/entity.ts
blob: d23022a8b7d5629036ef41a79132ba5cb83a0323 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

import { Dispatch, Store } from 'redux';
import { Region } from 'react-native-maps';
import { getTilesToLoad, loadTiles, getPortalDetails } from '../Api/api';
import { Portal, Link, Field } from '../Api/types';
import { decodePortal } from '../Api/entityDecoder';

const entity = {
    'update': (region: Region, width: number, refresh: boolean) => async (dispatch, getStore) => {
        const store = getStore()
        const queue = store.entities.loadQueue
        const loadedAlready = refresh ?
            [
                ...Object.keys(store.entities.portals),
                ...queue,
            ] : []
        const tiles = getTilesToLoad(region, width).filter(t => loadedAlready.indexOf(t) == -1)
        dispatch(entity.setLoadQueue([...queue, ...tiles]))
        setImmediate(() => dispatch(entity.loadRutine()))
    },
    'loadRutine': () => async (dispatch, getStore) => {
        const store = getStore()
        const queue = store.entities.loadQueue
        const chunk = queue.splice(0, 25)
        const { v, csrf } = store.auth
        const params = { v, csrf }
        loadTiles(chunk, params)
            .then(({ result, failed }) => {
                const queue = store.entities.loadQueue
                dispatch(entity.portalsSet(result.portals))
                dispatch(entity.linksSet(result.links))
                dispatch(entity.fieldsSet(result.fields))
                let newQueue = queue
                if (failed.length > 0) {
                    newQueue = [...queue, ...failed]
                }
                dispatch(entity.setLoadQueue([...newQueue]))
                if (newQueue.length > 0) {
                    setImmediate(() => dispatch(entity.loadRutine()))
                }
            })

    },
    'getPortalDetails': (guid: string) => async (dispatch, getStore) => {
        const store = getStore()
        const { v, csrf } = store.auth
        const params = { v, csrf }
        getPortalDetails(guid, params).then(async j => {
            const portal = decodePortal(j.result)
            portal.fullLoad = true
            dispatch(entity.portalSet(guid, portal))
            let addr = "нет"
            try {
                addr = await fetch(`https://nominatim.openstreetmap.org/reverse?format=json&polygon_geojson=1&lat=${portal.coords.latitude}&lon=${portal.coords.longitude}`)
                    .then(r => r.json())
                    .then(j => [j.address.city, j.address.road, j.address.house_number].filter(p => !!p).join(', ') || 'нет')
            } catch (e) {
                addr = "ошибка"
            }
            portal.address = addr
            dispatch(entity.portalSet(guid, portal))
        })
    },
    'setLoadQueue': (queue: string[][]) => ({
        type: 'setLoad',
        queue
    }),
    'portalsSet': (portals: { [guid: string]: Portal }) => ({
        type: 'portalsSet',
        portals,
    }),
    'linksSet': (links: { [guid: string]: Link }) => ({
        type: 'linksSet',
        links,
    }),
    'fieldsSet': (fields: { [guid: string]: Field }) => ({
        type: 'fieldsSet',
        fields,
    }),
    'portalSet': (guid: string, portal: Portal) => ({
        type: 'portalSet',
        guid, portal,
    }),
    'linkSet': (guid: string, link: Link) => ({
        type: 'linksSet',
        guid, link,
    }),
    'fieldSet': (guid: string, field: Field) => ({
        type: 'fieldsSet',
        guid, field,
    })
}
export default entity