From 6b9671b0eae57269ceef35459be5607d68c771eb Mon Sep 17 00:00:00 2001 From: SandraBergstrom Date: Tue, 27 Jun 2023 10:16:55 +0000 Subject: [PATCH] #18 #15 Add function fetchMoredata to utils.js --- src/utils/utils.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/utils/utils.js diff --git a/src/utils/utils.js b/src/utils/utils.js new file mode 100644 index 0000000..da7d56a --- /dev/null +++ b/src/utils/utils.js @@ -0,0 +1,16 @@ +import { axiosReq } from "../api/axiosDefaults"; + +export const fetchMoreData = async (resource, setResource) => { + try { + const { data } = await axiosReq.get(resource.next); + setResource((prevResource) => ({ + ...prevResource, + next: data.next, + results: data.results.reduce((acc, cur) => { + return acc.some((accResult) => accResult.id === cur.id) + ? acc + : [...acc, cur]; + }, prevResource.results), + })); + } catch (err) {} +};