Skip to content

Commit

Permalink
Merge pull request #203 from asmitdeb/randomize-projects
Browse files Browse the repository at this point in the history
Randomize Projects List
  • Loading branch information
harshkhandeparkar authored Nov 29, 2023
2 parents ecc42c8 + cac2756 commit cfb1e31
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/pages/Projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MdCancel } from "react-icons/md";
import ProjectCard from "../components/ProjectCard";
import { makeRequest } from "../util/backend";
import { IEndpointTypes } from "../util/types";
import { shuffle } from "../util/shuffle";
import Fuse from "fuse.js";
import SpinnerLoader from "../components/SpinnerLoader";
import { IconContext } from "react-icons";
Expand Down Expand Up @@ -32,7 +33,7 @@ function Projects() {
makeRequest("project", "get")
.then((response) => {
if (response.is_ok) {
setProjects(response.response);
setProjects(shuffle(response.response));
} else {
setError("Error fetching projects.");
console.log(response.response);
Expand Down
12 changes: 12 additions & 0 deletions src/util/shuffle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// function to randomly shuffle an array
export function shuffle(array: any[]):any[] {
let arraycopy: any[]= [];
array.forEach((val: any) => arraycopy.push(Object.assign({}, val)));
for (let i = arraycopy.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
let temp:any = arraycopy[i];
arraycopy[i] = arraycopy[j];
arraycopy[j] = temp;
}
return arraycopy;
}

0 comments on commit cfb1e31

Please sign in to comment.