You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.5 KiB

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