journey/client/actions/results.js

53 lines
1.1 KiB
JavaScript

import { pick } from 'lowline'
export const DELETE_RESULT = 'DELETE_RESULT'
const baseUrl = '/api/results'
const headers = {
'X-Requested-With': 'XMLHttpRequest',
accept: 'application/json',
}
export const FETCH_RESULTS = 'FETCH_RESULTS'
// query can be a query string (without ?, ie not a search string)
export function fetchResults(query = {}) {
query = typeof query === 'string' ? query : qs.stringify(query)
return (dispatch) => {
fetch(`${baseUrl}${query ? `?${query}` : ''}`, {
credentials: 'same-origin',
headers,
})
.then((res) => res.json())
.then((json) => {
const actions = [receiveResults(json.results || [])]
if (json.perPage) {
actions.push(setPagination(pick(json, 'perPage', 'totalCount')))
}
dispatch(batchActions(actions))
})
}
}
export const RECEIVE_RESULTS = 'RECEIVE_RESULTS'
export function receiveResults(json) {
return {
type: RECEIVE_RESULTS,
payload: json,
receivedAt: Date.now(),
}
}
export const REMOVE_RESULTS = 'REMOVE_RESULTS'
export function removeResults() {
return {
type: REMOVE_RESULTS,
}
}