logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

lists.js (8740B)


  1. import api from '../api';
  2. import { importFetchedAccounts } from './importer';
  3. import { showAlertForError } from './alerts';
  4. export const LIST_FETCH_REQUEST = 'LIST_FETCH_REQUEST';
  5. export const LIST_FETCH_SUCCESS = 'LIST_FETCH_SUCCESS';
  6. export const LIST_FETCH_FAIL = 'LIST_FETCH_FAIL';
  7. export const LISTS_FETCH_REQUEST = 'LISTS_FETCH_REQUEST';
  8. export const LISTS_FETCH_SUCCESS = 'LISTS_FETCH_SUCCESS';
  9. export const LISTS_FETCH_FAIL = 'LISTS_FETCH_FAIL';
  10. export const LIST_EDITOR_TITLE_CHANGE = 'LIST_EDITOR_TITLE_CHANGE';
  11. export const LIST_EDITOR_RESET = 'LIST_EDITOR_RESET';
  12. export const LIST_EDITOR_SETUP = 'LIST_EDITOR_SETUP';
  13. export const LIST_CREATE_REQUEST = 'LIST_CREATE_REQUEST';
  14. export const LIST_CREATE_SUCCESS = 'LIST_CREATE_SUCCESS';
  15. export const LIST_CREATE_FAIL = 'LIST_CREATE_FAIL';
  16. export const LIST_UPDATE_REQUEST = 'LIST_UPDATE_REQUEST';
  17. export const LIST_UPDATE_SUCCESS = 'LIST_UPDATE_SUCCESS';
  18. export const LIST_UPDATE_FAIL = 'LIST_UPDATE_FAIL';
  19. export const LIST_DELETE_REQUEST = 'LIST_DELETE_REQUEST';
  20. export const LIST_DELETE_SUCCESS = 'LIST_DELETE_SUCCESS';
  21. export const LIST_DELETE_FAIL = 'LIST_DELETE_FAIL';
  22. export const LIST_ACCOUNTS_FETCH_REQUEST = 'LIST_ACCOUNTS_FETCH_REQUEST';
  23. export const LIST_ACCOUNTS_FETCH_SUCCESS = 'LIST_ACCOUNTS_FETCH_SUCCESS';
  24. export const LIST_ACCOUNTS_FETCH_FAIL = 'LIST_ACCOUNTS_FETCH_FAIL';
  25. export const LIST_EDITOR_SUGGESTIONS_CHANGE = 'LIST_EDITOR_SUGGESTIONS_CHANGE';
  26. export const LIST_EDITOR_SUGGESTIONS_READY = 'LIST_EDITOR_SUGGESTIONS_READY';
  27. export const LIST_EDITOR_SUGGESTIONS_CLEAR = 'LIST_EDITOR_SUGGESTIONS_CLEAR';
  28. export const LIST_EDITOR_ADD_REQUEST = 'LIST_EDITOR_ADD_REQUEST';
  29. export const LIST_EDITOR_ADD_SUCCESS = 'LIST_EDITOR_ADD_SUCCESS';
  30. export const LIST_EDITOR_ADD_FAIL = 'LIST_EDITOR_ADD_FAIL';
  31. export const LIST_EDITOR_REMOVE_REQUEST = 'LIST_EDITOR_REMOVE_REQUEST';
  32. export const LIST_EDITOR_REMOVE_SUCCESS = 'LIST_EDITOR_REMOVE_SUCCESS';
  33. export const LIST_EDITOR_REMOVE_FAIL = 'LIST_EDITOR_REMOVE_FAIL';
  34. export const fetchList = id => (dispatch, getState) => {
  35. if (getState().getIn(['lists', id])) {
  36. return;
  37. }
  38. dispatch(fetchListRequest(id));
  39. api(getState).get(`/api/v1/lists/${id}`)
  40. .then(({ data }) => dispatch(fetchListSuccess(data)))
  41. .catch(err => dispatch(fetchListFail(id, err)));
  42. };
  43. export const fetchListRequest = id => ({
  44. type: LIST_FETCH_REQUEST,
  45. id,
  46. });
  47. export const fetchListSuccess = list => ({
  48. type: LIST_FETCH_SUCCESS,
  49. list,
  50. });
  51. export const fetchListFail = (id, error) => ({
  52. type: LIST_FETCH_FAIL,
  53. id,
  54. error,
  55. });
  56. export const fetchLists = () => (dispatch, getState) => {
  57. dispatch(fetchListsRequest());
  58. api(getState).get('/api/v1/lists')
  59. .then(({ data }) => dispatch(fetchListsSuccess(data)))
  60. .catch(err => dispatch(fetchListsFail(err)));
  61. };
  62. export const fetchListsRequest = () => ({
  63. type: LISTS_FETCH_REQUEST,
  64. });
  65. export const fetchListsSuccess = lists => ({
  66. type: LISTS_FETCH_SUCCESS,
  67. lists,
  68. });
  69. export const fetchListsFail = error => ({
  70. type: LISTS_FETCH_FAIL,
  71. error,
  72. });
  73. export const submitListEditor = shouldReset => (dispatch, getState) => {
  74. const listId = getState().getIn(['listEditor', 'listId']);
  75. const title = getState().getIn(['listEditor', 'title']);
  76. if (listId === null) {
  77. dispatch(createList(title, shouldReset));
  78. } else {
  79. dispatch(updateList(listId, title, shouldReset));
  80. }
  81. };
  82. export const setupListEditor = listId => (dispatch, getState) => {
  83. dispatch({
  84. type: LIST_EDITOR_SETUP,
  85. list: getState().getIn(['lists', listId]),
  86. });
  87. dispatch(fetchListAccounts(listId));
  88. };
  89. export const changeListEditorTitle = value => ({
  90. type: LIST_EDITOR_TITLE_CHANGE,
  91. value,
  92. });
  93. export const createList = (title, shouldReset) => (dispatch, getState) => {
  94. dispatch(createListRequest());
  95. api(getState).post('/api/v1/lists', { title }).then(({ data }) => {
  96. dispatch(createListSuccess(data));
  97. if (shouldReset) {
  98. dispatch(resetListEditor());
  99. }
  100. }).catch(err => dispatch(createListFail(err)));
  101. };
  102. export const createListRequest = () => ({
  103. type: LIST_CREATE_REQUEST,
  104. });
  105. export const createListSuccess = list => ({
  106. type: LIST_CREATE_SUCCESS,
  107. list,
  108. });
  109. export const createListFail = error => ({
  110. type: LIST_CREATE_FAIL,
  111. error,
  112. });
  113. export const updateList = (id, title, shouldReset) => (dispatch, getState) => {
  114. dispatch(updateListRequest(id));
  115. api(getState).put(`/api/v1/lists/${id}`, { title }).then(({ data }) => {
  116. dispatch(updateListSuccess(data));
  117. if (shouldReset) {
  118. dispatch(resetListEditor());
  119. }
  120. }).catch(err => dispatch(updateListFail(id, err)));
  121. };
  122. export const updateListRequest = id => ({
  123. type: LIST_UPDATE_REQUEST,
  124. id,
  125. });
  126. export const updateListSuccess = list => ({
  127. type: LIST_UPDATE_SUCCESS,
  128. list,
  129. });
  130. export const updateListFail = (id, error) => ({
  131. type: LIST_UPDATE_FAIL,
  132. id,
  133. error,
  134. });
  135. export const resetListEditor = () => ({
  136. type: LIST_EDITOR_RESET,
  137. });
  138. export const deleteList = id => (dispatch, getState) => {
  139. dispatch(deleteListRequest(id));
  140. api(getState).delete(`/api/v1/lists/${id}`)
  141. .then(() => dispatch(deleteListSuccess(id)))
  142. .catch(err => dispatch(deleteListFail(id, err)));
  143. };
  144. export const deleteListRequest = id => ({
  145. type: LIST_DELETE_REQUEST,
  146. id,
  147. });
  148. export const deleteListSuccess = id => ({
  149. type: LIST_DELETE_SUCCESS,
  150. id,
  151. });
  152. export const deleteListFail = (id, error) => ({
  153. type: LIST_DELETE_FAIL,
  154. id,
  155. error,
  156. });
  157. export const fetchListAccounts = listId => (dispatch, getState) => {
  158. dispatch(fetchListAccountsRequest(listId));
  159. api(getState).get(`/api/v1/lists/${listId}/accounts`, { params: { limit: 0 } }).then(({ data }) => {
  160. dispatch(importFetchedAccounts(data));
  161. dispatch(fetchListAccountsSuccess(listId, data));
  162. }).catch(err => dispatch(fetchListAccountsFail(listId, err)));
  163. };
  164. export const fetchListAccountsRequest = id => ({
  165. type: LIST_ACCOUNTS_FETCH_REQUEST,
  166. id,
  167. });
  168. export const fetchListAccountsSuccess = (id, accounts, next) => ({
  169. type: LIST_ACCOUNTS_FETCH_SUCCESS,
  170. id,
  171. accounts,
  172. next,
  173. });
  174. export const fetchListAccountsFail = (id, error) => ({
  175. type: LIST_ACCOUNTS_FETCH_FAIL,
  176. id,
  177. error,
  178. });
  179. export const fetchListSuggestions = q => (dispatch, getState) => {
  180. const params = {
  181. q,
  182. resolve: false,
  183. limit: 4,
  184. following: true,
  185. };
  186. api(getState).get('/api/v1/accounts/search', { params }).then(({ data }) => {
  187. dispatch(importFetchedAccounts(data));
  188. dispatch(fetchListSuggestionsReady(q, data));
  189. }).catch(error => dispatch(showAlertForError(error)));
  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. });