BuyWhat/src/actions/index.js

48 lines
1011 B
JavaScript

const apiUrl = 'https://api.trj.tw/api/v1/buywhat'
const genRequest = (method = 'GET', data = {}) => {
let json = {
method,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
mode: 'cors',
body: JSON.stringify(data)
}
if(/(get|head)/i.test(method)) delete json.body
return json
}
export const SetDev = (dev) => ({
type: 'device',
dev
})
export const toggleLoading = (flag = false) => ({
type: 'loading',
act: flag
})
export const addDialog = (msg = '') => ({
type: 'add_dialog',
msg
})
export const removeDialog = () => ({
type: 'remove_dialog'
})
export const getStoreList = () => dispatch => {
dispatch(toggleLoading(1))
fetch(`${apiUrl}/store`, genRequest())
.then(response => response.json())
.then(json => {
dispatch(toggleLoading(0))
if (json.status != 1) return dispatch(addDialog(json.message))
return dispatch({
type: 'store',
list: json.record || []
})
})
}