gab-social/app/javascript/gabsocial/actions/search.js

110 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-07-02 08:10:25 +01:00
import api from '../api';
import { fetchRelationships } from './accounts';
2020-05-04 19:44:37 +01:00
import { fetchGroupsSuccess, fetchGroupRelationships } from './groups'
import {
importFetchedAccounts,
importFetchedStatuses,
} from './importer';
import { importLinkCards } from './links'
import { SEARCH_FILTERS } from '../constants'
2019-07-02 08:10:25 +01:00
export const SEARCH_CHANGE = 'SEARCH_CHANGE';
export const SEARCH_CLEAR = 'SEARCH_CLEAR';
export const SEARCH_SHOW = 'SEARCH_SHOW';
export const SEARCH_FETCH_REQUEST = 'SEARCH_FETCH_REQUEST';
export const SEARCH_FETCH_SUCCESS = 'SEARCH_FETCH_SUCCESS';
export const SEARCH_FETCH_FAIL = 'SEARCH_FETCH_FAIL';
export const SEARCH_FILTER_SET = 'SEARCH_FILTER_SET'
2019-07-02 08:10:25 +01:00
export function changeSearch(value) {
return {
type: SEARCH_CHANGE,
value,
};
};
export function clearSearch() {
return {
type: SEARCH_CLEAR,
};
};
export function submitSearch() {
return (dispatch, getState) => {
const value = getState().getIn(['search', 'value']);
const onlyVerified = getState().getIn(['search', 'filter', 'onlyVerified'])
2019-07-02 08:10:25 +01:00
if (value.length === 0) return
2019-07-02 08:10:25 +01:00
dispatch(fetchSearchRequest());
api(getState).get('/api/v2/search', {
params: {
onlyVerified,
2019-07-02 08:10:25 +01:00
q: value,
resolve: true,
},
}).then(response => {
if (response.data.accounts) {
dispatch(importFetchedAccounts(response.data.accounts));
2020-04-29 23:32:49 +01:00
dispatch(fetchRelationships(response.data.accounts.map(item => item.id)));
2019-07-02 08:10:25 +01:00
}
if (response.data.statuses) {
dispatch(importFetchedStatuses(response.data.statuses));
}
if (response.data.links) {
dispatch(importLinkCards(response.data.links));
}
2020-05-04 19:44:37 +01:00
if (response.data.groups) {
dispatch(fetchGroupsSuccess(response.data.groups))
dispatch(fetchGroupRelationships(response.data.groups.map(item => item.id)))
}
2020-04-29 23:32:49 +01:00
2019-07-02 08:10:25 +01:00
dispatch(fetchSearchSuccess(response.data));
}).catch(error => {
dispatch(fetchSearchFail(error));
});
};
};
export function fetchSearchRequest() {
return {
type: SEARCH_FETCH_REQUEST,
};
};
export function fetchSearchSuccess(results) {
return {
type: SEARCH_FETCH_SUCCESS,
results,
};
};
export function fetchSearchFail(error) {
return {
type: SEARCH_FETCH_FAIL,
error,
};
};
export function showSearch() {
return {
type: SEARCH_SHOW,
};
};
export function setFilter(path, value, shouldSubmit) {
return (dispatch) => {
dispatch({
type: SEARCH_FILTER_SET,
path: path,
value: value,
})
if (shouldSubmit) dispatch(submitSearch())
}
}