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

add search function for boards #158

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions client/src/context/BoardContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ interface BoardContextType {
isToastSuccess: string;
setIsToastSuccess: (isToastSuccess: string) => void;
handleDownloadTemplate: (board: Board) => void;
searchInput: string;
setSearchInput: (searchInput: string) => void;
searchedBoards: Board[];
setSearchedBoards: (boards: Board[]) => void;
}

// Create the context with a default undefined value
Expand All @@ -54,6 +58,8 @@ export const BoardProvider = ({ children }: { children: ReactNode }) => {
const { editCard } = useEditCard();
const { deleteCard } = useDeleteCard();
const [isToastSuccess, setIsToastSuccess] = useState<string>("");
const [searchInput, setSearchInput] = useState<string>("");
const [searchedBoards, setSearchedBoards] = useState<Board[]>([]);

const updateTitleText = () => {
if (selectedCard) {
Expand Down Expand Up @@ -202,6 +208,10 @@ export const BoardProvider = ({ children }: { children: ReactNode }) => {
isToastSuccess,
setIsToastSuccess,
handleDownloadTemplate,
searchInput,
setSearchInput,
searchedBoards,
setSearchedBoards
}}
>
{children}
Expand Down
50 changes: 39 additions & 11 deletions client/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const Home: React.FC = () => {
setIsAddingNewBoard,
populateDummyData,
isToastSuccess,
searchInput,
setSearchInput,
searchedBoards,
setSearchedBoards
} = useBoard();

const { currentBoards, setCurrentBoards, currentBoardId } =
Expand Down Expand Up @@ -69,6 +73,7 @@ const Home: React.FC = () => {
);
setCurrentBoards(updatedBoards);
setUserBoards(updatedBoards);
setSearchedBoards(updatedBoards);
}
} catch (error) {
console.error("Error fetching boards:", error);
Expand Down Expand Up @@ -127,6 +132,20 @@ const Home: React.FC = () => {
setIsAddingNewBoard(false);
}, []);

useEffect(() => {
const originalBoards = userBoards;
let filteredBoards = userBoards;
if(searchInput){

filteredBoards = userBoards.filter((board) =>
board.name.toLowerCase().includes(searchInput.toLowerCase())
);
setSearchedBoards(filteredBoards);
}else{
setSearchedBoards(originalBoards);
}
}, [searchInput])

useEffect(() => {
if (isToastSuccess.length > 0) {
toast.success(isToastSuccess, {
Expand Down Expand Up @@ -187,11 +206,9 @@ const Home: React.FC = () => {
{selectedBoard &&
!selectedCard &&
!isTemplate &&
!uploadedTemplateNames.includes(selectedBoard!.name) &&
!uploadedTemplateNames.includes(selectedBoard!.name) &&
selectedBoard.cards?.length &&
selectedBoard.cards.length > 1 && (
<UploadBoardComponent />
)}
selectedBoard.cards.length > 1 && <UploadBoardComponent />}
{!selectedBoard && !selectedCard && !isAddingNewBoard && (
<>
{!isSearching && (
Expand Down Expand Up @@ -224,17 +241,28 @@ const Home: React.FC = () => {
{isAddingNewBoard ? (
<CreateBoardComponent handleCancel={handleCancel} />
) : (
<button
className=" bg-flair font-primary text-secondaryElements px-4 py-2 mb-4 rounded hover:text-white"
onClick={() => setIsAddingNewBoard(true)}
>
Create a new board
</button>
<>
<div className="mb-4">
<input
type="text"
id="searchInput"
placeholder="Search for boards"
className="mt-1 block w-full px-3 py-2 border border-gray-300 bg-white rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-1 focus:ring-gray-300 focus:border-gray-300"
onChange={(e) => setSearchInput(e.target.value)}
/>
</div>
<button
className=" bg-flair font-primary text-secondaryElements px-4 py-2 mb-4 rounded hover:text-white"
onClick={() => setIsAddingNewBoard(true)}
>
Create a new board
</button>
</>
)}

<div className="text-center">
<ul className="flex flex-row flex-wrap gap-4 justify-center">
{userBoards.map((board, i) => (
{searchedBoards.map((board, i) => (
<li key={i} className="cursor-pointer">
<BoardPreview
handleSelectBoard={() => setSelectedBoard(board)}
Expand Down