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.

367 lines
9.9 KiB

  1. import api from 'flavours/glitch/util/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 LIST_ADDER_RESET = 'LIST_ADDER_RESET';
  33. export const LIST_ADDER_SETUP = 'LIST_ADDER_SETUP';
  34. export const LIST_ADDER_LISTS_FETCH_REQUEST = 'LIST_ADDER_LISTS_FETCH_REQUEST';
  35. export const LIST_ADDER_LISTS_FETCH_SUCCESS = 'LIST_ADDER_LISTS_FETCH_SUCCESS';
  36. export const LIST_ADDER_LISTS_FETCH_FAIL = 'LIST_ADDER_LISTS_FETCH_FAIL';
  37. export const fetchList = id => (dispatch, getState) => {
  38. if (getState().getIn(['lists', id])) {
  39. return;
  40. }
  41. dispatch(fetchListRequest(id));
  42. api(getState).get(`/api/v1/lists/${id}`)
  43. .then(({ data }) => dispatch(fetchListSuccess(data)))
  44. .catch(err => dispatch(fetchListFail(id, err)));
  45. };
  46. export const fetchListRequest = id => ({
  47. type: LIST_FETCH_REQUEST,
  48. id,
  49. });
  50. export const fetchListSuccess = list => ({
  51. type: LIST_FETCH_SUCCESS,
  52. list,
  53. });
  54. export const fetchListFail = (id, error) => ({
  55. type: LIST_FETCH_FAIL,
  56. id,
  57. error,
  58. });
  59. export const fetchLists = () => (dispatch, getState) => {
  60. dispatch(fetchListsRequest());
  61. api(getState).get('/api/v1/lists')
  62. .then(({ data }) => dispatch(fetchListsSuccess(data)))
  63. .catch(err => dispatch(fetchListsFail(err)));
  64. };
  65. export const fetchListsRequest = () => ({
  66. type: LISTS_FETCH_REQUEST,
  67. });
  68. export const fetchListsSuccess = lists => ({
  69. type: LISTS_FETCH_SUCCESS,
  70. lists,
  71. });
  72. export const fetchListsFail = error => ({
  73. type: LISTS_FETCH_FAIL,
  74. error,
  75. });
  76. export const submitListEditor = shouldReset => (dispatch, getState) => {
  77. const listId = getState().getIn(['listEditor', 'listId']);
  78. const title = getState().getIn(['listEditor', 'title']);
  79. if (listId === null) {
  80. dispatch(createList(title, shouldReset));
  81. } else {
  82. dispatch(updateList(listId, title, shouldReset));
  83. }
  84. };
  85. export const setupListEditor = listId => (dispatch, getState) => {
  86. dispatch({
  87. type: LIST_EDITOR_SETUP,
  88. list: getState().getIn(['lists', listId]),
  89. });
  90. dispatch(fetchListAccounts(listId));
  91. };
  92. export const changeListEditorTitle = value => ({
  93. type: LIST_EDITOR_TITLE_CHANGE,
  94. value,
  95. });
  96. export const createList = (title, shouldReset) => (dispatch, getState) => {
  97. dispatch(createListRequest());
  98. api(getState).post('/api/v1/lists', { title }).then(({ data }) => {
  99. dispatch(createListSuccess(data));
  100. if (shouldReset) {
  101. dispatch(resetListEditor());
  102. }
  103. }).catch(err => dispatch(createListFail(err)));
  104. };
  105. export const createListRequest = () => ({
  106. type: LIST_CREATE_REQUEST,
  107. });
  108. export const createListSuccess = list => ({
  109. type: LIST_CREATE_SUCCESS,
  110. list,
  111. });
  112. export const createListFail = error => ({
  113. type: LIST_CREATE_FAIL,
  114. error,
  115. });
  116. export const updateList = (id, title, shouldReset, replies_policy) => (dispatch, getState) => {
  117. dispatch(updateListRequest(id));
  118. api(getState).put(`/api/v1/lists/${id}`, { title, replies_policy }).then(({ data }) => {
  119. dispatch(updateListSuccess(data));
  120. if (shouldReset) {
  121. dispatch(resetListEditor());
  122. }
  123. }).catch(err => dispatch(updateListFail(id, err)));
  124. };
  125. export const updateListRequest = id => ({
  126. type: LIST_UPDATE_REQUEST,
  127. id,
  128. });
  129. export const updateListSuccess = list => ({
  130. type: LIST_UPDATE_SUCCESS,
  131. list,
  132. });
  133. export const updateListFail = (id, error) => ({
  134. type: LIST_UPDATE_FAIL,
  135. id,
  136. error,
  137. });
  138. export const resetListEditor = () => ({
  139. type: LIST_EDITOR_RESET,
  140. });
  141. export const deleteList = id => (dispatch, getState) => {
  142. dispatch(deleteListRequest(id));
  143. api(getState).delete(`/api/v1/lists/${id}`)
  144. .then(() => dispatch(deleteListSuccess(id)))
  145. .catch(err => dispatch(deleteListFail(id, err)));
  146. };
  147. export const deleteListRequest = id => ({
  148. type: LIST_DELETE_REQUEST,
  149. id,
  150. });
  151. export const deleteListSuccess = id => ({
  152. type: LIST_DELETE_SUCCESS,
  153. id,
  154. });
  155. export const deleteListFail = (id, error) => ({
  156. type: LIST_DELETE_FAIL,
  157. id,
  158. error,
  159. });
  160. export const fetchListAccounts = listId => (dispatch, getState) => {
  161. dispatch(fetchListAccountsRequest(listId));
  162. api(getState).get(`/api/v1/lists/${listId}/accounts`, { params: { limit: 0 } })
  163. .then(({ data }) => dispatch(fetchListAccountsSuccess(listId, data)))
  164. .catch(err => dispatch(fetchListAccountsFail(listId, err)));
  165. };
  166. export const fetchListAccountsRequest = id => ({
  167. type: LIST_ACCOUNTS_FETCH_REQUEST,
  168. id,
  169. });
  170. export const fetchListAccountsSuccess = (id, accounts, next) => ({
  171. type: LIST_ACCOUNTS_FETCH_SUCCESS,
  172. id,
  173. accounts,
  174. next,
  175. });
  176. export const fetchListAccountsFail = (id, error) => ({
  177. type: LIST_ACCOUNTS_FETCH_FAIL,
  178. id,
  179. error,
  180. });
  181. export const fetchListSuggestions = q => (dispatch, getState) => {
  182. const params = {
  183. q,
  184. resolve: false,
  185. limit: 4,
  186. following: true,
  187. };
  188. api(getState).get('/api/v1/accounts/search', { params })
  189. .then(({ data }) => dispatch(fetchListSuggestionsReady(q, data)));
  190. };
  191. export const fetchListSuggestionsReady = (query, accounts) => ({
  192. type: LIST_EDITOR_SUGGESTIONS_READY,
  193. query,
  194. accounts,
  195. });
  196. export const clearListSuggestions = () => ({
  197. type: LIST_EDITOR_SUGGESTIONS_CLEAR,
  198. });
  199. export const changeListSuggestions = value => ({
  200. type: LIST_EDITOR_SUGGESTIONS_CHANGE,
  201. value,
  202. });
  203. export const addToListEditor = accountId => (dispatch, getState) => {
  204. dispatch(addToList(getState().getIn(['listEditor', 'listId']), accountId));
  205. };
  206. export const addToList = (listId, accountId) => (dispatch, getState) => {
  207. dispatch(addToListRequest(listId, accountId));
  208. api(getState).post(`/api/v1/lists/${listId}/accounts`, { account_ids: [accountId] })
  209. .then(() => dispatch(addToListSuccess(listId, accountId)))
  210. .catch(err => dispatch(addToListFail(listId, accountId, err)));
  211. };
  212. export const addToListRequest = (listId, accountId) => ({
  213. type: LIST_EDITOR_ADD_REQUEST,
  214. listId,
  215. accountId,
  216. });
  217. export const addToListSuccess = (listId, accountId) => ({
  218. type: LIST_EDITOR_ADD_SUCCESS,
  219. listId,
  220. accountId,
  221. });
  222. export const addToListFail = (listId, accountId, error) => ({
  223. type: LIST_EDITOR_ADD_FAIL,
  224. listId,
  225. accountId,
  226. error,
  227. });
  228. export const removeFromListEditor = accountId => (dispatch, getState) => {
  229. dispatch(removeFromList(getState().getIn(['listEditor', 'listId']), accountId));
  230. };
  231. export const removeFromList = (listId, accountId) => (dispatch, getState) => {
  232. dispatch(removeFromListRequest(listId, accountId));
  233. api(getState).delete(`/api/v1/lists/${listId}/accounts`, { params: { account_ids: [accountId] } })
  234. .then(() => dispatch(removeFromListSuccess(listId, accountId)))
  235. .catch(err => dispatch(removeFromListFail(listId, accountId, err)));
  236. };
  237. export const removeFromListRequest = (listId, accountId) => ({
  238. type: LIST_EDITOR_REMOVE_REQUEST,
  239. listId,
  240. accountId,
  241. });
  242. export const removeFromListSuccess = (listId, accountId) => ({
  243. type: LIST_EDITOR_REMOVE_SUCCESS,
  244. listId,
  245. accountId,
  246. });
  247. export const removeFromListFail = (listId, accountId, error) => ({
  248. type: LIST_EDITOR_REMOVE_FAIL,
  249. listId,
  250. accountId,
  251. error,
  252. });
  253. export const resetListAdder = () => ({
  254. type: LIST_ADDER_RESET,
  255. });
  256. export const setupListAdder = accountId => (dispatch, getState) => {
  257. dispatch({
  258. type: LIST_ADDER_SETUP,
  259. account: getState().getIn(['accounts', accountId]),
  260. });
  261. dispatch(fetchLists());
  262. dispatch(fetchAccountLists(accountId));
  263. };
  264. export const fetchAccountLists = accountId => (dispatch, getState) => {
  265. dispatch(fetchAccountListsRequest(accountId));
  266. api(getState).get(`/api/v1/accounts/${accountId}/lists`)
  267. .then(({ data }) => dispatch(fetchAccountListsSuccess(accountId, data)))
  268. .catch(err => dispatch(fetchAccountListsFail(accountId, err)));
  269. };
  270. export const fetchAccountListsRequest = id => ({
  271. type:LIST_ADDER_LISTS_FETCH_REQUEST,
  272. id,
  273. });
  274. export const fetchAccountListsSuccess = (id, lists) => ({
  275. type: LIST_ADDER_LISTS_FETCH_SUCCESS,
  276. id,
  277. lists,
  278. });
  279. export const fetchAccountListsFail = (id, err) => ({
  280. type: LIST_ADDER_LISTS_FETCH_FAIL,
  281. id,
  282. err,
  283. });
  284. export const addToListAdder = listId => (dispatch, getState) => {
  285. dispatch(addToList(listId, getState().getIn(['listAdder', 'accountId'])));
  286. };
  287. export const removeFromListAdder = listId => (dispatch, getState) => {
  288. dispatch(removeFromList(listId, getState().getIn(['listAdder', 'accountId'])));
  289. };