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.

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