1 |
8c57f958
|
Vaclav Honzik
|
import { createAsyncThunk } from '@reduxjs/toolkit'
|
2 |
|
|
import axiosInstance from '../../api/api'
|
3 |
a7ae217f
|
Vaclav Honzik
|
import { RootState } from '../redux/store'
|
4 |
c0b66eaf
|
Vaclav Honzik
|
import { MapPoint, PathVariant } from './trackingToolUtils'
|
5 |
8c57f958
|
Vaclav Honzik
|
|
6 |
|
|
export const sendTextForProcessing = createAsyncThunk(
|
7 |
|
|
'trackingTool/sendTextForProcessing',
|
8 |
|
|
async (text: string) => {
|
9 |
|
|
try {
|
10 |
|
|
const { data, status } = await axiosInstance.post('path', { text })
|
11 |
|
|
if (status !== 200) {
|
12 |
|
|
return Promise.reject(
|
13 |
|
|
'Error while fetching map, please try again later'
|
14 |
|
|
)
|
15 |
|
|
}
|
16 |
|
|
return data
|
17 |
|
|
} catch (err: any) {
|
18 |
|
|
return Promise.reject('Error, server is currently unavailable')
|
19 |
|
|
}
|
20 |
|
|
}
|
21 |
|
|
)
|
22 |
a7ae217f
|
Vaclav Honzik
|
|
23 |
|
|
// export const mergeWithImportedPath = createAsyncThunk('trackingTool/mergeWithImportedPath', async (jsonPath: PathVariant, { getState }) => {
|
24 |
|
|
// if (!jsonPath) {
|
25 |
|
|
// return undefined
|
26 |
|
|
// }
|
27 |
|
|
|
28 |
|
|
// // Get current state
|
29 |
|
|
// const state = getState() as RootState
|
30 |
|
|
// const { primaryPathIdx, pathVariants } = state.trackingTool
|
31 |
|
|
|
32 |
|
|
// // Return undefined if there is no pathVariants or index is out of range
|
33 |
|
|
// if (!pathVariants || pathVariants.length === 0 || primaryPathIdx >= pathVariants.length) {
|
34 |
|
|
// return undefined
|
35 |
|
|
// }
|
36 |
|
|
|
37 |
|
|
// // Get the path and create a map to check whether some point with the same id already exists
|
38 |
|
|
// const path = pathVariants[primaryPathIdx]
|
39 |
|
|
// const pathMap = new Map(path.map((item) => [item.catalogItem.id as string, item]))
|
40 |
|
|
|
41 |
|
|
// // Create an array of items to be replaced and items to be added to the end
|
42 |
|
|
// const itemsToReplace: MapPoint[] = []
|
43 |
|
|
// const itemsToAdd: MapPoint[] = []
|
44 |
|
|
|
45 |
|
|
// jsonPath.forEach((item) => {
|
46 |
|
|
// if (pathMap.has(item.catalogItem.id as string)) {
|
47 |
|
|
// // @ts-ignore - we know that the id is a string and typescript refuses to acknowledge that
|
48 |
|
|
// item.idx = pathMap[item.catalogItem.id as string].idx
|
49 |
|
|
// itemsToReplace.push(item)
|
50 |
|
|
// } else {
|
51 |
|
|
// itemsToAdd.push(item)
|
52 |
|
|
// }
|
53 |
|
|
// })
|
54 |
|
|
|
55 |
|
|
// // Iterate over items to replace and replace them
|
56 |
|
|
// const newPath = [...path]
|
57 |
|
|
// itemsToReplace.forEach((item) => {
|
58 |
|
|
// newPath[item.idx] = item
|
59 |
|
|
// })
|
60 |
|
|
|
61 |
|
|
// // Add items to the end
|
62 |
|
|
// itemsToAdd.forEach((item) => {
|
63 |
|
|
// item.active = false
|
64 |
|
|
// item.idx = newPath.length
|
65 |
|
|
// newPath.push(item)
|
66 |
|
|
// })
|
67 |
|
|
|
68 |
|
|
// // Return the new path
|
69 |
|
|
// return newPath
|
70 |
|
|
// })
|