index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { jwtDecode } from "./../helpers/index";
  2. export function promiseReducer(
  3. state = {},
  4. { type, status, payload, error, name }
  5. ) {
  6. if (type === "PROMISE") {
  7. return {
  8. ...state,
  9. [name]: { status, payload, error },
  10. };
  11. }
  12. return state;
  13. }
  14. export function authReducer(state, { type, token }) {
  15. if (!state) {
  16. if (localStorage.authToken) {
  17. type = "AUTH_LOGIN";
  18. token = localStorage.authToken;
  19. } else state = {};
  20. }
  21. if (type === "AUTH_LOGIN") {
  22. let payload = jwtDecode(token);
  23. if (!!token && typeof payload === "object") {
  24. localStorage.authToken = token;
  25. return {
  26. ...state,
  27. token,
  28. payload,
  29. };
  30. } else return state;
  31. }
  32. if (type === "AUTH_LOGOUT") {
  33. localStorage.removeItem("authToken");
  34. window.location.reload();
  35. return {};
  36. }
  37. return state;
  38. }
  39. export const localStoredReducer =
  40. (reducer, localStorageName) => (state, action) => {
  41. if (!state && localStorage[localStorageName]) {
  42. return JSON.parse(localStorage[localStorageName]);
  43. } else {
  44. let newState = reducer(state, action);
  45. localStorage.setItem(localStorageName, JSON.stringify(newState));
  46. return newState;
  47. }
  48. };
  49. export const searchReducer = (state = {}, { type, action, ...params }) => {
  50. if (type === "SEARCH_RESULT") {
  51. return { ...state, searchResult: { ...params } };
  52. }
  53. if (type === "SET_SEARCH") {
  54. return { ...state, setSearch: action };
  55. }
  56. return state;
  57. };
  58. let initState = {
  59. isPlaying: false,
  60. isPaused: true,
  61. duration: 0,
  62. track: null,
  63. playlist: null,
  64. indexInPlaylist: null,
  65. currentTime: 0,
  66. volume: 0.5,
  67. };
  68. export const playerReducer = (
  69. state = initState,
  70. {
  71. type,
  72. isPlaying,
  73. isPaused,
  74. duration,
  75. track,
  76. playlist,
  77. indexInPlaylist,
  78. currentTime,
  79. volume,
  80. }
  81. ) => {
  82. if (type === "LOAD_TRACK") {
  83. console.log(track, playlist, indexInPlaylist);
  84. return {
  85. ...state,
  86. track,
  87. playlist,
  88. indexInPlaylist,
  89. };
  90. }
  91. if (type === "PLAY_TRACK") {
  92. return {
  93. ...state,
  94. isPlaying,
  95. isPaused: !isPlaying,
  96. };
  97. }
  98. if (type === "PAUSE_TRACK") {
  99. return {
  100. ...state,
  101. isPaused,
  102. isPlaying: !isPaused,
  103. };
  104. }
  105. if (type === "SET_VOLUME") {
  106. return {
  107. ...state,
  108. volume,
  109. };
  110. }
  111. if (type === "SET_DURATION") {
  112. return {
  113. ...state,
  114. duration,
  115. };
  116. }
  117. if (type === "SET_CURRENT_TIME_TRACK") {
  118. return {
  119. ...state,
  120. currentTime,
  121. };
  122. }
  123. if (type === "SET_SEEK_TIME_TRACK") {
  124. return {
  125. ...state,
  126. currentTime,
  127. };
  128. }
  129. return state;
  130. };
  131. export function routeReducer(state = {}, { type, match }) {
  132. if (type === "ROUTE") {
  133. return match;
  134. }
  135. return state;
  136. }
  137. export function scrollTracksReducer(
  138. state = {},
  139. { type, newTracks, skipTracks }
  140. ) {
  141. if (type === "ADD_TRACKS") {
  142. return {
  143. ...state,
  144. loadedTracks: [...newTracks],
  145. };
  146. }
  147. if (type === "ADD_SKIP") {
  148. return {
  149. ...state,
  150. skipTracks: state?.skipTracks
  151. ? state?.skipTracks + skipTracks
  152. : skipTracks,
  153. };
  154. }
  155. if (type === "CLEAR_SKIP") {
  156. return {
  157. ...state,
  158. skipTracks: 0,
  159. };
  160. }
  161. return state;
  162. }