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.

313 lines
8.3 KiB

  1. import api from '../api';
  2. export const LIST_FETCH_REQUEST = 'LIST_FETCH_REQUEST';
  3. export const LIST_FETCH_SUCCESS = 'LIST_FETCH_SUCCESS';
  4. export const LIST_FETCH_FAIL = 'LIST_FETCH_FAIL';
  5. export const LISTS_FETCH_REQUEST = 'LISTS_FETCH_REQUEST';
  6. export const LISTS_FETCH_SUCCESS = 'LISTS_FETCH_SUCCESS';
  7. export const LISTS_FETCH_FAIL = 'LISTS_FETCH_FAIL';
  8. export const LIST_EDITOR_TITLE_CHANGE = 'LIST_EDITOR_TITLE_CHANGE';
  9. export const LIST_EDITOR_RESET = 'LIST_EDITOR_RESET';
  10. export const LIST_EDITOR_SETUP = 'LIST_EDITOR_SETUP';
  11. export const LIST_CREATE_REQUEST = 'LIST_CREATE_REQUEST';
  12. export const LIST_CREATE_SUCCESS = 'LIST_CREATE_SUCCESS';
  13. export const LIST_CREATE_FAIL = 'LIST_CREATE_FAIL';
  14. export const LIST_UPDATE_REQUEST = 'LIST_UPDATE_REQUEST';
  15. export const LIST_UPDATE_SUCCESS = 'LIST_UPDATE_SUCCESS';
  16. export const LIST_UPDATE_FAIL = 'LIST_UPDATE_FAIL';
  17. export const LIST_DELETE_REQUEST = 'LIST_DELETE_REQUEST';
  18. export const LIST_DELETE_SUCCESS = 'LIST_DELETE_SUCCESS';
  19. export const LIST_DELETE_FAIL = 'LIST_DELETE_FAIL';
  20. export const LIST_ACCOUNTS_FETCH_REQUEST = 'LIST_ACCOUNTS_FETCH_REQUEST';
  21. export const LIST_ACCOUNTS_FETCH_SUCCESS = 'LIST_ACCOUNTS_FETCH_SUCCESS';
  22. export const LIST_ACCOUNTS_FETCH_FAIL = 'LIST_ACCOUNTS_FETCH_FAIL';
  23. export const LIST_EDITOR_SUGGESTIONS_CHANGE = 'LIST_EDITOR_SUGGESTIONS_CHANGE';
  24. export const LIST_EDITOR_SUGGESTIONS_READY = 'LIST_EDITOR_SUGGESTIONS_READY';
  25. export const LIST_EDITOR_SUGGESTIONS_CLEAR = 'LIST_EDITOR_SUGGESTIONS_CLEAR';
  26. export const LIST_EDITOR_ADD_REQUEST = 'LIST_EDITOR_ADD_REQUEST';
  27. export const LIST_EDITOR_ADD_SUCCESS = 'LIST_EDITOR_ADD_SUCCESS';
  28. export const LIST_EDITOR_ADD_FAIL = 'LIST_EDITOR_ADD_FAIL';
  29. export const LIST_EDITOR_REMOVE_REQUEST = 'LIST_EDITOR_REMOVE_REQUEST';
  30. export const LIST_EDITOR_REMOVE_SUCCESS = 'LIST_EDITOR_REMOVE_SUCCESS';
  31. export const LIST_EDITOR_REMOVE_FAIL = 'LIST_EDITOR_REMOVE_FAIL';
  32. export const fetchList = id => (dispatch, getState) => {
  33. if (getState().getIn(['lists', id])) {
  34. return;
  35. }
  36. dispatch(fetchListRequest(id));
  37. api(getState).get(`/api/v1/lists/${id}`)
  38. .then(({ data }) => dispatch(fetchListSuccess(data)))
  39. .catch(err => dispatch(fetchListFail(id, err)));
  40. };
  41. export const fetchListRequest = id => ({
  42. type: LIST_FETCH_REQUEST,
  43. id,
  44. });
  45. export const fetchListSuccess = list => ({
  46. type: LIST_FETCH_SUCCESS,
  47. list,
  48. });
  49. export const fetchListFail = (id, error) => ({
  50. type: LIST_FETCH_FAIL,
  51. id,
  52. error,
  53. });
  54. export const fetchLists = () => (dispatch, getState) => {
  55. dispatch(fetchListsRequest());
  56. api(getState).get('/api/v1/lists')
  57. .then(({ data }) => dispatch(fetchListsSuccess(data)))
  58. .catch(err => dispatch(fetchListsFail(err)));
  59. };
  60. export const fetchListsRequest = () => ({
  61. type: LISTS_FETCH_REQUEST,
  62. });
  63. export const fetchListsSuccess = lists => ({
  64. type: LISTS_FETCH_SUCCESS,
  65. lists,
  66. });
  67. export const fetchListsFail = error => ({
  68. type: LISTS_FETCH_FAIL,
  69. error,
  70. });
  71. export const submitListEditor = shouldReset => (dispatch, getState) => {
  72. const listId = getState().getIn(['listEditor', 'listId']);
  73. const title = getState().getIn(['listEditor', 'title']);
  74. if (listId === null) {
  75. dispatch(createList(title, shouldReset));
  76. } else {
  77. dispatch(updateList(listId, title, shouldReset));
  78. }
  79. };
  80. export const setupListEditor = listId => (dispatch, getState) => {
  81. dispatch({
  82. type: LIST_EDITOR_SETUP,
  83. list: getState().getIn(['lists', listId]),
  84. });
  85. dispatch(fetchListAccounts(listId));
  86. };
  87. export const changeListEditorTitle = value => ({
  88. type: LIST_EDITOR_TITLE_CHANGE,
  89. value,
  90. });
  91. export const createList = (title, shouldReset) => (dispatch, getState) => {
  92. dispatch(createListRequest());
  93. api(getState).post('/api/v1/lists', { title }).then(({ data }) => {
  94. dispatch(createListSuccess(data));
  95. if (shouldReset) {
  96. dispatch(resetListEditor());
  97. }
  98. }).catch(err => dispatch(createListFail(err)));
  99. };
  100. export const createListRequest = () => ({
  101. type: LIST_CREATE_REQUEST,
  102. });
  103. export const createListSuccess = list => ({
  104. type: LIST_CREATE_SUCCESS,
  105. list,
  106. });
  107. export const createListFail = error => ({
  108. type: LIST_CREATE_FAIL,
  109. error,
  110. });
  111. export const updateList = (id, title, shouldReset) => (dispatch, getState) => {
  112. dispatch(updateListRequest(id));
  113. api(getState).put(`/api/v1/lists/${id}`, { title }).then(({ data }) => {
  114. dispatch(updateListSuccess(data));
  115. if (shouldReset) {
  116. dispatch(resetListEditor());
  117. }
  118. }).catch(err => dispatch(updateListFail(id, err)));
  119. };
  120. export const updateListRequest = id => ({
  121. type: LIST_UPDATE_REQUEST,
  122. id,
  123. });
  124. export const updateListSuccess = list => ({
  125. type: LIST_UPDATE_SUCCESS,
  126. list,
  127. });
  128. export const updateListFail = (id, error) => ({
  129. type: LIST_UPDATE_FAIL,
  130. id,
  131. error,
  132. });
  133. export const resetListEditor = () => ({
  134. type: LIST_EDITOR_RESET,
  135. });
  136. export const deleteList = id => (dispatch, getState) => {
  137. dispatch(deleteListRequest(id));
  138. api(getState).delete(`/api/v1/lists/${id}`)
  139. .then(() => dispatch(deleteListSuccess(id)))
  140. .catch(err => dispatch(deleteListFail(id, err)));
  141. };
  142. export const deleteListRequest = id => ({
  143. type: LIST_DELETE_REQUEST,
  144. id,
  145. });
  146. export const deleteListSuccess = id => ({
  147. type: LIST_DELETE_SUCCESS,
  148. id,
  149. });
  150. export const deleteListFail = (id, error) => ({
  151. type: LIST_DELETE_FAIL,
  152. id,
  153. error,
  154. });
  155. export const fetchListAccounts = listId => (dispatch, getState) => {
  156. dispatch(fetchListAccountsRequest(listId));
  157. api(getState).get(`/api/v1/lists/${listId}/accounts`, { params: { limit: 0 } })
  158. .then(({ data }) => dispatch(fetchListAccountsSuccess(listId, data)))
  159. .catch(err => dispatch(fetchListAccountsFail(listId, err)));
  160. };
  161. export const fetchListAccountsRequest = id => ({
  162. type: LIST_ACCOUNTS_FETCH_REQUEST,
  163. id,
  164. });
  165. export const fetchListAccountsSuccess = (id, accounts, next) => ({
  166. type: LIST_ACCOUNTS_FETCH_SUCCESS,
  167. id,
  168. accounts,
  169. next,
  170. });
  171. export const fetchListAccountsFail = (id, error) => ({
  172. type: LIST_ACCOUNTS_FETCH_FAIL,
  173. id,
  174. error,
  175. });
  176. export const fetchListSuggestions = q => (dispatch, getState) => {
  177. const params = {
  178. q,
  179. resolve: false,
  180. limit: 4,
  181. following: true,
  182. };
  183. api(getState).get('/api/v1/accounts/search', { params })
  184. .then(({ data }) => dispatch(fetchListSuggestionsReady(q, data)));
  185. };
  186. export const fetchListSuggestionsReady = (query, accounts) => ({
  187. type: LIST_EDITOR_SUGGESTIONS_READY,
  188. query,
  189. accounts,
  190. });
  191. export const clearListSuggestions = () => ({
  192. type: LIST_EDITOR_SUGGESTIONS_CLEAR,
  193. });
  194. export const changeListSuggestions = value => ({
  195. type: LIST_EDITOR_SUGGESTIONS_CHANGE,
  196. value,
  197. });
  198. export const addToListEditor = accountId => (dispatch, getState) => {
  199. dispatch(addToList(getState().getIn(['listEditor', 'listId']), accountId));
  200. };
  201. export const addToList = (listId, accountId) => (dispatch, getState) => {
  202. dispatch(addToListRequest(listId, accountId));
  203. api(getState).post(`/api/v1/lists/${listId}/accounts`, { account_ids: [accountId] })
  204. .then(() => dispatch(addToListSuccess(listId, accountId)))
  205. .catch(err => dispatch(addToListFail(listId, accountId, err)));
  206. };
  207. export const addToListRequest = (listId, accountId) => ({
  208. type: LIST_EDITOR_ADD_REQUEST,
  209. listId,
  210. accountId,
  211. });
  212. export const addToListSuccess = (listId, accountId) => ({
  213. type: LIST_EDITOR_ADD_SUCCESS,
  214. listId,
  215. accountId,
  216. });
  217. export const addToListFail = (listId, accountId, error) => ({
  218. type: LIST_EDITOR_ADD_FAIL,
  219. listId,
  220. accountId,
  221. error,
  222. });
  223. export const removeFromListEditor = accountId => (dispatch, getState) => {
  224. dispatch(removeFromList(getState().getIn(['listEditor', 'listId']), accountId));
  225. };
  226. export const removeFromList = (listId, accountId) => (dispatch, getState) => {
  227. dispatch(removeFromListRequest(listId, accountId));
  228. api(getState).delete(`/api/v1/lists/${listId}/accounts`, { params: { account_ids: [accountId] } })
  229. .then(() => dispatch(removeFromListSuccess(listId, accountId)))
  230. .catch(err => dispatch(removeFromListFail(listId, accountId, err)));
  231. };
  232. export const removeFromListRequest = (listId, accountId) => ({
  233. type: LIST_EDITOR_REMOVE_REQUEST,
  234. listId,
  235. accountId,
  236. });
  237. export const removeFromListSuccess = (listId, accountId) => ({
  238. type: LIST_EDITOR_REMOVE_SUCCESS,
  239. listId,
  240. accountId,
  241. });
  242. export const removeFromListFail = (listId, accountId, error) => ({
  243. type: LIST_EDITOR_REMOVE_FAIL,
  244. listId,
  245. accountId,
  246. error,
  247. });