statuses.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. };