Skip to content

Commit

Permalink
Merge pull request #156 from zimmerman-team/feat/DX-1550
Browse files Browse the repository at this point in the history
feat: DX-1550 Pagination for report media
  • Loading branch information
Psami-wondah authored Jun 7, 2024
2 parents f856dc8 + 9193b43 commit fe0fdde
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 140 deletions.
4 changes: 2 additions & 2 deletions src/app/hooks/useInfinityScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export const useInfinityScroll = (
//handle infinity scroll with IntersectionObserver api

const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
([entry]) => {
if (entry.isIntersecting) {
setIsObserved(true);
} else {
setIsObserved(false);
Expand Down
8 changes: 7 additions & 1 deletion src/app/hooks/useSearchMediaSources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ export function useSearchMediaSources(source: string, elementType: string) {
});
};

const reset = () => {
setData([]);
setPage(1);
setPageToken("");
};

const getUnsplashImages = async (q: string, nextPage: boolean) => {
await axios
.get(
Expand Down Expand Up @@ -178,7 +184,7 @@ export function useSearchMediaSources(source: string, elementType: string) {
const search = async (q: string, nextPage: boolean = false) => {
if ((elementType === "video" || elementType === "image") && token) {
if (!nextPage) {
setData([]);
reset();
}
setLoading(true);
const sources = sourceGetter[elementType as keyof typeof sourceGetter];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import {
import { mockChartList } from "app/modules/report-module/__test__/data";
import axios, { AxiosResponse } from "axios";
import { ChartsAppliedFiltersState } from "app/state/api/action-reducers/sync/charts/filters";
import { setupIntersectionObserverMock } from "./setupIntersectionObserver";
setupIntersectionObserverMock();

interface MockProps {
showHeaderItem: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Utility function that mocks the `IntersectionObserver` API. Necessary for components that rely
* on it, otherwise the tests will crash. Recommended to execute inside `beforeEach`.
* @param intersectionObserverMock - Parameter that is sent to the `Object.defineProperty`
* overwrite method. `jest.fn()` mock functions can be passed here if the goal is to not only
* mock the intersection observer, but its methods.
*/
export function setupIntersectionObserverMock({
root = null,
rootMargin = "",
thresholds = [],
disconnect = () => null,
observe = () => null,
takeRecords = () => [],
unobserve = () => null,
} = {}): void {
class MockIntersectionObserver implements IntersectionObserver {
readonly root: Element | null = root;
readonly rootMargin: string = rootMargin;
readonly thresholds: ReadonlyArray<number> = thresholds;
disconnect: () => void = disconnect;
observe: (target: Element) => void = observe;
takeRecords: () => IntersectionObserverEntry[] = takeRecords;
unobserve: (target: Element) => void = unobserve;
}

Object.defineProperty(window, "IntersectionObserver", {
writable: true,
configurable: true,
value: MockIntersectionObserver,
});

Object.defineProperty(global, "IntersectionObserver", {
writable: true,
configurable: true,
value: MockIntersectionObserver,
});
}
Loading

0 comments on commit fe0fdde

Please sign in to comment.