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.

141 lines
3.1 KiB

  1. import api from '../api';
  2. import { deleteFromTimelines } from './timelines';
  3. import { fetchStatusCard } from './cards';
  4. export const STATUS_FETCH_REQUEST = 'STATUS_FETCH_REQUEST';
  5. export const STATUS_FETCH_SUCCESS = 'STATUS_FETCH_SUCCESS';
  6. export const STATUS_FETCH_FAIL = 'STATUS_FETCH_FAIL';
  7. export const STATUS_DELETE_REQUEST = 'STATUS_DELETE_REQUEST';
  8. export const STATUS_DELETE_SUCCESS = 'STATUS_DELETE_SUCCESS';
  9. export const STATUS_DELETE_FAIL = 'STATUS_DELETE_FAIL';
  10. export const CONTEXT_FETCH_REQUEST = 'CONTEXT_FETCH_REQUEST';
  11. export const CONTEXT_FETCH_SUCCESS = 'CONTEXT_FETCH_SUCCESS';
  12. export const CONTEXT_FETCH_FAIL = 'CONTEXT_FETCH_FAIL';
  13. export function fetchStatusRequest(id, skipLoading) {
  14. return {
  15. type: STATUS_FETCH_REQUEST,
  16. id,
  17. skipLoading
  18. };
  19. };
  20. export function fetchStatus(id) {
  21. return (dispatch, getState) => {
  22. const skipLoading = getState().getIn(['statuses', id], null) !== null;
  23. dispatch(fetchContext(id));
  24. dispatch(fetchStatusCard(id));
  25. if (skipLoading) {
  26. return;
  27. }
  28. dispatch(fetchStatusRequest(id, skipLoading));
  29. api(getState).get(`/api/v1/statuses/${id}`).then(response => {
  30. dispatch(fetchStatusSuccess(response.data, skipLoading));
  31. }).catch(error => {
  32. dispatch(fetchStatusFail(id, error, skipLoading));
  33. });
  34. };
  35. };
  36. export function fetchStatusSuccess(status, skipLoading) {
  37. return {
  38. type: STATUS_FETCH_SUCCESS,
  39. status,
  40. skipLoading
  41. };
  42. };
  43. export function fetchStatusFail(id, error, skipLoading) {
  44. return {
  45. type: STATUS_FETCH_FAIL,
  46. id,
  47. error,
  48. skipLoading,
  49. skipAlert: true
  50. };
  51. };
  52. export function deleteStatus(id) {
  53. return (dispatch, getState) => {
  54. dispatch(deleteStatusRequest(id));
  55. api(getState).delete(`/api/v1/statuses/${id}`).then(response => {
  56. dispatch(deleteStatusSuccess(id));
  57. dispatch(deleteFromTimelines(id));
  58. }).catch(error => {
  59. dispatch(deleteStatusFail(id, error));
  60. });
  61. };
  62. };
  63. export function deleteStatusRequest(id) {
  64. return {
  65. type: STATUS_DELETE_REQUEST,
  66. id: id
  67. };
  68. };
  69. export function deleteStatusSuccess(id) {
  70. return {
  71. type: STATUS_DELETE_SUCCESS,
  72. id: id
  73. };
  74. };
  75. export function deleteStatusFail(id, error) {
  76. return {
  77. type: STATUS_DELETE_FAIL,
  78. id: id,
  79. error: error
  80. };
  81. };
  82. export function fetchContext(id) {
  83. return (dispatch, getState) => {
  84. dispatch(fetchContextRequest(id));
  85. api(getState).get(`/api/v1/statuses/${id}/context`).then(response => {
  86. dispatch(fetchContextSuccess(id, response.data.ancestors, response.data.descendants));
  87. }).catch(error => {
  88. if (error.response.status === 404) {
  89. dispatch(deleteFromTimelines(id));
  90. }
  91. dispatch(fetchContextFail(id, error));
  92. });
  93. };
  94. };
  95. export function fetchContextRequest(id) {
  96. return {
  97. type: CONTEXT_FETCH_REQUEST,
  98. id
  99. };
  100. };
  101. export function fetchContextSuccess(id, ancestors, descendants) {
  102. return {
  103. type: CONTEXT_FETCH_SUCCESS,
  104. id,
  105. ancestors,
  106. descendants,
  107. statuses: ancestors.concat(descendants)
  108. };
  109. };
  110. export function fetchContextFail(id, error) {
  111. return {
  112. type: CONTEXT_FETCH_FAIL,
  113. id,
  114. error,
  115. skipAlert: true
  116. };
  117. };