The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

84 lines
1.8 KiB

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