-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathindex.js
60 lines (55 loc) · 1.84 KB
/
index.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
import Constants from '../constants/recipesConstants'
import jwtDecode from 'jwt-decode'
export function login(username, password, baseUrl) {
return dispatch => {
let data = new FormData()
data.append('_username', username)
data.append('_password', password)
fetch(baseUrl + '/api/login_check', {
method: 'POST',
body: data
}).then( (response) => {
if (response.ok) {
return response.json()
} else {
throw Error(response.statusText)
}
}).then( (data) => {
const payload = jwtDecode(data.token)
let d = new Date(payload.exp * 1000)
document.cookie = 'BEARER='+data.token+'; expires='+d.toUTCString()+'; path=/'
dispatch({ type: Constants.LOGIN_TOKEN_RECEIVED, token: data.token })
}).catch( () => {
dispatch({ type: Constants.LOGIN_ERROR })
})
}
}
export function fetchForm(baseUrl, token) {
return dispatch => {
fetch(baseUrl + '/admin/api/form', {
method: 'GET',
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer '+token,
},
}).then( (response) => {
return response.json()
}).then( (data) => {
dispatch({ type: Constants.FORM_FETCHED, initialValues: data.initialValues, schema: data.schema })
})
}
}
export function fetchRecipes(baseUrl) {
return dispatch => {
fetch(baseUrl + '/api/recipes', {
method: 'GET',
headers: {
'Accept': 'application/json',
},
}).then( (response) => {
return response.json()
}).then( (data) => {
dispatch({ type: Constants.RECIPES_FETCHED, recipes: data })
})
}
}