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.

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