search.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 const SEARCH_EXPAND_REQUEST = 'SEARCH_EXPAND_REQUEST';
  11. export const SEARCH_EXPAND_SUCCESS = 'SEARCH_EXPAND_SUCCESS';
  12. export const SEARCH_EXPAND_FAIL = 'SEARCH_EXPAND_FAIL';
  13. export function changeSearch(value) {
  14. return {
  15. type: SEARCH_CHANGE,
  16. value,
  17. };
  18. };
  19. export function clearSearch() {
  20. return {
  21. type: SEARCH_CLEAR,
  22. };
  23. };
  24. export function submitSearch() {
  25. return (dispatch, getState) => {
  26. const value = getState().getIn(['search', 'value']);
  27. const signedIn = !!getState().getIn(['meta', 'me']);
  28. if (value.length === 0) {
  29. dispatch(fetchSearchSuccess({ accounts: [], statuses: [], hashtags: [] }, ''));
  30. return;
  31. }
  32. dispatch(fetchSearchRequest());
  33. api(getState).get('/api/v2/search', {
  34. params: {
  35. q: value,
  36. resolve: signedIn,
  37. limit: 5,
  38. },
  39. }).then(response => {
  40. if (response.data.accounts) {
  41. dispatch(importFetchedAccounts(response.data.accounts));
  42. }
  43. if (response.data.statuses) {
  44. dispatch(importFetchedStatuses(response.data.statuses));
  45. }
  46. dispatch(fetchSearchSuccess(response.data, value));
  47. dispatch(fetchRelationships(response.data.accounts.map(item => item.id)));
  48. }).catch(error => {
  49. dispatch(fetchSearchFail(error));
  50. });
  51. };
  52. };
  53. export function fetchSearchRequest() {
  54. return {
  55. type: SEARCH_FETCH_REQUEST,
  56. };
  57. };
  58. export function fetchSearchSuccess(results, searchTerm) {
  59. return {
  60. type: SEARCH_FETCH_SUCCESS,
  61. results,
  62. searchTerm,
  63. };
  64. };
  65. export function fetchSearchFail(error) {
  66. return {
  67. type: SEARCH_FETCH_FAIL,
  68. error,
  69. };
  70. };
  71. export const expandSearch = type => (dispatch, getState) => {
  72. const value = getState().getIn(['search', 'value']);
  73. const offset = getState().getIn(['search', 'results', type]).size;
  74. dispatch(expandSearchRequest());
  75. api(getState).get('/api/v2/search', {
  76. params: {
  77. q: value,
  78. type,
  79. offset,
  80. },
  81. }).then(({ data }) => {
  82. if (data.accounts) {
  83. dispatch(importFetchedAccounts(data.accounts));
  84. }
  85. if (data.statuses) {
  86. dispatch(importFetchedStatuses(data.statuses));
  87. }
  88. dispatch(expandSearchSuccess(data, value, type));
  89. dispatch(fetchRelationships(data.accounts.map(item => item.id)));
  90. }).catch(error => {
  91. dispatch(expandSearchFail(error));
  92. });
  93. };
  94. export const expandSearchRequest = () => ({
  95. type: SEARCH_EXPAND_REQUEST,
  96. });
  97. export const expandSearchSuccess = (results, searchTerm, searchType) => ({
  98. type: SEARCH_EXPAND_SUCCESS,
  99. results,
  100. searchTerm,
  101. searchType,
  102. });
  103. export const expandSearchFail = error => ({
  104. type: SEARCH_EXPAND_FAIL,
  105. error,
  106. });
  107. export const showSearch = () => ({
  108. type: SEARCH_SHOW,
  109. });