Skip to content

Commit

Permalink
Merge pull request LibrePhotos#42 from jmalm/migrate-redux
Browse files Browse the repository at this point in the history
Store throttled function as ref so it can be reused
  • Loading branch information
derneuere authored Nov 21, 2021
2 parents 7cb6854 + acdd713 commit b7e8472
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/layouts/photos/NoTimestampPhotosView.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
fetchNoTimestampPhotoPaginated,
} from "../../actions/photosActions";
import throttle from "lodash";
import React, { useEffect, useState, useCallback } from "react";
import React, { useEffect, useState, useRef } from "react";
import _ from "lodash";
import { PhotoListView } from "../../components/photolist/PhotoListView";
import { PhotosetType, PhotosState } from "../../reducers/photosReducer";
Expand All @@ -19,18 +18,20 @@ export const NoTimestampPhotosView = () => {
fetchNoTimestampPhotoPaginated(dispatch, 1);
}
}, [dispatch]); // Only run on first render


const getImages = (visibleItems: any) => {
if (visibleItems.filter((i: any) => i.isTemp && i.isTemp != undefined).length > 0) {
var firstTempObject = visibleItems.filter((i: any) => i.isTemp)[0];
var page = Math.ceil((parseInt(firstTempObject.id) + 1) / 100);
if (page > 1 && !pages.includes(page)) {
setPages([...pages, page]);
fetchNoTimestampPhotoPaginated(dispatch, page);
setPages([...pages, page]);
fetchNoTimestampPhotoPaginated(dispatch, page);
}
};
}

const throttledGetImages = useRef(_.throttle(visibleItems => getImages(visibleItems), 500)).current;

return (
<PhotoListView
Expand All @@ -42,10 +43,9 @@ export const NoTimestampPhotosView = () => {
idx2hash={photosFlat}
numberOfItems={numberOfPhotos}
updateItems={(visibleItems: any) =>
throttle((getImages(visibleItems), 500))
throttledGetImages(visibleItems)
}
selectable={true}
/>

);
}

0 comments on commit b7e8472

Please sign in to comment.