/
ReportContainer.js
91 lines (78 loc) · 1.88 KB
/
ReportContainer.js
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
import { Container } from 'unstated'
import ky from 'ky'
import { toaster } from 'evergreen-ui'
import { getAppRoot } from '../lib/common'
const api = ky.extend({ prefixUrl: getAppRoot() + '/api/' })
export default class ReportContainer extends Container {
constructor (props) {
super(props)
this.state = {
total: 0,
reports: [],
currentReport: {},
isFetching: false
}
}
async fetchReports (order = 'desc', sort = 'date', page = 0, projectId = 0) {
this.setState({
isFetching: true
})
const searchParams = new URLSearchParams()
if (order) {
searchParams.append('order', order)
searchParams.append('sort', sort)
searchParams.append('page', page)
}
try {
let data
let total
if (!projectId) {
const res = await api.get('reports', { searchParams }).json()
data = res.data
total = res.total
} else {
const res = await api.get(`projects/${projectId}/reports`, { searchParams }).json()
data = res.data
total = res.total
}
this.setState({
total,
reports: data,
isFetching: false
})
} catch (err) {
toaster.danger(err.message)
console.log('error: ', err)
}
}
async fetchReport (id) {
this.setState({
isFetching: true
})
try {
const r = await api.get(`reports/${id}`).json()
this.setState({
currentReport: r,
isFetching: false
})
} catch (err) {
toaster.danger(err.message)
console.log('error: ', err)
}
}
async deleteReport (id) {
this.setState({
isFetching: true
})
try {
await api.delete(`reports/${id}`).json()
this.setState({
isFetching: false
})
return true
} catch (err) {
toaster.danger(err.message)
console.log('error: ', err)
}
}
}