-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: collections tab [FC-0062] (#1257)
* feat: add collections query to search results * feat: collections tab with basic cards * feat: add collection card also fix inifinite scroll for collections * feat: collection empty states * test: add test for collections card
- Loading branch information
1 parent
4035931
commit 9b61037
Showing
24 changed files
with
1,070 additions
and
446 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { history } from '@edx/frontend-platform'; | ||
|
||
export const useScrollToHashElement = ({ isLoading }: { isLoading: boolean }) => { | ||
const [elementWithHash, setElementWithHash] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const currentHash = window.location.hash.substring(1); | ||
|
||
if (currentHash) { | ||
const element = document.getElementById(currentHash); | ||
if (element) { | ||
element.scrollIntoView(); | ||
history.replace({ hash: '' }); | ||
} | ||
setElementWithHash(currentHash); | ||
} | ||
}, [isLoading]); | ||
|
||
return { elementWithHash }; | ||
}; | ||
|
||
export const useEscapeClick = ({ onEscape, dependency }: { onEscape: () => void, dependency: any }) => { | ||
useEffect(() => { | ||
const handleEscapeClick = (event: KeyboardEvent) => { | ||
if (event.key === 'Escape') { | ||
onEscape(); | ||
} | ||
}; | ||
|
||
window.addEventListener('keydown', handleEscapeClick); | ||
|
||
return () => { | ||
window.removeEventListener('keydown', handleEscapeClick); | ||
}; | ||
}, [dependency]); | ||
}; | ||
|
||
/** | ||
* Hook which loads next page of items on scroll | ||
*/ | ||
export const useLoadOnScroll = ( | ||
hasNextPage: boolean | undefined, | ||
isFetchingNextPage: boolean, | ||
fetchNextPage: () => void, | ||
enabled: boolean, | ||
) => { | ||
useEffect(() => { | ||
if (enabled) { | ||
const onscroll = () => { | ||
// Verify the position of the scroll to implement an infinite scroll. | ||
// Used `loadLimit` to fetch next page before reach the end of the screen. | ||
const loadLimit = 300; | ||
const scrolledTo = window.scrollY + window.innerHeight; | ||
const scrollDiff = document.body.scrollHeight - scrolledTo; | ||
const isNearToBottom = scrollDiff <= loadLimit; | ||
if (isNearToBottom && hasNextPage && !isFetchingNextPage) { | ||
fetchNextPage(); | ||
} | ||
}; | ||
window.addEventListener('scroll', onscroll); | ||
return () => { | ||
window.removeEventListener('scroll', onscroll); | ||
}; | ||
} | ||
return () => { }; | ||
}, [hasNextPage, isFetchingNextPage, fetchNextPage]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.