tracks.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { LIMIT, STATUSES } from "../../../utils/constants";
  2. import { getGQL } from "../../../utils/getGQL";
  3. import {
  4. deleteTrackById,
  5. getTracksCountQuery,
  6. getTracksQuery,
  7. } from "../../../utils/graphQueries";
  8. import { jwtDecode } from "../../../utils/jwtDecoder";
  9. import types from "../types";
  10. const actionGetTracksPending = () => ({
  11. type: types.GET_TRACKS_PENDING,
  12. status: STATUSES.PENDING,
  13. });
  14. const actionGetTracksSuccess = (tracks) => ({
  15. type: types.GET_TRACKS_SUCCESS,
  16. payload: tracks,
  17. status: STATUSES.SUCCESS,
  18. });
  19. const actionGetTracksFail = () => ({
  20. type: types.GET_TRACKS_FAIL,
  21. status: STATUSES.FAIL,
  22. });
  23. const actionGetTracksCountPending = () => ({
  24. type: types.GET_TRACKS_COUNT_PENDING,
  25. status: STATUSES.PENDING,
  26. });
  27. const actionGetTracksCountSuccess = (value) => ({
  28. type: types.GET_TRACKS_COUNT_SUCCESS,
  29. payload: value,
  30. status: STATUSES.SUCCESS,
  31. });
  32. const actionGetTracksCountFail = () => ({
  33. type: types.GET_TRACKS_COUNT_FAIL,
  34. status: STATUSES.FAIL,
  35. });
  36. const actionDeleteTracksPending = () => ({
  37. type: types.DELETE_TRACKS_PENDING,
  38. status: STATUSES.PENDING,
  39. });
  40. const actionDeleteTracksSuccess = () => ({
  41. type: types.DELETE_TRACKS_SUCCESS,
  42. status: STATUSES.SUCCESS,
  43. });
  44. const actionDeleteTracksFail = () => ({
  45. type: types.DELETE_TRACKS_FAIL,
  46. status: STATUSES.FAIL,
  47. });
  48. const actionUploadTracksPending = () => ({
  49. type: types.UPLOAD_TRACKS_PENDING,
  50. status: STATUSES.PENDING,
  51. });
  52. const actionUploadTracksSuccess = () => ({
  53. type: types.UPLOAD_TRACKS_SUCCESS,
  54. status: STATUSES.SUCCESS,
  55. });
  56. const actionUploadTracksFail = () => ({
  57. type: types.UPLOAD_TRACKS_FAIL,
  58. status: STATUSES.FAIL,
  59. });
  60. export const actionUploadOpen = () => ({
  61. type: types.UPLOAD_OPEN,
  62. });
  63. export const actionUploadTracks = (files) => (dispatch) => {
  64. if (files.length === 0) {
  65. dispatch(actionUploadTracksFail());
  66. return;
  67. }
  68. dispatch(actionUploadTracksPending());
  69. const tracks = files.map((file) => {
  70. const formData = new FormData();
  71. formData.append("track", file);
  72. return getGQL_Upload({ formData, fetchPart: "track" });
  73. });
  74. Promise.all(tracks)
  75. .then(() => {
  76. dispatch(actionUploadTracksSuccess());
  77. })
  78. .catch(() => dispatch(actionUploadTracksFail()));
  79. };
  80. const actionNextTracksPage = () => ({ type: types.NEXT_TRACKS_PAGE });
  81. export const actionGetTracks = (page, sortBy, findAll = false) => (
  82. dispatch
  83. ) => {
  84. const jwtData = jwtDecode(localStorage.getItem("authToken"));
  85. dispatch(actionGetTracksPending());
  86. const paginationSettings = !findAll
  87. ? {
  88. limit: [LIMIT.TRACKS_ON_PAGE],
  89. skip: [page * LIMIT.TRACKS_ON_PAGE],
  90. sort: [{ originalFileName: sortBy }],
  91. }
  92. : {};
  93. getGQL(getTracksQuery, {
  94. query: JSON.stringify([
  95. {
  96. ___owner: jwtData.id,
  97. url: { $exists: true, $ne: "" },
  98. originalFileName: { $exists: true, $ne: "" },
  99. },
  100. paginationSettings,
  101. ]),
  102. })
  103. .then((tracks) => {
  104. dispatch(actionGetTracksSuccess(tracks));
  105. dispatch(actionNextTracksPage());
  106. })
  107. .catch(() => dispatch(actionGetTracksFail()))
  108. .finally(() => dispatch(actionTracksFetchingOff()));
  109. };
  110. export const actionTracksFetchingOn = () => ({
  111. type: types.TRACKS_FETCHING_ON,
  112. });
  113. export const actionTracksFetchingOff = () => ({
  114. type: types.TRACKS_FETCHING_OFF,
  115. });
  116. export const actionGetTracksCount = () => (dispatch) => {
  117. const jwtData = jwtDecode(localStorage.getItem("authToken"));
  118. dispatch(actionGetTracksCountPending());
  119. getGQL(getTracksCountQuery, {
  120. query: JSON.stringify([
  121. {
  122. ___owner: jwtData.id,
  123. },
  124. ]),
  125. })
  126. .then((value) => dispatch(actionGetTracksCountSuccess(value)))
  127. .catch(() => dispatch(actionGetTracksCountFail()));
  128. };
  129. export const actionSetSortByTracks = (value) => ({
  130. type: types.SET_SORT_BY_TRACKS,
  131. payload: value,
  132. });
  133. export const actionRemoveTracks = () => ({ type: types.REMOVE_TRACKS });
  134. export const actionTrackToDelete = (id) => ({
  135. type: types.TRACK_TO_DELETE,
  136. payload: id,
  137. });
  138. export const actionDeleteTracks = (ids) => (dispatch) => {
  139. dispatch(actionDeleteTracksPending());
  140. const promises = ids.map((id) => getGQL(deleteTrackById, { id }));
  141. Promise.all(promises)
  142. .then(() => dispatch(actionDeleteTracksSuccess()))
  143. .catch(() => dispatch(actionDeleteTracksFail()));
  144. };