Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correct Tracks Sorting When Changing Categories or Sorting Options on Landing Page #677

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion apps/web/components/Tracks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => {
const [currentPage, setCurrentPage] = useState<number>(1);
const [loading, setLoading] = useState<boolean>(true);
const [selectedCohort, setSelectedCohort] = useState<number | null>(null);
const [key, setKey] = useState<number>(new Date().getTime());
const [value, setValue] = useState<string | undefined>(undefined);

const tracksPerPage = 10;
const isCohort2Selected = selectedCohort === CohortGroup.Two;
Expand All @@ -70,6 +72,7 @@ export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => {
t.categories.some((c) => c.category.category === selectedCategory)
);
}
newFilteredTracks.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
setFilteredTracks(newFilteredTracks);
setCurrentPage(1); // Reset to first page on filtering
setLoading(false);
Expand Down Expand Up @@ -97,6 +100,9 @@ export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => {

useEffect(() => {
filterTracks();
setValue(undefined);
setKey(new Date().getTime());
setSortBy("new");
}, [selectedCategory, selectedCohort, tracks]);

useEffect(() => {
Expand Down Expand Up @@ -170,7 +176,7 @@ export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => {
</div>

{/* Sort */}
<Select onValueChange={(e) => setSortBy(e)}>
<Select key={key} value={value} onValueChange={(e) => setSortBy(e)}>
<SelectTrigger className="w-[250px]">
<SelectValue placeholder="Sort by" />
</SelectTrigger>
Expand Down