Form.js
957 Bytes
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
import axios from 'axios'
import Errors from '@/Utils/Errors'
class Form {
constructor(fields = {}) {
this.fields = fields
this.sending = false
this.errors = new Errors()
this.http = axios.create({
headers: { 'X-Requested-With': 'XMLHttpRequest' },
})
}
delete({ url, then }) {
this.request(this.http.delete(url), then)
}
post({ url, data = this.fields, then }) {
this.request(this.http.post(url, data), then)
}
put({ url, data = this.fields, then }) {
this.request(this.http.put(url, data), then)
}
request(request, then) {
this.sending = true
request.then(response => {
this.sending = false
then(response.data)
}).catch(error => {
this.sending = false
if (error.response && error.response.status === 422) {
this.errors.record(error.response.data.errors)
} else {
return Promise.reject(error)
}
})
}
}
export default Form