2020-11-15 18:48:32 +00:00
|
|
|
import api from '../api'
|
|
|
|
import { importFetchedAccounts } from './importer'
|
|
|
|
// import { showAlertForError } from './alerts'
|
2020-01-29 16:45:17 +00:00
|
|
|
import { me } from '../initial_state'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const LIST_FETCH_REQUEST = 'LIST_FETCH_REQUEST'
|
|
|
|
export const LIST_FETCH_SUCCESS = 'LIST_FETCH_SUCCESS'
|
|
|
|
export const LIST_FETCH_FAIL = 'LIST_FETCH_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const LISTS_FETCH_REQUEST = 'LISTS_FETCH_REQUEST'
|
|
|
|
export const LISTS_FETCH_SUCCESS = 'LISTS_FETCH_SUCCESS'
|
|
|
|
export const LISTS_FETCH_FAIL = 'LISTS_FETCH_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const LIST_EDITOR_TITLE_CHANGE = 'LIST_EDITOR_TITLE_CHANGE'
|
|
|
|
export const LIST_EDITOR_RESET = 'LIST_EDITOR_RESET'
|
|
|
|
export const LIST_EDITOR_SETUP = 'LIST_EDITOR_SETUP'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const LIST_CREATE_REQUEST = 'LIST_CREATE_REQUEST'
|
|
|
|
export const LIST_CREATE_SUCCESS = 'LIST_CREATE_SUCCESS'
|
|
|
|
export const LIST_CREATE_FAIL = 'LIST_CREATE_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const LIST_UPDATE_REQUEST = 'LIST_UPDATE_REQUEST'
|
|
|
|
export const LIST_UPDATE_SUCCESS = 'LIST_UPDATE_SUCCESS'
|
|
|
|
export const LIST_UPDATE_FAIL = 'LIST_UPDATE_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const LIST_DELETE_REQUEST = 'LIST_DELETE_REQUEST'
|
|
|
|
export const LIST_DELETE_SUCCESS = 'LIST_DELETE_SUCCESS'
|
|
|
|
export const LIST_DELETE_FAIL = 'LIST_DELETE_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const LIST_ACCOUNTS_FETCH_REQUEST = 'LIST_ACCOUNTS_FETCH_REQUEST'
|
|
|
|
export const LIST_ACCOUNTS_FETCH_SUCCESS = 'LIST_ACCOUNTS_FETCH_SUCCESS'
|
|
|
|
export const LIST_ACCOUNTS_FETCH_FAIL = 'LIST_ACCOUNTS_FETCH_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const LIST_EDITOR_SUGGESTIONS_CHANGE = 'LIST_EDITOR_SUGGESTIONS_CHANGE'
|
|
|
|
export const LIST_EDITOR_SUGGESTIONS_READY = 'LIST_EDITOR_SUGGESTIONS_READY'
|
|
|
|
export const LIST_EDITOR_SUGGESTIONS_CLEAR = 'LIST_EDITOR_SUGGESTIONS_CLEAR'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const LIST_EDITOR_ADD_REQUEST = 'LIST_EDITOR_ADD_REQUEST'
|
|
|
|
export const LIST_EDITOR_ADD_SUCCESS = 'LIST_EDITOR_ADD_SUCCESS'
|
|
|
|
export const LIST_EDITOR_ADD_FAIL = 'LIST_EDITOR_ADD_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const LIST_EDITOR_REMOVE_REQUEST = 'LIST_EDITOR_REMOVE_REQUEST'
|
|
|
|
export const LIST_EDITOR_REMOVE_SUCCESS = 'LIST_EDITOR_REMOVE_SUCCESS'
|
|
|
|
export const LIST_EDITOR_REMOVE_FAIL = 'LIST_EDITOR_REMOVE_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const LIST_ADDER_RESET = 'LIST_ADDER_RESET'
|
|
|
|
export const LIST_ADDER_SETUP = 'LIST_ADDER_SETUP'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const LIST_ADDER_LISTS_FETCH_REQUEST = 'LIST_ADDER_LISTS_FETCH_REQUEST'
|
|
|
|
export const LIST_ADDER_LISTS_FETCH_SUCCESS = 'LIST_ADDER_LISTS_FETCH_SUCCESS'
|
|
|
|
export const LIST_ADDER_LISTS_FETCH_FAIL = 'LIST_ADDER_LISTS_FETCH_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const fetchList = (id) => (dispatch, getState) => {
|
|
|
|
if (!me) return
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
if (getState().getIn(['lists', id])) return
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(fetchListRequest(id))
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
api(getState).get(`/api/v1/lists/${id}`)
|
|
|
|
.then(({ data }) => dispatch(fetchListSuccess(data)))
|
2020-11-15 18:48:32 +00:00
|
|
|
.catch((err) => dispatch(fetchListFail(id, err)))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchListRequest = id => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_FETCH_REQUEST,
|
|
|
|
id,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchListSuccess = list => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_FETCH_SUCCESS,
|
|
|
|
list,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchListFail = (id, error) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_FETCH_FAIL,
|
|
|
|
id,
|
|
|
|
error,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
export const fetchLists = () => (dispatch, getState) => {
|
2020-02-19 23:57:07 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(fetchListsRequest())
|
2020-02-19 23:57:07 +00:00
|
|
|
if (!me) return reject()
|
|
|
|
api(getState).get('/api/v1/lists').then(({ data }) => {
|
|
|
|
dispatch(fetchListsSuccess(data))
|
|
|
|
return resolve()
|
|
|
|
}).catch((err) => {
|
|
|
|
dispatch(fetchListsFail(err))
|
|
|
|
return reject()
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2020-02-19 23:57:07 +00:00
|
|
|
})
|
2020-11-15 18:48:32 +00:00
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchListsRequest = () => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LISTS_FETCH_REQUEST,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchListsSuccess = (lists) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LISTS_FETCH_SUCCESS,
|
|
|
|
lists,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchListsFail = (error) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LISTS_FETCH_FAIL,
|
|
|
|
error,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-04-28 06:33:58 +01:00
|
|
|
export const submitListEditor = (shouldReset) => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
const listId = getState().getIn(['listEditor', 'listId'])
|
|
|
|
const title = getState().getIn(['listEditor', 'title'])
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
if (listId === null) {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(createList(title, shouldReset))
|
2019-07-02 08:10:25 +01:00
|
|
|
} else {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(updateList(listId, title, shouldReset))
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
2020-11-15 18:48:32 +00:00
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-04-28 06:33:58 +01:00
|
|
|
export const setupListEditor = (listId) => (dispatch, getState) => {
|
2019-07-02 08:10:25 +01:00
|
|
|
dispatch({
|
|
|
|
type: LIST_EDITOR_SETUP,
|
|
|
|
list: getState().getIn(['lists', listId]),
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(fetchListAccounts(listId))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-04-28 06:33:58 +01:00
|
|
|
export const changeListEditorTitle = (value) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_EDITOR_TITLE_CHANGE,
|
|
|
|
value,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
export const createList = (title, shouldReset) => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
if (!me) return
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(createListRequest())
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
api(getState).post('/api/v1/lists', { title }).then(({ data }) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(createListSuccess(data))
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
if (shouldReset) {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(resetListEditor())
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
2020-11-15 18:48:32 +00:00
|
|
|
}).catch((err) => dispatch(createListFail(err)))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
export const createListRequest = () => ({
|
|
|
|
type: LIST_CREATE_REQUEST,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const createListSuccess = (list) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_CREATE_SUCCESS,
|
|
|
|
list,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const createListFail = (error) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_CREATE_FAIL,
|
|
|
|
error,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
export const updateList = (id, title, shouldReset) => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
if (!me) return
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(updateListRequest(id))
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
api(getState).put(`/api/v1/lists/${id}`, { title }).then(({ data }) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(updateListSuccess(data))
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
if (shouldReset) {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(resetListEditor())
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
2020-11-15 18:48:32 +00:00
|
|
|
}).catch((err) => dispatch(updateListFail(id, err)))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
export const updateListRequest = id => ({
|
|
|
|
type: LIST_UPDATE_REQUEST,
|
|
|
|
id,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
export const updateListSuccess = list => ({
|
|
|
|
type: LIST_UPDATE_SUCCESS,
|
|
|
|
list,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
export const updateListFail = (id, error) => ({
|
|
|
|
type: LIST_UPDATE_FAIL,
|
|
|
|
id,
|
|
|
|
error,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
export const resetListEditor = () => ({
|
|
|
|
type: LIST_EDITOR_RESET,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-04-28 06:33:58 +01:00
|
|
|
export const deleteList = (id) => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
if (!me) return
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(deleteListRequest(id))
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
api(getState).delete(`/api/v1/lists/${id}`)
|
|
|
|
.then(() => dispatch(deleteListSuccess(id)))
|
2020-11-15 18:48:32 +00:00
|
|
|
.catch((err) => dispatch(deleteListFail(id, err)))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const deleteListRequest = (id) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_DELETE_REQUEST,
|
|
|
|
id,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const deleteListSuccess = (id) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_DELETE_SUCCESS,
|
|
|
|
id,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
export const deleteListFail = (id, error) => ({
|
|
|
|
type: LIST_DELETE_FAIL,
|
|
|
|
id,
|
|
|
|
error,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-04-28 06:33:58 +01:00
|
|
|
export const fetchListAccounts = (listId) => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
if (!me) return
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(fetchListAccountsRequest(listId))
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
api(getState).get(`/api/v1/lists/${listId}/accounts`, { params: { limit: 0 } }).then(({ data }) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(importFetchedAccounts(data))
|
|
|
|
dispatch(fetchListAccountsSuccess(listId, data))
|
|
|
|
}).catch((err) => dispatch(fetchListAccountsFail(listId, err)))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const fetchListAccountsRequest = (id) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_ACCOUNTS_FETCH_REQUEST,
|
|
|
|
id,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
export const fetchListAccountsSuccess = (id, accounts, next) => ({
|
|
|
|
type: LIST_ACCOUNTS_FETCH_SUCCESS,
|
|
|
|
id,
|
|
|
|
accounts,
|
|
|
|
next,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
export const fetchListAccountsFail = (id, error) => ({
|
|
|
|
type: LIST_ACCOUNTS_FETCH_FAIL,
|
|
|
|
id,
|
|
|
|
error,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-04-28 06:33:58 +01:00
|
|
|
export const fetchListSuggestions = (q) => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
if (!me) return
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
const params = {
|
|
|
|
q,
|
|
|
|
resolve: false,
|
2020-05-03 06:22:49 +01:00
|
|
|
limit: 25,
|
2020-11-15 18:48:32 +00:00
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
api(getState).get('/api/v1/accounts/search', { params }).then(({ data }) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(importFetchedAccounts(data))
|
|
|
|
dispatch(fetchListSuggestionsReady(q, data))
|
2020-03-14 17:31:29 +00:00
|
|
|
})
|
2020-11-15 18:48:32 +00:00
|
|
|
// }).catch(error => dispatch(showAlertForError(error)))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-12-03 04:22:51 +00:00
|
|
|
const fetchListSuggestionsReady = (query, accounts) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_EDITOR_SUGGESTIONS_READY,
|
|
|
|
query,
|
|
|
|
accounts,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
export const clearListSuggestions = () => ({
|
|
|
|
type: LIST_EDITOR_SUGGESTIONS_CLEAR,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-04-28 06:33:58 +01:00
|
|
|
export const changeListSuggestions = (value) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_EDITOR_SUGGESTIONS_CHANGE,
|
|
|
|
value,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
export const addToListEditor = accountId => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(addToList(getState().getIn(['listEditor', 'listId']), accountId))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
export const addToList = (listId, accountId) => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
if (!me) return
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(addToListRequest(listId, accountId))
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
api(getState).post(`/api/v1/lists/${listId}/accounts`, { account_ids: [accountId] })
|
|
|
|
.then(() => dispatch(addToListSuccess(listId, accountId)))
|
2020-11-15 18:48:32 +00:00
|
|
|
.catch((err) => dispatch(addToListFail(listId, accountId, err)))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const addToListRequest = (listId, accountId) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_EDITOR_ADD_REQUEST,
|
|
|
|
listId,
|
|
|
|
accountId,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const addToListSuccess = (listId, accountId) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_EDITOR_ADD_SUCCESS,
|
|
|
|
listId,
|
|
|
|
accountId,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const addToListFail = (listId, accountId, error) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_EDITOR_ADD_FAIL,
|
|
|
|
listId,
|
|
|
|
accountId,
|
|
|
|
error,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
export const removeFromListEditor = accountId => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(removeFromList(getState().getIn(['listEditor', 'listId']), accountId))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
export const removeFromList = (listId, accountId) => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
if (!me) return
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(removeFromListRequest(listId, accountId))
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
api(getState).delete(`/api/v1/lists/${listId}/accounts`, { params: { account_ids: [accountId] } })
|
|
|
|
.then(() => dispatch(removeFromListSuccess(listId, accountId)))
|
2020-11-15 18:48:32 +00:00
|
|
|
.catch((err) => dispatch(removeFromListFail(listId, accountId, err)))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const removeFromListRequest = (listId, accountId) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_EDITOR_REMOVE_REQUEST,
|
|
|
|
listId,
|
|
|
|
accountId,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const removeFromListSuccess = (listId, accountId) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_EDITOR_REMOVE_SUCCESS,
|
|
|
|
listId,
|
|
|
|
accountId,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const removeFromListFail = (listId, accountId, error) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_EDITOR_REMOVE_FAIL,
|
|
|
|
listId,
|
|
|
|
accountId,
|
|
|
|
error,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
export const resetListAdder = () => ({
|
|
|
|
type: LIST_ADDER_RESET,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
export const setupListAdder = accountId => (dispatch, getState) => {
|
|
|
|
dispatch({
|
|
|
|
type: LIST_ADDER_SETUP,
|
|
|
|
account: getState().getIn(['accounts', accountId]),
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
|
|
|
dispatch(fetchLists())
|
|
|
|
dispatch(fetchAccountLists(accountId))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-04-28 06:33:58 +01:00
|
|
|
export const fetchAccountLists = (accountId) => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
if (!me) return
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(fetchAccountListsRequest(accountId))
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
api(getState).get(`/api/v1/accounts/${accountId}/lists`)
|
|
|
|
.then(({ data }) => dispatch(fetchAccountListsSuccess(accountId, data)))
|
2020-11-15 18:48:32 +00:00
|
|
|
.catch((err) => dispatch(fetchAccountListsFail(accountId, err)))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchAccountListsRequest = (id) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type:LIST_ADDER_LISTS_FETCH_REQUEST,
|
|
|
|
id,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchAccountListsSuccess = (id, lists) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_ADDER_LISTS_FETCH_SUCCESS,
|
|
|
|
id,
|
|
|
|
lists,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchAccountListsFail = (id, err) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: LIST_ADDER_LISTS_FETCH_FAIL,
|
|
|
|
id,
|
|
|
|
err,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-04-28 06:33:58 +01:00
|
|
|
export const addToListAdder = (listId) => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(addToList(listId, getState().getIn(['listAdder', 'accountId'])))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-04-28 06:33:58 +01:00
|
|
|
export const removeFromListAdder = (listId) => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(removeFromList(listId, getState().getIn(['listAdder', 'accountId'])))
|
|
|
|
}
|