1
|
import { createAsyncThunk } from '@reduxjs/toolkit'
|
2
|
import axiosInstance from '../../api/api'
|
3
|
import { Path } from './trackingToolUtils'
|
4
|
|
5
|
/**
|
6
|
* Sends post request to process the text
|
7
|
*/
|
8
|
export const sendTextForProcessing = createAsyncThunk(
|
9
|
'trackingTool/sendTextForProcessing',
|
10
|
async (text: string) => {
|
11
|
try {
|
12
|
const { data, status } = await axiosInstance.post('path', { text })
|
13
|
if (status !== 200) {
|
14
|
return Promise.reject(
|
15
|
'Error while fetching map, please try again later'
|
16
|
)
|
17
|
}
|
18
|
return data
|
19
|
} catch (err: any) {
|
20
|
return Promise.reject('Error, server is currently unavailable')
|
21
|
}
|
22
|
}
|
23
|
)
|
24
|
|
25
|
/**
|
26
|
* Sends request to get all items from the local catalog with specified filter
|
27
|
*/
|
28
|
export const importFromLocalCatalog = createAsyncThunk('trackingTool/importFromLocalCatalog', async ({ name, country, type, writtenForm }: {
|
29
|
name?: string,
|
30
|
country?: string,
|
31
|
type?: string,
|
32
|
writtenForm?: string
|
33
|
}) => {
|
34
|
try {
|
35
|
const { data, status } = await axiosInstance.get('/catalog-items', {
|
36
|
params: {
|
37
|
name: name ? encodeURIComponent(name) : undefined,
|
38
|
country: country ? encodeURIComponent(country) : undefined,
|
39
|
type: type ? encodeURIComponent(type) : undefined,
|
40
|
writtenForm: writtenForm ? encodeURIComponent(writtenForm) : undefined
|
41
|
}
|
42
|
})
|
43
|
|
44
|
if (status !== 200) {
|
45
|
return Promise.reject('Error while local catalog items, please try again later')
|
46
|
}
|
47
|
|
48
|
return data
|
49
|
}
|
50
|
catch (err: any) {
|
51
|
return Promise.reject('Error, server is currently unavailable')
|
52
|
}
|
53
|
})
|
54
|
|
55
|
/**
|
56
|
* Imports items from external catalog
|
57
|
*/
|
58
|
export const importFromExternalCatalog = createAsyncThunk(
|
59
|
'trackingTool/importFromExternalCatalog',
|
60
|
async ({ name, source }: { name: string, source: string }) => {
|
61
|
try {
|
62
|
const { data, status } = await axiosInstance.get('/external-catalog-items', {
|
63
|
params: { name: name ? encodeURIComponent(name) : undefined, source }
|
64
|
})
|
65
|
|
66
|
if (status !== 200) {
|
67
|
return Promise.reject(
|
68
|
'Error while fetching external catalog items, please try again later'
|
69
|
)
|
70
|
}
|
71
|
|
72
|
return data
|
73
|
}
|
74
|
catch (err: any) {
|
75
|
return Promise.reject('Error, server is currently unavailable')
|
76
|
}
|
77
|
}
|
78
|
)
|
79
|
|
80
|
/**
|
81
|
* Save item to the local catalog
|
82
|
*/
|
83
|
export const saveToCatalog = createAsyncThunk(
|
84
|
'trackingTool/savePathItems',
|
85
|
async (path: Path) => {
|
86
|
try {
|
87
|
const { status } = await axiosInstance.post('catalog-items/batch', path.filter(item => item.addToPath && !item.hidden)
|
88
|
.map(item => ({ ...item.catalogItem })))
|
89
|
|
90
|
if (status !== 200) {
|
91
|
return Promise.reject('Error while saving to catalog, please try again later')
|
92
|
}
|
93
|
|
94
|
return true
|
95
|
}
|
96
|
catch (err: any) {
|
97
|
return Promise.reject('Error while saving to catalog, please try again later')
|
98
|
}
|
99
|
}
|
100
|
)
|