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

110 lines
2.4 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'
2020-11-15 18:48:32 +00:00
/**
*
*/
export const changeSearch = (value) => ({
type: SEARCH_CHANGE,
value,
})
/**
*
*/
export const clearSearch = () => ({
type: SEARCH_CLEAR,
})
/**
*
*/
export const submitSearch = () => (dispatch, getState) => {
const value = getState().getIn(['search', 'value'])
const onlyVerified = getState().getIn(['search', 'filter', 'onlyVerified'])
if (value.length === 0) return
dispatch(fetchSearchRequest())
api(getState).get('/api/v2/search', {
params: {
onlyVerified,
q: value,
resolve: true,
},
}).then((response) => {
if (response.data.accounts) {
dispatch(importFetchedAccounts(response.data.accounts))
dispatch(fetchRelationships(response.data.accounts.map(item => item.id)))
}
if (response.data.statuses) {
dispatch(importFetchedStatuses(response.data.statuses))
}
if (response.data.links) {
dispatch(importLinkCards(response.data.links))
}
if (response.data.groups) {
dispatch(fetchGroupsSuccess(response.data.groups))
dispatch(fetchGroupRelationships(response.data.groups.map(item => item.id)))
}
dispatch(fetchSearchSuccess(response.data))
}).catch((error) => {
dispatch(fetchSearchFail(error))
})
}
const fetchSearchRequest = () => ({
type: SEARCH_FETCH_REQUEST,
})
const fetchSearchSuccess = (results) => ({
type: SEARCH_FETCH_SUCCESS,
results,
})
const fetchSearchFail = (error) => ({
type: SEARCH_FETCH_FAIL,
showToast: true,
2020-11-15 18:48:32 +00:00
error,
})
/**
*
*/
export const showSearch = () => ({
type: SEARCH_SHOW,
})
/**
*
*/
export const setFilter = (path, value, shouldSubmit) => (dispatch) => {
dispatch({
type: SEARCH_FILTER_SET,
path: path,
value: value,
})
if (shouldSubmit) dispatch(submitSearch())
}