@bigbinary/neeto-image-uploader-frontend 1.6.0-beta1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import { noop as noop$1, keysToCamelCase, findBy, isPresent } from '@bigbinary/n
4
4
  import { Delete, LeftArrow, MenuHorizontal } from '@bigbinary/neeto-icons';
5
5
  import Button from '@bigbinary/neetoui/Button';
6
6
  import Typography from '@bigbinary/neetoui/Typography';
7
- import { pipe, values, flatten as flatten$1, map, toUpper, join, equals, isNil, isEmpty, mergeRight, mergeLeft, concat, __, inc, mergeDeepRight } from 'ramda';
7
+ import { pipe, values, flatten as flatten$1, map, toUpper, join, equals, isNil, isEmpty, mergeRight, mergeLeft, identity, prop, mergeDeepRight } from 'ramda';
8
8
  import { Trans, useTranslation } from 'react-i18next';
9
9
  import { globalProps } from '@bigbinary/neeto-commons-frontend/initializers';
10
10
  import i18next, { t as t$1 } from 'i18next';
@@ -14,8 +14,8 @@ import Dropdown from '@bigbinary/neetoui/Dropdown';
14
14
  import NeetoUIModal from '@bigbinary/neetoui/Modal';
15
15
  import Tab from '@bigbinary/neetoui/Tab';
16
16
  import Spinner from '@bigbinary/neetoui/Spinner';
17
- import { useMutation, useQuery } from '@tanstack/react-query';
18
- import { useMutationWithInvalidation, useDebounce, useFuncDebounce } from '@bigbinary/neeto-commons-frontend/react-utils';
17
+ import { useMutation, useInfiniteQuery, keepPreviousData } from '@tanstack/react-query';
18
+ import { useMutationWithInvalidation, useDebounce } from '@bigbinary/neeto-commons-frontend/react-utils';
19
19
  import axios from 'axios';
20
20
  import { DirectUpload } from '@rails/activestorage';
21
21
  import { withEventTargetValue } from '@bigbinary/neeto-commons-frontend/utils';
@@ -3905,19 +3905,30 @@ function _objectSpread$3(e) { for (var r = 1; r < arguments.length; r++) { var t
3905
3905
  var useFetchAssets = function useFetchAssets(_ref) {
3906
3906
  var searchTerm = _ref.searchTerm,
3907
3907
  url = _ref.url,
3908
- page = _ref.page,
3909
3908
  per = _ref.per;
3910
3909
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3911
- return useQuery(_objectSpread$3({
3912
- queryKey: [QUERY_KEYS.ASSETS_LIST],
3913
- queryFn: function queryFn() {
3910
+ return useInfiniteQuery(_objectSpread$3({
3911
+ queryKey: [QUERY_KEYS.ASSETS_LIST, searchTerm],
3912
+ queryFn: function queryFn(_ref2) {
3913
+ var _ref2$pageParam = _ref2.pageParam,
3914
+ pageParam = _ref2$pageParam === void 0 ? 1 : _ref2$pageParam;
3914
3915
  return imagesApi.fetch({
3915
3916
  searchTerm: searchTerm,
3916
3917
  url: url,
3917
- page: page,
3918
+ page: pageParam,
3918
3919
  per: per
3919
3920
  });
3920
3921
  },
3922
+ getNextPageParam: function getNextPageParam(lastPage, _, lastPageParam) {
3923
+ return lastPage.length === IMAGES_PER_PAGE ? lastPageParam + 1 : null;
3924
+ },
3925
+ select: function select(data) {
3926
+ var results = data.pages.flatMap(identity);
3927
+ return {
3928
+ results: results
3929
+ };
3930
+ },
3931
+ initialPageParam: 1,
3921
3932
  staleTime: ASSETS_STALE_TIME
3922
3933
  }, options));
3923
3934
  };
@@ -4751,74 +4762,42 @@ var InfiniteScroller = function InfiniteScroller(_ref) {
4751
4762
  };
4752
4763
 
4753
4764
  var Images = function Images(_ref) {
4754
- var query = _ref.query,
4755
- debouncedQuery = _ref.debouncedQuery,
4765
+ var debouncedQuery = _ref.debouncedQuery,
4756
4766
  setSelectedImage = _ref.setSelectedImage;
4757
- var _useState = useState(true),
4758
- _useState2 = _slicedToArray$2(_useState, 2),
4759
- hasMore = _useState2[0],
4760
- setHasMore = _useState2[1];
4761
- var _useState3 = useState([]),
4762
- _useState4 = _slicedToArray$2(_useState3, 2),
4763
- images = _useState4[0],
4764
- setImages = _useState4[1];
4765
- var _useState5 = useState(1),
4766
- _useState6 = _slicedToArray$2(_useState5, 2),
4767
- pageNo = _useState6[0],
4768
- setPageNo = _useState6[1];
4769
- var _useState7 = useState(false),
4770
- _useState8 = _slicedToArray$2(_useState7, 2),
4771
- error = _useState8[0],
4772
- setError = _useState8[1];
4773
- var onSuccess = function onSuccess() {
4774
- var results = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
4775
- equals(pageNo, 1) ? setImages(results) : setImages(concat(__, results));
4776
- setPageNo(inc);
4777
- setHasMore(results.length === IMAGES_PER_PAGE);
4778
- };
4779
- var onError = function onError() {
4780
- return setError(true);
4781
- };
4782
4767
  var _useTranslation = useTranslation(),
4783
4768
  t = _useTranslation.t;
4784
4769
  var _useFetchAssets = useFetchAssets({
4785
4770
  searchTerm: debouncedQuery,
4786
- page: pageNo,
4787
4771
  per: IMAGES_PER_PAGE,
4788
4772
  url: ASSETS_FETCH_URL
4789
- }, {
4790
- onSuccess: onSuccess,
4791
- onError: onError
4792
4773
  }),
4793
- refetch = _useFetchAssets.refetch,
4794
- isFetching = _useFetchAssets.isFetching;
4795
- var loadMore = useFuncDebounce(function () {
4796
- if (isFetching) return;
4797
- if (!hasMore) return;
4798
- if (pageNo > 1) {
4799
- setError(false);
4800
- refetch();
4801
- }
4802
- }, 500);
4803
- useEffect(function () {
4804
- setPageNo(1);
4805
- }, [query]);
4806
- useEffect(function () {
4807
- setImages([]);
4808
- refetch();
4809
- }, [debouncedQuery]);
4810
- if (error) {
4774
+ fetchNextPage = _useFetchAssets.fetchNextPage,
4775
+ hasNextPage = _useFetchAssets.hasNextPage,
4776
+ _useFetchAssets$data = _useFetchAssets.data,
4777
+ _useFetchAssets$data2 = _useFetchAssets$data === void 0 ? {} : _useFetchAssets$data,
4778
+ _useFetchAssets$data3 = _useFetchAssets$data2.results,
4779
+ results = _useFetchAssets$data3 === void 0 ? [] : _useFetchAssets$data3,
4780
+ isLoading = _useFetchAssets.isLoading,
4781
+ isError = _useFetchAssets.isError;
4782
+ if (isError) {
4811
4783
  return /*#__PURE__*/jsx(Typography, {
4812
4784
  className: "mt-9 flex justify-center",
4813
4785
  style: "body2",
4814
4786
  children: t("neetoImageUploader.unsplash.errorMessage")
4815
4787
  });
4816
4788
  }
4817
- return /*#__PURE__*/jsx(InfiniteScroller, {
4818
- hasMore: hasMore,
4819
- images: images,
4820
- loadMore: loadMore,
4789
+ if (isLoading) {
4790
+ return /*#__PURE__*/jsx("div", {
4791
+ className: "flex justify-around",
4792
+ children: /*#__PURE__*/jsx(Spinner, {})
4793
+ });
4794
+ }
4795
+ return /*#__PURE__*/createElement(InfiniteScroller, {
4821
4796
  setSelectedImage: setSelectedImage,
4797
+ hasMore: hasNextPage,
4798
+ images: results,
4799
+ key: debouncedQuery,
4800
+ loadMore: fetchNextPage,
4822
4801
  tab: TABS[0].key
4823
4802
  });
4824
4803
  };
@@ -4848,18 +4827,32 @@ function ownKeys$2(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymb
4848
4827
  function _objectSpread$2(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$2(Object(t), !0).forEach(function (r) { _defineProperty$3(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$2(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4849
4828
  var UNSPLASH_LIST = QUERY_KEYS.UNSPLASH_LIST;
4850
4829
  var useFetchUnsplashImages = function useFetchUnsplashImages(_ref) {
4851
- var pageNo = _ref.pageNo,
4852
- query = _ref.query;
4830
+ var query = _ref.query;
4853
4831
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
4854
- return useQuery(_objectSpread$2({
4855
- queryKey: [UNSPLASH_LIST],
4856
- queryFn: function queryFn() {
4832
+ return useInfiniteQuery(_objectSpread$2({
4833
+ queryKey: [UNSPLASH_LIST, {
4834
+ query: query
4835
+ }],
4836
+ queryFn: function queryFn(_ref2) {
4837
+ var _ref2$pageParam = _ref2.pageParam,
4838
+ pageParam = _ref2$pageParam === void 0 ? 1 : _ref2$pageParam;
4857
4839
  return unsplashApi.searchUnsplashImages({
4858
- pageNo: pageNo,
4840
+ pageNo: pageParam,
4859
4841
  query: query
4860
4842
  });
4861
4843
  },
4862
- staleTime: UNSPLASH_STALE_TIME
4844
+ initialPageParam: 1,
4845
+ getNextPageParam: function getNextPageParam(lastPage, _, lastPageParam) {
4846
+ return lastPageParam < lastPage.totalPages ? lastPageParam + 1 : null;
4847
+ },
4848
+ select: function select(data) {
4849
+ var results = data.pages.flatMap(prop("results"));
4850
+ return {
4851
+ results: results
4852
+ };
4853
+ },
4854
+ staleTime: UNSPLASH_STALE_TIME,
4855
+ placeholderData: keepPreviousData
4863
4856
  }, options));
4864
4857
  };
4865
4858
 
@@ -4906,37 +4899,12 @@ var uploadWithImageKit = function uploadWithImageKit(file) {
4906
4899
  };
4907
4900
 
4908
4901
  var UnsplashLibrary = function UnsplashLibrary(_ref) {
4909
- var query = _ref.query,
4910
- debouncedQuery = _ref.debouncedQuery,
4902
+ var debouncedQuery = _ref.debouncedQuery,
4911
4903
  setSelectedImage = _ref.setSelectedImage,
4912
4904
  setIsUploading = _ref.setIsUploading,
4913
4905
  setActiveTab = _ref.setActiveTab;
4914
- var _useState = useState(true),
4915
- _useState2 = _slicedToArray$2(_useState, 2),
4916
- hasMore = _useState2[0],
4917
- setHasMore = _useState2[1];
4918
- var _useState3 = useState([]),
4919
- _useState4 = _slicedToArray$2(_useState3, 2),
4920
- images = _useState4[0],
4921
- setImages = _useState4[1];
4922
- var _useState5 = useState(1),
4923
- _useState6 = _slicedToArray$2(_useState5, 2),
4924
- pageNo = _useState6[0],
4925
- setPageNo = _useState6[1];
4926
- var _useState7 = useState(false),
4927
- _useState8 = _slicedToArray$2(_useState7, 2),
4928
- error = _useState8[0],
4929
- setError = _useState8[1];
4930
- var onSuccess = function onSuccess(_ref2) {
4931
- var _ref2$results = _ref2.results,
4932
- results = _ref2$results === void 0 ? [] : _ref2$results,
4933
- totalPages = _ref2.totalPages;
4934
- equals(pageNo, 1) ? setImages(results) : setImages(concat(__, results));
4935
- setPageNo(inc);
4936
- setHasMore(pageNo < totalPages);
4937
- };
4938
4906
  var handleUnsplashImageSelect = /*#__PURE__*/function () {
4939
- var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(image) {
4907
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(image) {
4940
4908
  var _image$urls, response, file, result;
4941
4909
  return _regeneratorRuntime.wrap(function _callee$(_context) {
4942
4910
  while (1) switch (_context.prev = _context.next) {
@@ -4986,50 +4954,42 @@ var UnsplashLibrary = function UnsplashLibrary(_ref) {
4986
4954
  }, _callee, null, [[0, 19, 23, 26]]);
4987
4955
  }));
4988
4956
  return function handleUnsplashImageSelect(_x) {
4989
- return _ref3.apply(this, arguments);
4957
+ return _ref2.apply(this, arguments);
4990
4958
  };
4991
4959
  }();
4992
- var onError = function onError() {
4993
- return setError(true);
4994
- };
4995
4960
  var _useTranslation = useTranslation(),
4996
4961
  t = _useTranslation.t;
4997
4962
  var _useFetchUnsplashImag = useFetchUnsplashImages({
4998
- query: debouncedQuery,
4999
- pageNo: pageNo
5000
- }, {
5001
- onSuccess: onSuccess,
5002
- onError: onError
4963
+ query: debouncedQuery
5003
4964
  }),
5004
- refetch = _useFetchUnsplashImag.refetch,
5005
- isFetching = _useFetchUnsplashImag.isFetching;
5006
- var loadMore = useFuncDebounce(function () {
5007
- if (isFetching || !hasMore) return;
5008
- if (pageNo > 1) {
5009
- setError(false);
5010
- refetch();
5011
- }
5012
- }, 500);
5013
- useEffect(function () {
5014
- setPageNo(1);
5015
- }, [query]);
5016
- useEffect(function () {
5017
- setPageNo(1);
5018
- setImages([]);
5019
- refetch();
5020
- }, [debouncedQuery]);
5021
- if (error) {
4965
+ fetchNextPage = _useFetchUnsplashImag.fetchNextPage,
4966
+ hasNextPage = _useFetchUnsplashImag.hasNextPage,
4967
+ _useFetchUnsplashImag2 = _useFetchUnsplashImag.data,
4968
+ _useFetchUnsplashImag3 = _useFetchUnsplashImag2 === void 0 ? {} : _useFetchUnsplashImag2,
4969
+ _useFetchUnsplashImag4 = _useFetchUnsplashImag3.results,
4970
+ results = _useFetchUnsplashImag4 === void 0 ? [] : _useFetchUnsplashImag4,
4971
+ isError = _useFetchUnsplashImag.isError,
4972
+ isLoading = _useFetchUnsplashImag.isLoading;
4973
+ if (isError) {
5022
4974
  return /*#__PURE__*/jsx(Typography, {
5023
4975
  className: "mt-9 flex justify-center",
5024
4976
  style: "body2",
5025
4977
  children: t("neetoImageUploader.unsplash.errorMessage")
5026
4978
  });
5027
4979
  }
5028
- return /*#__PURE__*/jsx(InfiniteScroller, {
4980
+ if (isLoading) {
4981
+ return /*#__PURE__*/jsx("div", {
4982
+ className: "flex justify-around",
4983
+ children: /*#__PURE__*/jsx(Spinner, {})
4984
+ });
4985
+ }
4986
+ return /*#__PURE__*/createElement(InfiniteScroller, {
4987
+ debouncedQuery: debouncedQuery,
5029
4988
  handleUnsplashImageSelect: handleUnsplashImageSelect,
5030
- hasMore: hasMore,
5031
- images: images,
5032
- loadMore: loadMore,
4989
+ hasMore: hasNextPage,
4990
+ images: results,
4991
+ key: debouncedQuery,
4992
+ loadMore: fetchNextPage,
5033
4993
  tab: TABS[1].key
5034
4994
  });
5035
4995
  };