actionCatsFind.js 974 B

123456789101112131415161718192021222324
  1. import { actionPromise } from '../reducers';
  2. export const actionCatsFind =
  3. ({ text = '', limit = 7, skip = 0, promiseName = 'catsFind', orderBy = '' }) =>
  4. async (dispatch, getState) => {
  5. dispatch(
  6. actionPromise(
  7. promiseName,
  8. fetch(`/categories/?limit=${limit}&skip=${skip}&text=${text}${orderBy && `&orderBy=` + orderBy}`, {
  9. method: 'GET',
  10. headers: {
  11. 'Content-Type': 'application/json',
  12. ...(localStorage.authToken ? { Authorization: 'Bearer ' + localStorage.authToken } : {}),
  13. },
  14. })
  15. .then((res) => res.json())
  16. .then((data) => {
  17. if (data.errors) {
  18. throw new Error(JSON.stringify(data.errors));
  19. } else return data.data;
  20. })
  21. )
  22. );
  23. };