search.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import api from '../api'
  2. export const SEARCH_CHANGE = 'SEARCH_CHANGE';
  3. export const SEARCH_CLEAR = 'SEARCH_CLEAR';
  4. export const SEARCH_SHOW = 'SEARCH_SHOW';
  5. export const SEARCH_FETCH_REQUEST = 'SEARCH_FETCH_REQUEST';
  6. export const SEARCH_FETCH_SUCCESS = 'SEARCH_FETCH_SUCCESS';
  7. export const SEARCH_FETCH_FAIL = 'SEARCH_FETCH_FAIL';
  8. export function changeSearch(value) {
  9. return {
  10. type: SEARCH_CHANGE,
  11. value
  12. };
  13. };
  14. export function clearSearch() {
  15. return {
  16. type: SEARCH_CLEAR
  17. };
  18. };
  19. export function submitSearch() {
  20. return (dispatch, getState) => {
  21. const value = getState().getIn(['search', 'value']);
  22. if (value.length === 0) {
  23. return;
  24. }
  25. dispatch(fetchSearchRequest());
  26. api(getState).get('/api/v1/search', {
  27. params: {
  28. q: value,
  29. resolve: true
  30. }
  31. }).then(response => {
  32. dispatch(fetchSearchSuccess(response.data));
  33. }).catch(error => {
  34. dispatch(fetchSearchFail(error));
  35. });
  36. };
  37. };
  38. export function fetchSearchRequest() {
  39. return {
  40. type: SEARCH_FETCH_REQUEST
  41. };
  42. };
  43. export function fetchSearchSuccess(results) {
  44. return {
  45. type: SEARCH_FETCH_SUCCESS,
  46. results,
  47. accounts: results.accounts,
  48. statuses: results.statuses
  49. };
  50. };
  51. export function fetchSearchFail(error) {
  52. return {
  53. type: SEARCH_FETCH_FAIL,
  54. error
  55. };
  56. };
  57. export function showSearch() {
  58. return {
  59. type: SEARCH_SHOW
  60. };
  61. };