Skip to content

Commit 11e0452

Browse files
authored
fix: Load random page (#3229)
* fix: Load random page * fix: refactor loadRandomPage method * fix: notEnoughItemsToAskMore condition
1 parent 71aa6c3 commit 11e0452

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

src/components/ItemEditorPage/LeftPanel/Items/Items.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export default class Items extends React.PureComponent<Props, State> {
278278
const { isLoading, totalItems, reviewedItems } = this.props
279279
const { currentTab } = this.state
280280
let headerInnerContent
281-
const notEnoughItemsToAskMore = !!totalItems && (totalItems < LEFT_PANEL_PAGE_SIZE || reviewedItems.length === totalItems)
281+
const notEnoughItemsToAskMore = !totalItems || totalItems < LEFT_PANEL_PAGE_SIZE || reviewedItems.length === totalItems
282282

283283
switch (currentTab) {
284284
case ItemPanelTabs.TO_REVIEW:

src/components/ItemEditorPage/LeftPanel/LeftPanel.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,23 +151,21 @@ export default class LeftPanel extends React.PureComponent<Props, State> {
151151
loadRandomPage = (currentItems: Item[]) => {
152152
const { pages } = this.state
153153
const { totalItems, totalCollections, onSetReviewedItems } = this.props
154+
154155
const totalResources = this.isCollectionTabActive() ? totalCollections : totalItems
155156
const totalPages = Math.ceil(totalResources! / LEFT_PANEL_PAGE_SIZE)
156-
if (pages.length !== totalPages) {
157-
let randomPage: number | undefined
158-
while (randomPage === undefined) {
159-
randomPage = this.getRandomPage(1, totalPages)
160-
if (pages.includes(randomPage)) {
161-
randomPage = undefined
162-
}
163-
}
164-
onSetReviewedItems(currentItems)
165-
if (randomPage !== undefined) {
166-
this.setState(prevState => ({ pages: [...prevState.pages, randomPage as number] }), this.fetchResource)
157+
158+
if (totalPages > pages.length) {
159+
const availablePages = [...Array(totalPages).keys()].map(i => i + 1).filter(page => !pages.includes(page))
160+
161+
if (availablePages.length > 0) {
162+
const randomPage = availablePages[Math.floor(Math.random() * availablePages.length)]
163+
164+
this.setState(prevState => ({ pages: [...prevState.pages, randomPage] }), this.fetchResource)
167165
}
168-
} else {
169-
onSetReviewedItems(currentItems)
170166
}
167+
168+
onSetReviewedItems(currentItems)
171169
}
172170

173171
handleTabChange = (tab: ItemEditorTabs) => {

0 commit comments

Comments
 (0)